标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1270 Accepted Submission(s): 512
#include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include <algorithm> #include<vector> typedef long long LL; LL mod; LL pow_mod(LL a,LL n){ LL ans = 1; while(n){ if(n&1) ans=ans*a%mod; a= a*a%mod; n>>=1; } return ans; } LL cal(LL p,LL n){ ///这里是递归求解等比数列模板 1+p+p^2...+p^n if(n==0) return 1; if(n&1){///(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1)); return (1+pow_mod(p,n/2+1))*cal(p,n/2)%mod; } else { ///(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2); return (pow_mod(p,n/2)+(1+pow_mod(p,n/2+1))*cal(p,n/2-1))%mod; } } int main() { LL n; while(scanf("%lld%lld",&n,&mod)!=EOF) { if(n==1&&mod==1) { printf("0\n"); continue; } LL k = (n+1)/2; LL ans = cal(4,k-1); if(n&1){ printf("%lld\n",ans); }else { printf("%lld\n",ans*2%mod); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5632477.html