CSU - 1556 Jerry's trouble(高速幂取模)

Posted llguanli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSU - 1556 Jerry's trouble(高速幂取模)相关的知识,希望对你有一定的参考价值。

题目链接】:click here

题目大意】:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < 1000

解题思路】:高速幂取模

代码:

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 two:

#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;
}



以上是关于CSU - 1556 Jerry&#39;s trouble(高速幂取模)的主要内容,如果未能解决你的问题,请参考以下文章

CodeTyphonGCC编译出现 undefined reference to `__libc_csu_init' 和 `__libc_csu_fini'的解决方法

(转)mysqldump: Got error: 1556: You can't use locks with log tables.

CSU 1333 & Uva 12661 Funny Car Racing最短路变形+spfa算法,链式前向星建图

mysqldump: Got error: 1556: You can't use locks with log tables. when using LOCK TABLES

java [Tom&Jerry-Dagger2] #tags:Android

Jerry's spark demo application