QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#65813 | #4843. Infectious Disease | clf123456 | AC ✓ | 1617ms | 302192kb | C++20 | 1.3kb | 2022-12-03 18:34:26 | 2022-12-03 18:34:28 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e7+5e6;
const int mod=1e9+7;
int jc[maxn],inv[maxn],n;
signed dp[1000000][16];
int mp[maxn];
int C(int n,int m){
return jc[n]*inv[n-m]%mod*inv[m]%mod;
}
int ksm(int x,int p,int mod){
int ans=1;
while(p){
if(p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
int dfs(int x,int y){
if(x==0) return 0;
//if(y!=1&&mp[y]==0) cout<<y<<" "<<mp[y]<<endl;
if(y!=1) assert(mp[y]);
if(dp[x][mp[y]]!=-1) return dp[x][mp[y]];
int r=min(2*x,n-y);
int z=min(2*y,n-y);
int sum=n-y;
int mn=max(0LL,r+z-sum);
int mx=min(r,z);
int ans=1;
int fm=ksm(C(n-y,z),mod-2,mod);
for(int i=mn;i<=mx;i++){
ans+=C(r,i)*C(n-y-r,z-i)%mod*dfs(r-i,z+y)%mod*fm%mod;
ans%=mod;
}
return dp[x][mp[y]]=ans;
}
void init(){
jc[0]=inv[0]=1;
for(int i=1;i<maxn;i++) jc[i]=i*jc[i-1]%mod;
inv[maxn-1]=ksm(jc[maxn-1],mod-2,mod);
for(int i=maxn-2;i>=1;i--){
inv[i]=(i+1)*inv[i+1]%mod;
}
mp[1]=0;
int base=3;
for(int i=1;;i++){
if(base>=n){
mp[n]=i;
break;
}
mp[base]=i;
//cout<<base<<" "<<mp[base]<<endl;
base*=3;
}
}
signed main(){
memset(dp,-1,sizeof(dp));
cin>>n;
init();
int base=1;
cout<<dfs(1,1)<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 219ms
memory: 301832kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 187ms
memory: 301528kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: 0
Accepted
time: 1588ms
memory: 300972kb
input:
14000000
output:
44565093
result:
ok 1 number(s): "44565093"
Test #4:
score: 0
Accepted
time: 1617ms
memory: 301916kb
input:
12345678
output:
123143093
result:
ok 1 number(s): "123143093"
Test #5:
score: 0
Accepted
time: 264ms
memory: 302088kb
input:
776777
output:
764022068
result:
ok 1 number(s): "764022068"
Test #6:
score: 0
Accepted
time: 201ms
memory: 302008kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 143ms
memory: 302052kb
input:
4
output:
666666673
result:
ok 1 number(s): "666666673"
Test #8:
score: 0
Accepted
time: 172ms
memory: 300188kb
input:
5
output:
833333341
result:
ok 1 number(s): "833333341"
Test #9:
score: 0
Accepted
time: 157ms
memory: 301028kb
input:
6
output:
300000004
result:
ok 1 number(s): "300000004"
Test #10:
score: 0
Accepted
time: 174ms
memory: 301736kb
input:
7
output:
533333339
result:
ok 1 number(s): "533333339"
Test #11:
score: 0
Accepted
time: 166ms
memory: 300340kb
input:
8
output:
952380961
result:
ok 1 number(s): "952380961"
Test #12:
score: 0
Accepted
time: 153ms
memory: 302028kb
input:
9
output:
964285723
result:
ok 1 number(s): "964285723"
Test #13:
score: 0
Accepted
time: 185ms
memory: 301712kb
input:
10
output:
416666672
result:
ok 1 number(s): "416666672"
Test #14:
score: 0
Accepted
time: 167ms
memory: 301724kb
input:
26
output:
990086590
result:
ok 1 number(s): "990086590"
Test #15:
score: 0
Accepted
time: 179ms
memory: 300788kb
input:
27
output:
528360342
result:
ok 1 number(s): "528360342"
Test #16:
score: 0
Accepted
time: 166ms
memory: 300432kb
input:
28
output:
273239648
result:
ok 1 number(s): "273239648"
Test #17:
score: 0
Accepted
time: 527ms
memory: 301700kb
input:
4782967
output:
332194401
result:
ok 1 number(s): "332194401"
Test #18:
score: 0
Accepted
time: 549ms
memory: 301924kb
input:
4782968
output:
362625027
result:
ok 1 number(s): "362625027"
Test #19:
score: 0
Accepted
time: 523ms
memory: 301180kb
input:
4782969
output:
971032452
result:
ok 1 number(s): "971032452"
Test #20:
score: 0
Accepted
time: 542ms
memory: 302192kb
input:
4782970
output:
452836984
result:
ok 1 number(s): "452836984"
Test #21:
score: 0
Accepted
time: 526ms
memory: 301820kb
input:
4782971
output:
349324970
result:
ok 1 number(s): "349324970"
Test #22:
score: 0
Accepted
time: 533ms
memory: 300628kb
input:
4782972
output:
46862963
result:
ok 1 number(s): "46862963"
Test #23:
score: 0
Accepted
time: 196ms
memory: 302104kb
input:
114514
output:
972669965
result:
ok 1 number(s): "972669965"
Test #24:
score: 0
Accepted
time: 506ms
memory: 300916kb
input:
1919810
output:
482430785
result:
ok 1 number(s): "482430785"
Test #25:
score: 0
Accepted
time: 1552ms
memory: 302164kb
input:
8877877
output:
486769120
result:
ok 1 number(s): "486769120"