QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#420425 | #2824. 找树 | 300_205_205# | RE | 0ms | 0kb | C++20 | 3.2kb | 2024-05-24 18:03:33 | 2024-05-24 18:03:37 |
answer
//#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define N 1000005
#define mod 998244353
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ls (rt<<1)
#define rs ((rt<<1)|1)
#define fi first
#define se second
#define INF 1e9
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
int qpow(int a,int b){
int res=1;
for(;b;b>>=1){
if(b&1) res=res*a%mod;
a=a*a%mod;
}
return res;
}
int fac[N],ifac[N];
int C(int n,int m){
if(m>n||m<0||n<0) return 0;
return fac[n]*ifac[n-m]%mod*ifac[m]%mod;
}
void init(){
fac[0]=1;
for(int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod;
ifac[N-1]=qpow(fac[N-1],mod-2);
for(int i=N-2;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod;
}
inline int lowbit(int x){return x&(-x);}
inline int read(){
int x=0,t=1;char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') t=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch-'0');
ch=getchar();
}
return x*t;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10+'0');
}
int T,n,k,q,p1[N],p2[N],val[N],B=sqrt(mod)+40,inc[25],sum[N],now[25],res[25],out[N][21];
int dp[N][21];
struct matrix{int a[21][21];}tmp,a0[40000],ab[40000];
matrix operator*(matrix a,matrix b){
matrix c;memset(c.a,0,sizeof(c.a));
for(int i=0;i<=k;i++)
for(int j=0;j<=k;j++)
for(int o=0;o<=k;o++)
(c.a[i][j]+=a.a[i][o]*b.a[o][j])%=mod;
return c;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>k>>q;init();
//k位,每位0~n
p1[0]=1;for(int i=1;i<=k;i++) p1[i]=p1[i-1]*n;
p2[0]=1;for(int i=1;i<=k;i++) p2[i]=p2[i-1]*(n-1);
for(int i=0;i<=k;i++)
inc[i]=C(k,i)*p2[i]%mod,inc[i]=qpow(inc[i],mod-2);
for(int i=0;i<=k;i++){
if(i) tmp.a[i-1][i]=i;
tmp.a[i][i]=i*(n-2)%mod;
if(i!=k) tmp.a[i+1][i]=(k-i)*(n-1)%mod;
}
a0[1]=tmp;
for(int i=2;i<=B;i++) a0[i]=a0[i-1]*tmp;
ab[1]=a0[B];
for(int i=2;i<=B;i++) ab[i]=ab[i-1]*a0[B];
for(int i=0;i<=k;i++) a0[0].a[i][i]=ab[0].a[i][i]=1;
int all=1;
for(int i=1;i<=k;i++) all=all*n;
for(int i=0;i<all;i++) cin>>val[i],dp[i][0]=val[i];
for(int l=0;l<all;l++)
for(int i=0;i<k;i++){
int pos=l-p1[i]*((l/p1[i])%n);
out[l][i]=pos;
}
for(int i=0;i<k;i++){
for(int j=i+1;j>=1;j--){
for(int l=0;l<all;l++) sum[l]=0;
for(int l=0;l<all;l++){
int pos=out[l][i];
(sum[pos]+=dp[l][j-1]);
}
for(int l=0;l<all;l++){
int pos=out[l][i];
dp[l][j]=(dp[l][j]+sum[pos]-dp[l][j-1]);
}
}
}
for(int l=0;l<all;l++)
for(int i=0;i<=k;i++)
dp[l][i]%=mod;
int lst=1;
while(q--){
int a,b;cin>>a>>b;
b=b*lst%mod;
for(int i=0;i<=k;i++) now[i]=a0[b%B].a[i][0];
for(int i=0;i<=k;i++) res[i]=0;
for(int i=0;i<=k;i++)
for(int j=0;j<=k;j++){
int v=ab[b/B].a[i][j];
(res[i]+=v*now[j])%=mod;
}
int ans=0;
for(int i=0;i<=k;i++)(ans+=res[i]*inc[i]%mod*dp[a][i])%=mod;
//for(int i=0;i<=k;i++) cout<<res[i]<<" "<<dp[a][i]<<" "<<inc[i]<<endl;
cout<<ans<<endl;lst=ans;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
70 100 &&&&&&&&&&&& 59 1 577 11 18 513 41 11 6 7 33 1 25 26 577 3 57 516 28 5 3 13 56 0 2 32 7 66 53 517 42 31 519 41 34 70 37 54 579 24 15 512 70 32 512 51 49 64 55 19 69 3 31 517 24 8 582 44 45 513 47 58 517 47 41 577 33 22 519 57 41 0 70 50 67 3 6 6 14 43 6 21 1 579 32 27 576 62 7 514 39 36 69 12...