51 NOD 1013 3的幂的和
做法:快速幂+求逆元取模
因为ans=((3^(n+2))/2)%P
而ans%P/2!=ans/2%P
所以由费马小定理当gcd(a,p)==1&&P为质数时,a^(p-1)≡1(mod p)可得:ans*(p+1)/2≡ans/2 (%p)
然后就可以美滋滋地对ans取模辣
Code:
1 #include <cstdio> 2 inline int read() 3 { 4 register int f=1,k=0;register char c=getchar(); 5 while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 6 while (c>='0'&&c<='9')k=k*10+c-'0',c=getchar(); 7 return k*f; 8 } 9 const long long MOD=1000000007; 10 int main() 11 { 12 register int n=read()+1;register long long t=3,ans=1; 13 while (n) 14 { 15 if(n&1)ans=ans*t%MOD; 16 t=t*t%MOD; 17 n>>=1; 18 } 19 printf("%lld\n",ans); 20 ans-=1; 21 printf("%lld\n",ans*500000004%MOD); 22 }