HDU--4704

题目:

Sum

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4704

分析:实际上就是求2^(n-1)mod(1e9+7).因为2与1e9+7互素,所以可以用费马小定理。

参考:http://baike.baidu.com/view/263807.htm

(a,p)=1,则a^(p-1)Ξ1(modp).

可得:2^(n-1)%mod=2^((n-1)%(mod-1)) %mod;

证明:(n-1)=(n-1)%mod+k*(mod-1).

则:2^(n-1)=2^((n-1)%(mod-1))  *  2^(k*(mod-1));

可知结论成立。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 #define maxn 100005
 8 #define LL __int64
 9 #define mod 1000000007
10 char s[maxn];
11 LL pow_mod(LL a,LL b)
12 {
13     if(b==0)return 1;
14     LL temp=pow_mod(a,b>>1);
15     temp=temp*temp%mod;
16     if(b&1)temp=temp*a%mod;
17     return temp;
18 }
19 int main()
20 {
21     while(scanf("%s",s)!=EOF)
22     {
23         LL n=0;
24         int len=strlen(s);
25         for(int i=0;i<len;i++)
26         {
27             n=(n*10+s[i]-'0')%(mod-1);
28         }
29         n=(n-1+mod-1)%(mod-1);
30         LL ans=pow_mod(2,n);
31         printf("%I64d\n",ans);
32     }
33     return 0;
34 }
View Code

 

posted @ 2013-09-15 21:57  EtheGreat  阅读(188)  评论(0编辑  收藏  举报