Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 647 Accepted Submission(s): 320
Problem Description
Sample Input
2
Sample Output
2
Hint
1. For N = 2, S(1) = S(2) = 1.
2. The input file consists of multiple test cases.
Source
思路:一道整数划分题目,不难推出公式:2^(n-1),
根据费马小定理:(2,MOD)互质,则2^(p-1)%p=1,于是我们可以转化为:2^(n-1)%MOD=2^((n-1)%(MOD-1))%MOD,从而用快速幂求解。
公式2^(n-1) % MOD;
可先对(n-1)%(MOD-1)
import java.io.*; import java.util.*; import java.math.*; public class Main { BigInteger n; String s=""; BigInteger one=BigInteger.valueOf(1); BigInteger Mod=BigInteger.valueOf((long)(1e9+7)); BigInteger Mod1=BigInteger.valueOf((long)(1e9+6)); public static void main(String[] args) { new Main().work(); } void work(){ Scanner sc=new Scanner(new BufferedInputStream(System.in)); while(sc.hasNext()){ s=sc.next(); n=BigInteger.valueOf(0); for(int i=0;i<s.length();i++){ n=(n.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(s.charAt(i)-'0'))).mod(Mod1); } long num=n.longValue()-1; System.out.println(pow(BigInteger.valueOf(2),num).mod(Mod)); } } BigInteger pow(BigInteger a,long b){ BigInteger sum=BigInteger.ONE; while(b!=00){ if((b&1)!=0){ sum=sum.multiply(a).mod(Mod); } a=a.multiply(a).mod(Mod); b>>=1; } return sum; } }