HDU 4704 Sum

Sum

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


Problem Description
 

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.
 

Source
 

Recommend
zhuyuanchen520
 
题意: 略
思路: 求 2 ^(n - 1) % mod = ?
由费马小定理得,
2 ^ (n - 1) % p = 2 ^ [(n - 1) % (p - 1)] % p
(n - 1) % mod = (n % mod - 1 + mod) % mod;
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
const int V = 100000 + 50;
const int mod = 1000000000 + 7;
char ch[V], dh[V];
__int64 num[V], ans;
__int64 Quick_Pow(__int64 a, __int64 b) {
    __int64 res = 1;
    while(b) {
        if(b & 1)
            res = (res * a) % mod;
        b /= 2;
        a = (a * a) % mod;
    }
    return res % mod;
}
int main() {
    int i, j;
    num[0] = 1;
    for(i = 1; i < V; ++i)
        num[i] = (num[i - 1] % (mod - 1) * 10) % (mod - 1);
    while(~scanf("%s", &ch)) {
        int len = strlen(ch);
        for(i = 0; ch[i]; ++i)
            dh[len - i - 1] = ch[i];
        ans = 0;
        for(i = 0; i < len; ++i)
            ans = (ans + (dh[i] - '0') * num[i] % (mod - 1)) % (mod - 1);
        ans = (ans + mod - 2) % (mod - 1);
		printf("%I64d\n", Quick_Pow(2, ans));
    }
}



展开阅读全文

没有更多推荐了,返回首页