G - sum of power 大数处理

G - sum of power

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

use MathJax to parse formulas

Description

Calculate  mod (1000000000+7) for given nm.

Input

Input contains two integers n,m(1≤n≤1000,0≤m≤10).

Output

Output the answer in a single line.

Sample Input

10 0

Sample Output

10

题意:给你n和m,把从1到n的m次幂相加,mod 10e9+7;

比如说,998^10 显然无法计算,所以不能用求出准确值的方法 然后进行取模,必须在过程中进行取模运算。显然,大数取模,想到快速幂(很重要,可以作为公式备注)。(注意,此题不是 多输入)。

G题代码

#include <iostream>
#include <cmath>
#include <cstdio>
//大数处理 
using namespace std;
const  long long P=1000000007;

//重写pow函数 
long long pow(int a,int n){
	long long ans=1;
	for(int i=0;i<n;i++){
	ans*=a;
	ans=ans%P;	//数字太大多次取余  
	}
	return ans;
}

int main()
{
    int n,m;
    long long sum=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        sum+=pow(i,m);
        sum=sum%P;
    }
    cout<<sum<<endl;

    return 0;
}
发布了94 篇原创文章 · 获赞 34 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/dujuancao11/article/details/80027011