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);
用个大数对小数取模,和快速幂就好了
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <queue>
#define ll long long int
#define mod 1000000007
#define INF 1000000006
using namespace std;
char a[];
int b[];
int t;
ll fun()
{
int i,j;
ll sum=;
for(i=t;i>=;i--)
{
sum=sum*+b[i];
sum%=INF;
}
return sum;
}
ll funa(ll sum)
{
if(sum==)
return ;
if(sum==)
return ;
if(sum&)
{
ll temp=funa((sum-)/);
return temp*temp%mod*%mod;
}
else
{
ll temp=funa(sum/);
return temp*temp%mod;
}
}
int main()
{
while(scanf("%s",a)!=EOF)
{
t=strlen(a);
int i,j;
j=;
memset(b,,sizeof(b));
for(i=t-;i>=;i--)
{
b[j++]=a[i]-'';
}
for(i=;i<j;i++)
if(b[i]>) {b[i]--;break;}
else b[i]=;
ll res=fun();
cout<<funa(res)<<endl;
}
}