HDU-4707 Sum

来源:互联网 发布:国外数据库网站 编辑:程序博客网 时间:2024/05/14 07:28

Input
2
Output
2         
Hint
1. For N = 2, S(1) = S(2) = 1.2. The input file consists of multiple test cases.
Sample Input
2
Sample Output
2          
Hint
1.  For N = 2, S(1) = S(2) = 1.2.  The input file consists of multiple test cases. 


嗯,利用隔板原理,可以转换题意为求2的n次方,因为n比较大,所以可以用费马小定理的降幂公式

那个函数是欧拉函数,表示不大于n的与n互素的个数。

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 1e5+7;const LL mod = 1e9+7;char num[MAXN];LL quick_mod(LL a,LL b){    LL sum = 1,base = a;    while(b)    {        if(b&1)sum = sum*base%mod;        base = base*base%mod;        b >>= 1;    }    return sum;}int main(){    while(~scanf("%s",num))    {        LL b = 0;        int l = strlen(num);        LL phi = mod - 1;        for(int i = 0; i < l; ++i)b = (b*10+num[i]-'0')%phi;        b = (b-1+phi)%phi;        printf("%I64d\n",quick_mod(2,b+phi));    }    return 0;}