NC25446. Kuangyeye's Resistance
描述
输入描述
The input only one test cases, which consists of three integers representing n, R, and P.
输出描述
Output the resistance value described above.
示例1
输入:
1 1 2
输出:
1
示例2
输入:
2 2 5
输出:
4
C++11(clang++ 3.9) 解法, 执行用时: 55ms, 内存消耗: 380K, 提交时间: 2019-06-01 15:00:09
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL n,p,R; LL qpow(LL a,LL x){ LL res = 1; while(x){ if(x&1) res = res * a % p; a = a * a % p; x >>= 1; } return res; } int main(){ cin >> n >> R >> p; LL pre = 1; for(int i = 2;i <= n; ++i){ pre += 2; pre = pre*qpow(pre+1,p-2)%p; } cout << pre * R % p; return 0; }
C++14(g++5.4) 解法, 执行用时: 69ms, 内存消耗: 480K, 提交时间: 2019-06-06 22:32:31
#include <bits/stdc++.h> #define ll long long using namespace std; ll n,r,p; ll quickpow(ll a,ll b){ ll r=1; a%=p; while(b){ if(b&1) r=(r*a)%p; a=(a*a)%p; b>>=1; } return r; } int main(){ cin>>n>>r>>p; ll ans=1; for(int i=1;i<n;i++){ ans=(ans+2)%p*quickpow(ans+3,p-2)%p; } ans=ans*r%p; cout<<ans<<endl; return 0; }