(同余与模算术)sum of power

2019-04-13 20:34发布

Problem 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

Hint

Source

“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)这个题,关键是同余与模算术降低数据规模,如果直接用pow的话,数据范围会超。同余与模算术:(a+b)%n=(a%n+b%n); (a-b)%n=(a%n-b%n+n)%n; ab%n=(a%n)(b%n)%n.#include #include using namespace std; #define MOD 1000000000+7 int main() { int n,m; cin>>n>>m; long long sum=0,res=0; for(int i=1;i<=n;i++){ res=1; for(int j=1;j<=m;j++){ res*=i,res%=MOD; } sum+=res; sum%=MOD; } cout<