CSU - 1556 Jerry's trouble(快速幂)

Time Limit: 10000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu

Status

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

题意:给出两个数,n 和 m, 求 1~n 之间每个数的 m 次幂的和,再取模

思路:快速幂


<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

#define ll long long
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const ll MOD = 1e9+7;

ll pow_mod(ll n, ll m) {
    ll res = 1;
    while(m) {
        if(m&1)
            res = res*n%MOD;
        n = n*n%MOD;
        m >>= 1;
    }
    return res;
}
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ll n, m;
    ll ans;
    while(cin>>n>>m) {
        ans = 0;
        for(ll i = 1; i <= n; i++) {
            ans += pow_mod(i, m);
            ans %= MOD;
        }
        cout<<ans<<endl;
    }
    return 0;
}

</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值