山东省第八届ACM省赛 G 题 sum of power 解答


sum of power

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

Calculate  mod (1000000000+7) for givennm.

Input

Input contains two integers n,m(1≤n≤1000,0≤m≤10).

Output

Output the answer in a single line.

Example Input
10 0
Example Output
10



题意

很明确,不必多说


思路

比如说,998^10 显然无法计算,所以不能用求出准确值的方法 然后进行取模,必须在过程中进行取模运算。显然,大数取模,想到快速幂(很重要,可以作为公式备注)。(注意,此题不是 多输入,白白WA了一次)。


AC代码: g++  0ms

#include<iostream>
#include<cstdio> //AC 0ms
#include<cstring>
#include<cmath>
const int  mod  = 1e9+7 ;
using namespace std;
int n,m;
long long sum;
long long pow_mod(int a,int n,int m)//快速幂,a为底数,n为幂次,m为mod数;
{
    if(n==0) return 1;
    long long x=pow_mod(a,n/2,m);
    long long ans=(long long)x*x%m;
    if(n%2==1) ans=ans*a%m;
    return ans;    //返回值是 余数
}
int main()
{
    scanf("%d%d",&n,&m);
    sum=0;
    for(int i=1;i<=n;i++){
        sum+=pow_mod(i,m,mod);  //循环累加过程
        sum=sum%mod;
    }
    cout<<sum<<endl;    //用cout输出,避免 lld 或者I64d 的编译区分
    return 0;
}

stut 3899


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值