CSU 1556 Jerry's trouble

题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1556

Description

 Jerry is caught by Tom. He was penned up in one room with a door, which only can be opened by its code. The code is the answer of the sum of the sequence of number written on the door. The type of the sequence of number is

But Jerry’s mathematics is poor, help him to escape from the room.

Input

 There are some cases (about 500). For each case, there are two integer numbers n, m describe as above ( 1 <= n < 1 000 000, 1 <= m < 1000).

Output

 For each case, you program will output the answer of the sum of the sequence of number (mod 1e9+7).

Sample Input

4 1
5 1
4 2
5 2
4 3

Sample Output

10
15
30
55
100

Hint

Source

题意:

  看sample猜公式。QAQ

题解:

  公式是 1^m + 2^m + ···· + n^m。用快速幂解决。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define ms(a, b)  memset((a), (b), sizeof(a))
16 //#define LOCAL
17 #define eps 0.0000001
18 #define LNF (1<<60)
19 typedef long long LL;
20 const int inf = 0x3f3f3f3f;
21 const int maxn = 100000+10;
22 const int mod = 1e9+7;
23 
24 LL qpow(LL a, LL b)
25 {
26     LL ans = 1;
27     LL base = a;
28     while(b){
29         if(b&1)     ans = (ans * base)%mod;
30         b >>= 1;
31         base = (base * base)%mod;
32     }
33     return ans;
34 }
35 int main()
36 {
37 #ifdef LOCAL
38     freopen("input.txt", "r", stdin);
39 //      freopen("output.txt", "w", stdout);
40 #endif // LOCAL
41 
42     int n,m;
43     while(~scanf("%d%d", &n, &m))
44     {
45         LL ans = 0;
46         for(int i = 1;i<=n;i++)
47         {
48             ans = (ans + qpow(i, m))%mod;
49         }
50         printf("%lld
", ans);
51     }
52     return 0;
53 }
View Code
原文地址:https://www.cnblogs.com/denghaiquan/p/6745244.html