E - Critical Hit -- ATCODER
E - Critical Hit
https://atcoder.jp/contests/abc280/tasks/abc280_e
REFERENCE
https://blog.csdn.net/sophilex/article/details/128169335
dp[i]=(dp[i-2]+1)*p/100+(dp[i-1]+1)*(1-p/100)
https://atcoder.jp/contests/abc280/submissions/36968112
求逆模
https://blog.csdn.net/qq_41897386/article/details/82289975
逆元的定义见文章。
让除法运算改成乘法运算
费马小定理(Fermat's little theorem)
假如 p 是质数,那么 a ^ (p-1) ≡ 1 (mod p) 。
推论: b ^ (p - 2) % p 即为 b mod p 的乘法逆元。
费马小定理的证明
https://www.cnblogs.com/CYLSY/p/16818835.html
Code
https://atcoder.jp/contests/abc280/submissions/37047001
#define ll long long int n, p; const int mod = 998244353; ll in; map<ll, ll> cache; ll ksm(ll x,ll y) { ll ans=1; while(y) { if(y&1) ans=ans*x%mod; x=x*x%mod; y>>=1; } return ans; } ll inv(ll x) { return ksm(x,mod-2); } ll e(ll n){ if(cache[n] != 0){ return cache[n]; } if (n<=0){ return 0; } else if(n == 1){ return 1; } ll big = e(n-2); ll small = e(n-1); big *= p; small *= 100 - p; big %= mod; small %= mod; big *= in; small *= in; big %= mod; small %= mod; ll av = big + small + 1; av %= mod; cache[n] = av; return av; } int main() { cin >> n; cin >> p; in=inv(100); cout << e(n) << endl; return 0; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。