HDU 4704 Sum (2013多校10,1009题)

 

Sum

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

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
2013 Multi-University Training Contest 10  

 

Recommend zhuyuanchen520  

 

 

 

其实这题就是求2^(n-1) % MOD;

可先对(n-1)%(MOD-1)

 

这样就很容易求了

 

 1 /* ***********************************************
 2 Author        :kuangbin
 3 Created Time  :2013/8/22 12:00:50
 4 File Name     :F:\2013ACM练习\2013多校10\1009.cpp
 5 ************************************************ */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 #include <queue>
13 #include <set>
14 #include <map>
15 #include <string>
16 #include <math.h>
17 #include <stdlib.h>
18 #include <time.h>
19 using namespace std;
20 
21 const int MOD = 1e9+7;
22 long long n;
23 long long solve()
24 {
25     long long ret = 1;
26     long long tmp = 2;
27     while( n > 0)
28     {
29         if(n%2)
30         {
31             ret *= tmp;
32             ret %= MOD;
33         }
34         tmp *= tmp;
35         tmp %= MOD;
36         n = n/2;
37     }
38     return ret;
39 }
40 char str[5000000];
41 int main()
42 {
43     //freopen("in.txt","r",stdin);
44     //freopen("out.txt","w",stdout);
45     while(scanf("%s",str) == 1)
46     {
47         n = 0;
48         int len = strlen(str);
49         for(int i = 0;i < len;i++)
50         {
51             n = (n*10 + str[i] -'0')%(MOD-1);
52         }
53         n = (n-1+MOD-1)%(MOD-1);
54         printf("%d\n",(int)solve());
55     }
56     return 0;
57 }

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3275593.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞