山东省第八届ACM省赛 G 题 sum of power 解答

来源:互联网 发布:c语言之父 编辑:程序博客网 时间:2024/06/12 00:37


sum of power

Time Limit: 1000MSMemory Limit: 65536KB

Problem Description

Calculate  mod (1000000000+7) for givennm.

Input

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

Output

Output the answer in a single line.

Example Input

10 0

Example Output

10



题意

很明确,不必多说


思路

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


AC代码: g++  0ms

#include<iostream>#include<cstdio> //AC 0ms#include<cstring>#include<cmath>const int  mod  = 1e9+7 ;using namespace std;int n,m;long long sum;long long pow_mod(int a,int n,int m)//快速幂,a为底数,n为幂次,m为mod数;{    if(n==0) return 1;    long long x=pow_mod(a,n/2,m);    long long ans=(long long)x*x%m;    if(n%2==1) ans=ans*a%m;    return ans;    //返回值是 余数}int main(){    scanf("%d%d",&n,&m);    sum=0;    for(int i=1;i<=n;i++){        sum+=pow_mod(i,m,mod);  //循环累加过程        sum=sum%mod;    }    cout<<sum<<endl;    //用cout输出,避免 lld 或者I64d 的编译区分    return 0;}

stut 3899


阅读全文
1 0
原创粉丝点击