QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#65764 | #4843. Infectious Disease | gwj12345 | TL | 19ms | 3404kb | C++14 | 2.2kb | 2022-12-03 16:38:24 | 2022-12-03 16:38:26 |
Judging History
answer
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const LL mod = 1e9+7;
const LL N = 1e7+4e6+10;
LL pows(LL a, LL x, LL p){
if(x==0)return 1;
LL t = pows(a, x/2, p); if(x%2==0)return t*t%p;else return t*t%p*a%p;
}
LL inv(LL a, LL p){ return pows(a, p-2, p); }
// LL fac[N], inv[N];
// void init(){
// fac[0] = inv[0] = 1;
// for(LL i = 1; i < N; i++){
// fac[i] = fac[i-1]*i%mod; inv[i] = pows(fac[i], mod-2, mod);
// }
// }
// LL C(LL x, LL y){
// if(y<0 || y>x)return 0;
// return fac[x]*inv[y]%mod*inv[x-y]%mod;
// }
LL C(LL a , LL b, LL p){
LL res = 1;
for(LL i = 1, j = a; i <= b; i++, j--){
res = res*j%p;
res = res*pows(i,mod-2,mod)%mod;
}
return res%p;
}
LL lucas(LL a, LL b, LL p){
if(a < p && b<p) return C(a,b,p);
else return C(a%p, b%p, p)*lucas(a/p, b/p, p)%p;
}
LL n;
LL ans = 0;
//打过疫苗的,正常人,感染的人
void dfs(LL x, LL y, LL z, LL d, LL p){
// cout<<x<<" "<<y<<" "<<z<<" "<<d<<" "<<p<<"\n";
if(z==0){
ans = (ans+p*d%mod)%mod;
return ;
}
if(2*z<= y){
y = (y-z)%mod;
(z += z)%=mod;
}else{
z = (z+y)%mod;
y = 0;
}
LL t;
if(z-(2*x-y)<0){
t = 0;
}else{
t = min(z, z-(2*x-y));
}
for(LL i = 0; i <= t; i++){
// if(z-(2*x-y) > i){
// dfs(x*3, y-(2*x-(z-i)), i, d+1, p*lucas(z,z-i,mod)*lucas(y,2*x-(z-i),mod)*inv(lucas(y+z,min(2*x,y+z),mod),mod));
// break;
// }
// dfs(min(n,x*3), max(0LL, y-(2*x-(z-i))), i, d+1, p*lucas(z,z-i,mod)*lucas(y,2*x-(z-i),mod)*inv(lucas(y+z,min(2*x,y+z),mod),mod));
dfs(min(n,x*3), max(0LL, y-(2*x-(z-i))), i, d+1, p*lucas(z,z-i,mod)%mod*lucas(y,min(y, 2*x-(z-i)),mod)%mod*inv(lucas(y+z,min(2*x,y+z),mod),mod)%mod);
}
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// cout<<29*inv(15,mod)%mod<<"\n";
// cout<<2*inv(5,mod)%mod<<"\n";
// cout<<11LL*inv(6,mod)%mod<<"\n";
// init();
// cout<<lucas(4,3,mod);
cin>>n;
dfs(1,n-2,1,0,1);
cout<<ans%mod<<"\n";
return 0;
}
//114
//505208013
//2
//1
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3224kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 19ms
memory: 3404kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: -100
Time Limit Exceeded
input:
14000000