zcmu-2007: sum of power(快速幂)

2007: sum of power

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 62  Solved: 28
[Submit][Status][Web Board]

Description

Calculate     mod (1000000000+7) for given n,m.

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

【分析】取余运算,(a*b)%mod=(a%mod*b%mod)%mod;

不定义函数:

#include<bits/stdc++.h> 
using namespace std;
const int maxn=1000000007;
int main()
{
   int n,m;
   long long sum=0,cnt=1;
   scanf("%d%d",&n,&m);
   for(int i=1;i<=n;i++)
   {
   		for(int j=1;j<=m;j++)
   			cnt=(i%maxn*cnt)%maxn;//,cout<<cnt<<endl;
   	//	cout<<"i^m="<<cnt<<endl;
		sum+=cnt,sum=sum%maxn;cnt=1;		   
   }
   cout<<sum<<endl;
    return 0;
}

定义函数:

#include<bits/stdc++.h> 
using namespace std;
const int maxn=1000000007;
long long pow(int a,int b)
{
	long long ans=1;
	for(int i=0;i<b;i++)
	{
		ans*=a;
		ans=ans%maxn;
	}
	return ans;
}
int main()
{
   int n,m;
   long long sum=0;
   scanf("%d%d",&n,&m);
   for(int i=1;i<=n;i++)
   {
		sum+=pow(i,m);
		sum=sum%maxn;	   
   }
   cout<<sum<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/81626283
sum