QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#588555 | #8211. Enumerating Substrings | fzx | AC ✓ | 537ms | 82128kb | C++14 | 4.8kb | 2024-09-25 13:10:59 | 2024-09-25 13:10:59 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int INFM=2e3+5;
const int INFN=1e6+5;
const int Mod=1e9+7;
bool vis[INFM];
int n,m,k,fav[INFN],L[INFM],R[INFM],ifav[INFN],f[INFM][INFM],B[INFM][INFM];
int ksm(int x,int y) {
int ba=x%Mod,ans=1;
while (y) {
if (y&1) ans=(ans*ba)%Mod;
ba=(ba*ba)%Mod;y>>=1;
}
return ans;
}
void init() {
int N=1e6;fav[0]=1;
for (int i=1;i<=N;i++) fav[i]=fav[i-1]*i%Mod;
ifav[N]=ksm(fav[N],Mod-2);
for (int i=N-1;~i;i--) ifav[i]=ifav[i+1]*(i+1)%Mod;
}
int C(int x,int y) {
if (x<y) return 0;
return fav[x]*ifav[y]%Mod*ifav[x-y]%Mod;
}
int calc(int x,int y) {
return (ksm(x,y+1)-1)*ksm(x-1,Mod-2)%Mod;
}
signed main()
{
ios::sync_with_stdio(false);
cin>>n>>m>>k;init();int res=0;
for (int i=0;i<=m+3;i++)
for (int j=0;j<=m+3;j++)
f[i][j]=0;
f[0][0]=1;
for (int i=0;i<m;i++)
for (int j=0;j<=i;j++) {
f[i+1][j+1]+=f[i][j]*(k-j-(i-j)/2);
if (j) f[i+1][j-1]+=f[i][j]*j;
f[i+1][j+1]%=Mod;
if (j) f[i+1][j-1]%=Mod;
}
int sum=0;
for (int j=0;j<=m;j++) sum+=f[m][j],sum%=Mod;
for (int i=1;i<=n;i++) {
if (i+m-1>n) continue;
res+=sum*ksm(k,n-m)%Mod;res%=Mod;
}
for (int j=0;j<=m;j++) {
B[j][0]=1;
int ba=k-j-(m-j)/2+1;
for (int u=1;u<=m;u++)
B[j][u]=B[j][u-1]*ba%Mod,ba++;
}
int res2=0,invk=ksm(k,Mod-2);
for (int b=1;b<=m/2;b++) {
// b ... b
int sum=0;
for (int j=0;j<=m-b*2;j++) {
// if (k-j-(m-b*2-j)/2<b) continue;
// int cnt3=1;
// for (int q=k-j-(m-b*2-j)/2;q>=k-j-(m-b*2-j)/2-b+1;q--) cnt3*=q,cnt3%=Mod;
// for (int q=1;q<=b;q++) cnt3*=ifav[q]*fav[q-1]%Mod,cnt3%=Mod;
sum+=f[m-b*2][j]*B[j][b]%Mod;
sum%=Mod;
}
int A=ksm(invk*invk%Mod,m-b),L7=ksm(invk,m-b),invA=ksm(A-1,Mod-2),S3=0,L6=ksm(invk,b);
L6*=sum;L6%=Mod;
for (int i=1;i<=m;i++) {
int cnt=1;
// m-b
if (i-1<m-b) cnt*=1,cnt%=Mod;
else cnt*=1-L7,cnt%=Mod;
// (m-b)*j<=n-b-i+1
int li=(n-b-i+1)/(m-b),S2=0;
if (A==1) S2-=li/2,S2%=Mod;
else S2=-(ksm(A,li/2+1)-A)*invA%Mod;
S2*=cnt;S2%=Mod;
S2*=L6;S2%=Mod;
res2+=S2;res2%=Mod;
}
if (m+1<=n-b+1) {
for (int k=0;k<m;k++) vis[k]=0;
for (int i=m+1;i<=n-b+1;i++) {
if (vis[(n-b-i+1)%(m-b)]) break;
vis[(n-b-i+1)%(m-b)]=1;L[(n-b-i+1)%(m-b)]=(n-b-i+1);
}
for (int k=0;k<m;k++) vis[k]=0;
for (int i=n-b+1;i>=m+1;i--) {
if (vis[(n-b-i+1)%(m-b)]) break;
vis[(n-b-i+1)%(m-b)]=1;R[(n-b-i+1)%(m-b)]=(n-b-i+1);
}
for (int i=0;i<m-b;i++) {
if (!vis[i]) continue;
int S2=0;
int ll=R[i]/(m-b),rr=L[i]/(m-b);
if (ll>rr) continue;
if (ll==rr) {
if (A==1) {
for (int li=ll;li<=rr;li++)
S2-=li/2,S2%=Mod;
} else {
for (int li=ll;li<=rr;li++)
S2+=-ksm(A,li/2+1),S2%=Mod;
S2*=invA%Mod;S2%=Mod;
S2+=A*invA%Mod*(rr-ll+1)%Mod;S2%=Mod;
}
S3+=S2;S3%=Mod;
continue;
}
if (A==1) {
int L=ll,R=rr;
if (ll&1) S2+=-ll/2,S2%=Mod,ll++;
if (rr%2==0) S2+=-rr/2,S2%=Mod,rr--;
S2-=(ll/2+rr/2)*(rr/2-ll/2+1);S2%=Mod;
// for (int li=ll/2;li<=rr/2;li++)
// S2-=li*2,S2%=Mod;
} else {
int L=ll,R=rr;
if (ll&1) S2+=-ksm(A,ll/2),S2%=Mod,ll++;
if (rr%2==0) S2+=-ksm(A,rr/2),S2%=Mod,rr--;
S2+=-(calc(A,rr/2)-calc(A,ll/2-1))*2;
// for (int li=ll/2;li<=rr/2;li++)
// S2+=-ksm(A,li)*2,S2%=Mod;
ll=L;rr=R;
S2*=invA*A%Mod;S2%=Mod;
S2+=A*invA%Mod*(rr-ll+1)%Mod;S2%=Mod;
}
S3+=S2;S3%=Mod;
}
S3*=(invk-ksm(invk,1+(m-b)));S3%=Mod;
S3*=sum;S3%=Mod;S3*=ksm(invk,b-1);S3%=Mod;
res2+=S3;res2%=Mod;
}
}
res2*=ksm(k,n);res2%=Mod;res2+=Mod;res2%=Mod;
res+=res2;res%=Mod;
res+=Mod;res%=Mod;
cout<<res<<"\n";
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 22692kb
input:
4 2 3
output:
228
result:
ok 1 number(s): "228"
Test #2:
score: 0
Accepted
time: 537ms
memory: 82060kb
input:
999999 1999 12345678
output:
52352722
result:
ok 1 number(s): "52352722"
Test #3:
score: 0
Accepted
time: 9ms
memory: 23164kb
input:
7 4 2
output:
182
result:
ok 1 number(s): "182"
Test #4:
score: 0
Accepted
time: 7ms
memory: 22300kb
input:
4 3 4
output:
480
result:
ok 1 number(s): "480"
Test #5:
score: 0
Accepted
time: 11ms
memory: 22516kb
input:
3 1 1
output:
3
result:
ok 1 number(s): "3"
Test #6:
score: 0
Accepted
time: 7ms
memory: 22492kb
input:
5 5 1
output:
0
result:
ok 1 number(s): "0"
Test #7:
score: 0
Accepted
time: 12ms
memory: 23020kb
input:
7 4 3
output:
5784
result:
ok 1 number(s): "5784"
Test #8:
score: 0
Accepted
time: 3ms
memory: 22808kb
input:
5 2 4
output:
3932
result:
ok 1 number(s): "3932"
Test #9:
score: 0
Accepted
time: 3ms
memory: 21592kb
input:
8 2 2
output:
1522
result:
ok 1 number(s): "1522"
Test #10:
score: 0
Accepted
time: 8ms
memory: 21344kb
input:
8 1 2
output:
2048
result:
ok 1 number(s): "2048"
Test #11:
score: 0
Accepted
time: 8ms
memory: 21788kb
input:
7 5 3
output:
2430
result:
ok 1 number(s): "2430"
Test #12:
score: 0
Accepted
time: 8ms
memory: 22932kb
input:
10 4 3
output:
272004
result:
ok 1 number(s): "272004"
Test #13:
score: 0
Accepted
time: 90ms
memory: 42444kb
input:
675978 614 2
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 0
Accepted
time: 23ms
memory: 24604kb
input:
244613 38 1
output:
0
result:
ok 1 number(s): "0"
Test #15:
score: 0
Accepted
time: 61ms
memory: 69760kb
input:
186293 1462 1
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 25ms
memory: 50852kb
input:
24867 886 1
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 0
Accepted
time: 198ms
memory: 55256kb
input:
976164 1014 2
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 0
Accepted
time: 19ms
memory: 21560kb
input:
179356 2 716844809
output:
577866092
result:
ok 1 number(s): "577866092"
Test #19:
score: 0
Accepted
time: 50ms
memory: 27624kb
input:
621001 130 310625363
output:
892869197
result:
ok 1 number(s): "892869197"
Test #20:
score: 0
Accepted
time: 137ms
memory: 49116kb
input:
678862 850 754662812
output:
582264789
result:
ok 1 number(s): "582264789"
Test #21:
score: 0
Accepted
time: 165ms
memory: 55108kb
input:
650845 978 348443366
output:
825425732
result:
ok 1 number(s): "825425732"
Test #22:
score: 0
Accepted
time: 66ms
memory: 35972kb
input:
669914 402 87448112
output:
318098088
result:
ok 1 number(s): "318098088"
Test #23:
score: 0
Accepted
time: 112ms
memory: 40532kb
input:
998593 530 681228665
output:
408255654
result:
ok 1 number(s): "408255654"
Test #24:
score: 0
Accepted
time: 466ms
memory: 82060kb
input:
369361 1954 125266115
output:
509912384
result:
ok 1 number(s): "509912384"
Test #25:
score: 0
Accepted
time: 283ms
memory: 65772kb
input:
900226 1378 424079373
output:
406320917
result:
ok 1 number(s): "406320917"
Test #26:
score: 0
Accepted
time: 291ms
memory: 69828kb
input:
334887 1506 17859926
output:
503264679
result:
ok 1 number(s): "503264679"
Test #27:
score: 0
Accepted
time: 103ms
memory: 40232kb
input:
936048 544 53978328
output:
548647866
result:
ok 1 number(s): "548647866"
Test #28:
score: 0
Accepted
time: 192ms
memory: 63528kb
input:
152789 1264 792983073
output:
839541707
result:
ok 1 number(s): "839541707"
Test #29:
score: 0
Accepted
time: 274ms
memory: 67684kb
input:
714493 1392 91796331
output:
721071046
result:
ok 1 number(s): "721071046"
Test #30:
score: 0
Accepted
time: 98ms
memory: 49072kb
input:
269571 816 830801077
output:
330064211
result:
ok 1 number(s): "330064211"
Test #31:
score: 0
Accepted
time: 173ms
memory: 52976kb
input:
845120 944 424581630
output:
348960190
result:
ok 1 number(s): "348960190"
Test #32:
score: 0
Accepted
time: 60ms
memory: 32772kb
input:
533990 368 163586376
output:
522092095
result:
ok 1 number(s): "522092095"
Test #33:
score: 0
Accepted
time: 384ms
memory: 80012kb
input:
181707 1792 462399634
output:
373795106
result:
ok 1 number(s): "373795106"
Test #34:
score: 0
Accepted
time: 459ms
memory: 82128kb
input:
417349 1920 761212891
output:
587051329
result:
ok 1 number(s): "587051329"
Test #35:
score: 0
Accepted
time: 253ms
memory: 65564kb
input:
526583 1344 500217637
output:
108767800
result:
ok 1 number(s): "108767800"
Test #36:
score: 0
Accepted
time: 127ms
memory: 46892kb
input:
867054 769 93998191
output:
239123369
result:
ok 1 number(s): "239123369"
Extra Test:
score: 0
Extra Test Passed