" topic link ": Click here
" main idea": Calculate x1^m+x2^m+. Xn^m (1<=x1<=n) (1 <= N < 1, 1 <= m < 1000)
" Thinking of Solving problems ": fast power-taking mode
Code:
Solution One:
#include <bits/stdc++.h>
#define LL Long Long
using namespace std;
Const LL mod= (LL) 1e9+7;
ll Pow_mod (ll a,ll p,ll N)
{
if (p==0) return 1;
LL Ans=pow_mod (a,p/2,n);
ans=ans*ans%n;
if (p&1) ans=ans*a%n;
return ans;
}
int n,m;
int main ()
{while
(scanf ("%d%d", &n,&m)!=eof)
{
LL s=0;
for (int i=1; i<=n; i++)
s= (S+pow_mod (i%mod,m,mod))%mod;
printf ("%lld\n", s);
}
return 0;
}
Solution:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm >
using namespace std;
typedef long long LL;
Const LL mod=1e9+7;
ll Pow_mod (ll A,ll b)
{
ll res=a,ans=1;
while (b)
{
if (b&1) ans= (Res*ans)%mod;
Res=res*res%mod;
b>>=1;
}
return ans;
}
int main ()
{
LL n,m;
while (~scanf ("%lld%lld", &n,&m))
{
LL s=0;
for (int i=1; i<=n; ++i)
s+=pow_mod (i%mod,m)%mod;
printf ("%lld\n", S%mod);
}
return 0;
}