Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 127    Accepted Submission(s): 60

Problem Description
 

 

Sample Input
2
 

 

Sample Output
2

 

解题思路:

 很容易看得出,这是个组合数学的插板问题,答案为2^(n-1);

由于n特别大,则(2^(n-1))%1000000007=(2^((n-1)%1000000006))%1000000007;

因为1000000007为素数,设为p,有2^p=2(%p),即2^(p-1)=1(%p);

用个大数对小数取模,和快速幂就好了

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 #include <queue>
 7 #define ll long long int
 8 #define mod 1000000007
 9 #define INF 1000000006
10 using namespace std;
11 char a[100010];
12 int b[100010];
13 int t;
14 ll fun()
15 {
16     int i,j;
17     ll sum=0;
18     for(i=t;i>=0;i--)
19     {
20         sum=sum*10+b[i];
21         sum%=INF;
22     }
23     return sum;
24 }
25 ll funa(ll sum)
26 {
27     if(sum==0)
28     return 1;
29     if(sum==1)
30     return 2;
31     if(sum&1)
32     {
33         ll temp=funa((sum-1)/2);
34         return temp*temp%mod*2%mod;
35     }
36     else
37     {
38         ll temp=funa(sum/2);
39         return temp*temp%mod;
40     }
41 }
42 int main()
43 {
44     while(scanf("%s",a)!=EOF)
45     {
46         t=strlen(a);
47         int i,j;
48         j=0;
49         memset(b,0,sizeof(b));
50         for(i=t-1;i>=0;i--)
51         {
52             b[j++]=a[i]-'0';
53         }
54         for(i=0;i<j;i++)
55         if(b[i]>0) {b[i]--;break;}
56         else b[i]=9;
57         ll res=fun();
58         cout<<funa(res)<<endl;
59     }
60 }
View Code

 

 

 

 

posted on 2013-08-22 19:03  ERKE  阅读(396)  评论(0编辑  收藏  举报