QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65827 | #4843. Infectious Disease | clf123456 | AC ✓ | 4315ms | 240952kb | C++20 | 1.3kb | 2022-12-03 18:55:59 | 2022-12-03 18:56:02 |
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;
int mp[maxn];
struct hash_map{
int operator()(const pair<int,int>& p) const{
return p.first+mp[p.second]*maxn;
}
};
unordered_map<pair<int,int>,int,hash_map>dp;
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;
pair<int,int>t=make_pair(x,y);
if(dp[t]) return dp[t];
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[t]=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;
}
详细
Test #1:
score: 100
Accepted
time: 145ms
memory: 239444kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 159ms
memory: 238020kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: 0
Accepted
time: 4315ms
memory: 240048kb
input:
14000000
output:
44565093
result:
ok 1 number(s): "44565093"
Test #4:
score: 0
Accepted
time: 4268ms
memory: 240668kb
input:
12345678
output:
123143093
result:
ok 1 number(s): "123143093"
Test #5:
score: 0
Accepted
time: 402ms
memory: 239480kb
input:
776777
output:
764022068
result:
ok 1 number(s): "764022068"
Test #6:
score: 0
Accepted
time: 169ms
memory: 239276kb
input:
3
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 181ms
memory: 238100kb
input:
4
output:
666666673
result:
ok 1 number(s): "666666673"
Test #8:
score: 0
Accepted
time: 169ms
memory: 239676kb
input:
5
output:
833333341
result:
ok 1 number(s): "833333341"
Test #9:
score: 0
Accepted
time: 170ms
memory: 238796kb
input:
6
output:
300000004
result:
ok 1 number(s): "300000004"
Test #10:
score: 0
Accepted
time: 187ms
memory: 238612kb
input:
7
output:
533333339
result:
ok 1 number(s): "533333339"
Test #11:
score: 0
Accepted
time: 138ms
memory: 238348kb
input:
8
output:
952380961
result:
ok 1 number(s): "952380961"
Test #12:
score: 0
Accepted
time: 181ms
memory: 239556kb
input:
9
output:
964285723
result:
ok 1 number(s): "964285723"
Test #13:
score: 0
Accepted
time: 154ms
memory: 238528kb
input:
10
output:
416666672
result:
ok 1 number(s): "416666672"
Test #14:
score: 0
Accepted
time: 184ms
memory: 237996kb
input:
26
output:
990086590
result:
ok 1 number(s): "990086590"
Test #15:
score: 0
Accepted
time: 191ms
memory: 238756kb
input:
27
output:
528360342
result:
ok 1 number(s): "528360342"
Test #16:
score: 0
Accepted
time: 151ms
memory: 238056kb
input:
28
output:
273239648
result:
ok 1 number(s): "273239648"
Test #17:
score: 0
Accepted
time: 1206ms
memory: 240336kb
input:
4782967
output:
332194401
result:
ok 1 number(s): "332194401"
Test #18:
score: 0
Accepted
time: 1197ms
memory: 239140kb
input:
4782968
output:
362625027
result:
ok 1 number(s): "362625027"
Test #19:
score: 0
Accepted
time: 1230ms
memory: 238636kb
input:
4782969
output:
971032452
result:
ok 1 number(s): "971032452"
Test #20:
score: 0
Accepted
time: 1268ms
memory: 239800kb
input:
4782970
output:
452836984
result:
ok 1 number(s): "452836984"
Test #21:
score: 0
Accepted
time: 1225ms
memory: 238444kb
input:
4782971
output:
349324970
result:
ok 1 number(s): "349324970"
Test #22:
score: 0
Accepted
time: 1195ms
memory: 239096kb
input:
4782972
output:
46862963
result:
ok 1 number(s): "46862963"
Test #23:
score: 0
Accepted
time: 202ms
memory: 238892kb
input:
114514
output:
972669965
result:
ok 1 number(s): "972669965"
Test #24:
score: 0
Accepted
time: 1230ms
memory: 239324kb
input:
1919810
output:
482430785
result:
ok 1 number(s): "482430785"
Test #25:
score: 0
Accepted
time: 4247ms
memory: 240952kb
input:
8877877
output:
486769120
result:
ok 1 number(s): "486769120"