题解:P11577 [CCC2020] Epidemiology

· · 题解

思路

分析题目:

0 天:已感染人数 n 人。

1 天:新增人数为 nr 人,已感染人数为 n+nr 人。

2 天:新增人数为 nr^2 人,已感染人数为 n+nr+nr^2 人。

x 天:新增人数为 nr^x 人,已感染人数为 n \times \sum_{i=0}^{x} r^i 人。

所以我们只需找到一个最小的 x ,使得 n \times \sum_{i=0}^{x} r^i 大于等于 p+1 即可。

代码

#include<bits/stdc++.h>
using namespace std;
int p,n,r,days,cnt;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>p>>n>>r;
    cnt=n;          //cnt 为已感染人数 
    while(cnt<=p){
        n*=r;       //更新新增感染人数 
        cnt+=n;     //更新已感染人数 
        days++;     //增加天数 
    }
    cout<<days;
    return 0;
}