HDU 4990 Reading comprehension
快速幂
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; long long n,MOD; long long cal(long long a,long long b,long long mod) { long long c=1; while(b!=0) { if(b%2==1) c=(c*a)%mod,b--; else a=(a*a)%mod,b=b/2; } return c; } int main() { while(cin>>n>>MOD) { if(n%2==1) cout<<(cal(2,n+1,MOD*3)-1)/3<<endl; else cout<<(cal(2,n+1,MOD*3)-2)/3<<endl; } return 0; }