题目链接:https://nanti.jisuanke.com/t/31716
题意:队友读的题,题意可以转化为输入N,1≤N≤10^100000,求2^(N-1);
解析:套用指数降幂的模板即可,算法原理可以自行百度欧拉降幂公式。
具体可以参考:https://blog.csdn.net/acdreamers/article/details/8236942
https://blog.csdn.net/sdau20163942/article/details/83384083
代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1000005;
typedef long long LL;
const LL MOD=1e9+7;char str[N];LL euler_phi(LL n) //较快
{LL ans=n;for(LL i=2;i*i<=n;i++)if(n%i==0){ans-=ans/i;while(n%i==0)n/=i;}if(n>1)ans-=ans/n;;return ans;
}LL multi(LL a,LL b)
{LL ans = 0;a %= MOD;while(b){if(b & 1){ans = (ans + a) % MOD;b--;}b >>= 1;a = (a + a) % MOD;}return ans;
}LL pow_mod(LL n,LL k) //快速幂求n^k余m的结果
{LL res=1;n=n%MOD;while(k>0){if(k&1)res=res*n%MOD;n=n*n%MOD;k>>=1;}return res;
}void Solve(LL a,char str[],LL c)
{LL len = strlen(str);LL ans = 0;LL p = euler_phi(c);if(len <= 15){for(int i=0; i<len; i++)ans = ans * 10 + str[i] - '0';ans=ans-1;}else{for(int i=0; i<len; i++){ans = ans * 10 + str[i] - '0';ans %= p;}ans=(ans-1+MOD)%MOD;ans += p;}printf("%lld\n",pow_mod(a,ans));
}int main()
{int T;scanf("%d",&T);while(T--){scanf("%s",str);Solve(2ll,str,MOD);}return 0;
}
开始做题时是一冲动写的JAVA大数的快速幂,代码是超时的,这里记录一下:
import java.math.BigInteger;
import java.util.Scanner;public class Main {static BigInteger pow_mod(BigInteger n,BigInteger k,BigInteger m) //快速幂求(n^p)%m的值{BigInteger res=BigInteger.ONE;BigInteger tw=BigInteger.valueOf(2);while(!k.equals(BigInteger.ZERO)){if(k.mod(tw).equals(BigInteger.ONE))res=res.multiply(n).mod(m);n=n.multiply(n).mod(m);k=k.divide(tw);}return res;}public static void main(String[] args) {Scanner cin=new Scanner(System.in);int T;T=cin.nextInt();BigInteger MOD=BigInteger.valueOf(1000000007);while(T--!=0){BigInteger n=cin.nextBigInteger();n=n.subtract(BigInteger.ONE);System.out.println(pow_mod(BigInteger.valueOf(2),n,MOD));}cin.close();}
}