当前位置: 代码迷 >> 综合 >> (同余与模算术)sum of power
  详细解决方案

(同余与模算术)sum of power

热度:59   发布时间:2023-11-02 23:06:45.0

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<iostream>
#include<cmath>
using namespace std;
#define MOD 1000000000+7int 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<<sum<<endl;return 0;
}



  相关解决方案