题干:
精通程序设计的 Applese 叕写了一个游戏。
在这个游戏中,有一个 n 行 m 列的方阵。现在它要为这个方阵涂上黑白两种颜色。规定左右相邻两格的颜色不能相同。请你帮它统计一下有多少种涂色的方法。由于答案很大,你需要将答案对 109+7109+7 取模。
输入描述:
仅一行两个正整数 n, m,表示方阵的大小。
输出描述:
输出一个正整数,表示方案数对 109+7109+7 取模。
示例1
输入
1 1
输出
2
示例2
输入
2 2
输出
4
备注:
1≤n,m≤10^100000
解题报告:
这题解法很多,FZU - 1759类似的原题。
AC代码:(phthon版)
def qpow(a,b,c):
a=a%c
ans=1
while b!=0:
if b&1:
ans=(ans*a)%c
b>>=1
a=(a*a)%c
return ans
x = list(map(int, input().strip().split()))
print(qpow(2,x[0]%(1000000006),1000000007))
AC代码2:(phthon版)
print(pow(2,int(input().split()[0]),10**9+7))
所以pow函数内部是跨粟米实现的???不明白23333
AC代码3:(费马小定理 + 指数循环节)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const ll mod=1000000007;
char s[MAX],s1[MAX];
ll qpow(ll a,ll k) {
ll res = 1;
while(k) {
if(k&1) res=(res*a)%mod;
a=a*a%mod;
k>>=1;
}
return res;
}
ll euler(int n) {
int res=n,a=n;
for(int i=2; i*i<=a; i++) {
if(a%i==0) {
res=res/i*(i-1);
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
int main() {
int n,m;
ll c = euler(mod);//1e9+6
ll b=0;
scanf("%s",s);
scanf("%s",s1);
int len=strlen(s);
// for(int i=len-1; i>=0; i--) {
// if(s[i]=='0') s[i]='9';
// else {
// s[i]=s[i]-1;break;
// }
// }
for(int i=0; i<len; i++) {
b=b*10+s[i]-'0';
b=b%c;
}
cout << qpow(2,b+c) <<endl;
return 0;
}
注释掉的那一部分可以当成一个小板子、、目的在于给一个字符串存下的整数执行 -1 操作。