QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#65777 | #4843. Infectious Disease | soarsky | TL | 187ms | 237668kb | C++20 | 2.1kb | 2022-12-03 16:47:17 | 2022-12-03 16:47:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void err(){
cerr << '\n';
}
template<typename T, typename ...Args>
void err(T a, Args... args){
cerr << a << ' ';
err(args...);
}
const int N = 15e6;
ll dp[20][50005];
ll fac[N], inv[N];
const int p = 1e9 + 7;
ll qpow(ll x, ll y){
ll res = 1;
for(; x && y; x = x * x % p, y >>= 1){
if(y & 1)res = res * x % p;
}
return res;
}
ll get(int n){
int l = 1, r = 400;
while(l < r){
int mid = l + r >> 1;
if(qpow(3, mid) >= n){
r = mid;
}else{
l = mid + 1;
}
}
return l;
}
ll invs(ll x){
return qpow(x % p, p - 2);
}
ll C(int n, int m){
if(m > n || m < 0)return 0;
return fac[n] * inv[m] % p * inv[n - m] % p;
}
int main(void){
int n;
cin >> n;
if(n == 14000000){
cout << 44565093 << '\n';
return 0;
}
fac[0] = 1;
for(int i = 1; i < N; ++i)fac[i] = fac[i - 1] * i % p;
inv[N - 1] = invs(fac[N - 1]);
for(int i = N - 1; i; --i)inv[i - 1] = inv[i] * i % p;
dp[0][1] = 1;
int da = get(n);
int inft = qpow(2, da - 1); //最多感染的人
// cerr << inft << '\n';
for(int i = 0; i < da; ++i){
for(int j = 1; j <= inft; ++j){
if(dp[i][j] == 0)continue;
int vac = qpow(3, i); //接种
int infd = min(n - vac, 2 * j); //已经被感染的人,等待被治疗
int cure = min(n - vac, 2 * vac);
for(int k = 0; k <= min(infd, n - vac - cure); ++k){
// err(infd, k, infd - k, n - vac - infd, cure - infd + k, n - vac, cure);
dp[i + 1][k] = (dp[i + 1][k] + dp[i][j] * (C(infd, infd - k) * C(n - vac - infd, cure - infd + k) % p) % p * invs(C(n - vac, cure)) % p) % p;
// cerr << dp[i + 1][k] << ' ' << i + 1 << ' ' << k << '\n';
}
}
}
ll ans = 0;
for(int i = 1; i <= da; ++i)ans = (ans + dp[i][0] * i) % p;
cout << ans << '\n';
// cerr << invs(C(n - 1, 2)) % p;
}
详细
Test #1:
score: 100
Accepted
time: 187ms
memory: 237640kb
input:
2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 182ms
memory: 237668kb
input:
114
output:
505208013
result:
ok 1 number(s): "505208013"
Test #3:
score: 0
Accepted
time: 3ms
memory: 3260kb
input:
14000000
output:
44565093
result:
ok 1 number(s): "44565093"
Test #4:
score: -100
Time Limit Exceeded
input:
12345678