鏈接:https://ac.nowcoder.com/acm/contest/330/E
來源:牛客網
題目描述
精通程序設計的 Applese 叕寫了一個遊戲。
在這個遊戲中,有一個 n 行 m 列的方陣。現在它要爲這個方陣塗上黑白兩種顏色。規定左右相鄰兩格的顏色不能相同。請你幫它統計一下有多少種塗色的方法。由於答案很大,你需要將答案對 109+7 取模。
輸入描述:
僅一行兩個正整數 n, m,表示方陣的大小。
輸出描述:
輸出一個正整數,表示方案數對 109+7 取模。
示例1
輸入
1 1
輸出
2
示例2
輸入
2 2
輸出
4
備註:
1≤n,m≤10100000
因爲每行只有兩種塗色可能,所以共有2的n次方種塗色方法
只記得用快速冪忘了用費馬小定理了otz
費馬小定理:a^x%p=a^(x%(p-1))%p.
所以先用費馬小定理降冪再用快速冪纔不會超時orz因爲給出的數據範圍是大數所以用了java(……)
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static long testPosition(Long a,BigInteger b) {
long mod=1000000007;
long s = 1;
BigInteger r=new BigInteger("0");
r=BigInteger.valueOf(2);
BigInteger h=new BigInteger("0");
while (b.compareTo(BigInteger.valueOf(0))>0) {
h=b.mod(r);
if (h.equals(BigInteger.valueOf(1))) {
s = s % mod;
a = a % mod;
s = s * a;
}
a = a % mod;
a = a * a;
b=b.divide(BigInteger.valueOf(2));
}
return s % mod;
}
public static void main(String[] args) {
long mod=1000000007;
BigInteger n=new BigInteger("0");
BigInteger m=new BigInteger("0");
Scanner cin=new Scanner(System.in);
n=cin.nextBigInteger();
n=n.mod(BigInteger.valueOf(mod-1));
long g=2;
System.out.println(testPosition(g,n));
}
}