山东省第八届 ACM 省赛 G sum of power 【快速幂】

本文介绍了一种使用快速幂算法解决特定数学问题的方法——计算给定范围内所有整数的m次方之和并对1e9+7取模。通过C++实现的代码示例展示了如何高效地完成这一任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2015: G sum of power

时间限制: 1 Sec  内存限制: 128 MB
提交: 13  解决: 5
[ 提交][ 状态][ 讨论版]

题目描述

Calculate   mod (1e9+7) for givennm.


输入

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

输出

Output the answer in a single line.

样例输入

10 0

样例输出

10

这题就是求个和---

直接套快速幂模板就能过。数据类型全用longlong。

上代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;

const LL mod=1e9+7;

LL qpow(LL a,LL b)///快速幂------
{
    LL ans=1;

    if(a==0)
    {
        return 0;
    }
    else
    {
        while(b)
        {
            if(b&1)ans=(a%mod)*(ans%mod);
            b>>=1;
            a=(a%mod)*(a%mod);
        }
    }

    return ans;
}


int main()
{
    LL n,m;///数据类型全用long long
    while(~scanf("%lld %lld",&n,&m))
    {
        LL sum=0;
        for(int i=1;i<=n;i++)
        {

            sum=(sum+qpow(i,m))%mod;
        }
        printf("%lld\n",sum);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值