次方求和

博客探讨了如何求解1^k到n^k的和,对于不同的k值提供了分类处理的方法。当k等于1时,利用公式(1+n)*n/2可以得出结果;k等于2时,给出了相应思路;而对于k等于3的情况,给出了1^3+2^3+3^3+...+n^3等于(1+2+3+...+n)^2的结论。

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

题意:

给定n和k,求 1^k+2^k+3^k+...+(n−1)^k+n^k

n(1e9),k(<=3)

 思路:分类k;

 

总结:公式!

k=1:(1+n)*(n)/2

k=2: 

k=3:1^3+2^3+3^3+.+n^3=(1+2+3+.+n)^2

#include<bits/stdc++.h>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<map>
#include<stack>
#include<string>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI acos(-1)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
ll gcd(ll a,ll b){while(b^=a^=b^=a%=b);return a;}
const int maxn=3e6+9;
const int mod=998244353;

template <class T>
inline void sc(T &ret)
{
    char c;
    ret = 0;
    while ((c = getchar()) < '0' || c > '9');
    while (c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0'), c = getchar();
    }
}

ll pow_mod(ll a, ll b, ll p){//a的b次方求余p
    ll ret = 1;
    while(b){
        if(b & 1) ret = (ret * a) % p;
        a = (a * a) % p;
        b >>= 1;
    }
    return ret;
}
ll Fermat(ll a, ll p){//费马求a关于b的逆元
        return pow_mod(a, p-2, p);
}

int main()
{
    ll n,k;
    while(cin>>n>>k)
    {
        n%=mod;
        if(k==0)
        {
            cout<<n<<endl;
        }
        if(k==1)
        {
            ll ans=(1+n)*(n)/2;
            cout<<ans%mod<<endl;
        }
        if(k==2)
        {
            ll ans=n*(n+1);
            ans%=mod;
            ans=ans*(2*n+1);
            ans%=mod;
            ans=ans*166374059;//6关于mod的逆元
            cout<<ans%mod<<endl;
        }
        if(k==3)
        {
            ll ans=(1+n)*(n)/2;
            ans%=mod;
            ans=ans*ans%mod;
            cout<<ans<<endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值