QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65776 | #4843. Infectious Disease | gwj12345 | TL | 154ms | 222048kb | C++20 | 2.6kb | 2022-12-03 16:46:54 | 2022-12-03 16:46:56 |
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 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[N-1] = pows(fac[N-1], mod - 2, mod);
for (LL i = N-1; i; i -- ) {
inv[i - 1] = inv[i] * i % mod;
}
}
LL lucas(LL x, LL y, LL mod){
if(y<0 || y>x)return 0;
return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
LL inv2(LL a, LL p){
// return inv[a];
return pows(a, p-2, p);
}
// 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*inv2(lucas(y+z,min(2*x,y+z),mod),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*inv2(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";
// cout<<inv2(10,mod)<<" "<<inv[10]<<"\n";
// cout<<lucas(4,4,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
详细
Test #1:
score: 100
Accepted
time: 154ms
memory: 222000kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 149ms
memory: 222048kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: -100
Time Limit Exceeded
input:
14000000