G - sum of power 大数处理

大数取模算法实现
本文介绍了一种解决大数取模问题的算法实现方法,通过重写pow函数以快速计算从1到n的m次幂之和并取模10e9+7。文章提供了一个具体的C++代码示例,展示了如何在计算过程中逐步取模以避免溢出。

 

G - sum of power

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

use MathJax to parse formulas

Description

Calculate  mod (1000000000+7) for given nm.

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

题意:给你n和m,把从1到n的m次幂相加,mod 10e9+7;

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

G题代码

#include <iostream>
#include <cmath>
#include <cstdio>
//大数处理 
using namespace std;
const  long long P=1000000007;

//重写pow函数 
long long pow(int a,int n){
	long long ans=1;
	for(int i=0;i<n;i++){
	ans*=a;
	ans=ans%P;	//数字太大多次取余  
	}
	return ans;
}

int main()
{
    int n,m;
    long long sum=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        sum+=pow(i,m);
        sum=sum%P;
    }
    cout<<sum<<endl;

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值