-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrixExpo.cpp
More file actions
153 lines (138 loc) · 3.62 KB
/
matrixExpo.cpp
File metadata and controls
153 lines (138 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//------------------------ Un, Deux, Trois, Quatre, Cinq -----------------------//
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
//Shorthands
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define ld long double
#define vll vector<ll>
#define vv vector<vll>
#define pb push_back
#define lb lower_bound2
#define ub upper_bound
#define pll pair<ll,ll>
#define vpll vector<pll>
#define ff first
#define ss second
#define tll tuple<ll,ll,ll>
#define vtll vector<tll>
#define mt make_tuple
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,a,b) for(ll i=a;i>=b;i--)
#define vect(a,n) vll a(n); rep(i,0,n) cin>>a[i];
#define all(a) a.begin(),a.end()
#define sortall(a) sort(a.begin(),a.end());
#define shuf(a) shuffle(a.begin(), a.end(), default_random_engine(0));
//Debug
#define printv(a) rep(i,0,a.size()) cout<<a[i]<<" "; cout<<endl;
#define pout(p) cout<<"("<<p.ff<<","<<p.ss<<")"<<" ";
#define printvp(a) rep(i,0,a.size()) cout<<"("<<a[i].ff<<","<<a[i].ss<<")"<<" "; cout<<endl;
#define tout(t) cout<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")"<<endl;
#define trace(x) cout<< '>' << #x << ':' << (x) << "\n";
#define trace2(x,y) cout<< '>' << #x << ':' << (x) << " | " << #y << ':' << (y) << "\n";
#define trace3(a,b,c) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n";
#define trace4(a,b,c,d) cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n";
//Constants
const ll INF = 1e12;
const ll MOD = 1e9 + 7;
const ll mod = 998244353;
const ll mxN = 1e5+1;
//Input-Output File
void fileio(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout) ;
#endif
}
//-------------------------- Six, Sept, Huit, Neuf, Dix ------------------------//
const ll M=MOD;
const ll N=11;
void multiply(ll mat[N][N],ll res[N][N])
{
ll temp[N][N];
rep(i,0,N)
{
rep(j,0,N)
{
ll c=0;
rep(k,0,N) c=(c+mat[i][k]*res[k][j]%M)%M;
temp[i][j]=c;
}
}
rep(i,0,N) rep(j,0,N) res[i][j]=temp[i][j];
}
void matrixExpo(ll mat[N][N], ll k)
{
ll res[N][N];
rep(i,0,N)
{
rep(j,0,N)
{
if(i==j) res[i][j]=1;
else res[i][j]=0;
}
}
while(k>0)
{
if(k&1) multiply(mat,res);
k/=2;
multiply(mat,mat);
}
rep(i,0,N) rep(j,0,N) mat[i][j]=res[i][j];
}
void solve()
{
ll n; cin>>n;
vll a(n+1);
rep(i,1,n+1) cin>>a[i];
vll c(11,0);
rep(i,1,n+1) c[a[i]]++;
ll q; cin>>q;
while(q--)
{
ll k,r; cin>>k>>r;
map<ll,ll> m;
rep(i,0,k)
{
ll x; cin>>x;
if(x<=n) m[x]=1;
}
for(auto x:m) c[a[x.ff]]--;
vll dp(11,0), s(11,0);
rep(i,1,11)
{
dp[i]=c[i];
rep(j,1,11)
{
if(i-j<=0) break;
dp[i]+=c[j]*dp[i-j]%M;
dp[i]%=M;
}
s[i]=(s[i-1]+dp[i])%M;
}
for(auto x:m) c[a[x.ff]]++;
if(r<=10){ cout<<s[r]<<endl; continue; }
ll mat[11][11];
mat[0][0]=1;
mat[1][0]=0;
rep(i,1,11) mat[0][i]=mat[1][i]=c[11-i];
rep(i,2,11)
{
rep(j,0,11) mat[i][j]=0;
mat[i][i-1]=1;
}
matrixExpo(mat,r-10);
ll ans=mat[0][0]*s[10]%M;
rep(i,1,11){ ans+=mat[0][i]*dp[11-i]%M; ans%=M; }
cout<<ans<<endl;
}
}
int main()
{
fastio; fileio();
solve();
}