牛客寒假算法基础集训营4 E applese 涂颜色

链接:https://ac.nowcoder.com/acm/contest/330/E


精通程序设计的 Applese 叕写了一个游戏。


在这个游戏中,有一个 n 行 m 列的方阵。现在它要为这个方阵涂上黑白两种颜色。规定左右相邻两格的颜色不能相同。请你帮它统计一下有多少种涂色的方法。由于答案很大,你需要将答案对 109+7

由于左右不蹦相邻 ,所以第一列固定 就固定了。 答案2^n

但是n特别大,用到了费马小定理

如果 p为质数 an%mod = a  n%(p-1)%mod

然后 快速幂即可

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int mod=1e9+7;
 4 typedef long long ll;
 5 char num1[1000010],num2[1000010];
 6  
 7 ll m_pow(ll a,ll b) {
 8     ll tmp=a%mod;
 9     ll sum=1;
10     while(b) {
11         if(b&1) sum=sum*tmp%mod;
12         tmp=tmp*tmp%mod;
13         b>>=1;
14     }
15     return sum;
16 }
17 int main() {
18  
19        scanf("%s%s", num1,num2); 
20         
21            int len = strlen(num1); 
22            ll ans = 0; 
23            for(int i = 0; i < len; ++i) 
24            { 
25                ans = (ans*10 + (num1[i]-'0'))%(mod-1); 
26            }   
27        printf("%lld",m_pow(2,ans));
28  
29  
30 }

猜你喜欢

转载自www.cnblogs.com/ACMerszl/p/10335989.html