比赛 2026.5.30 评测结果 AAWWTTWWWW
题目名称 数列求和 最终得分 20
用户昵称 汐汐很希希 运行时间 11.233 s
代码语言 C++ 内存使用 3.65 MiB
提交时间 2026-05-30 10:12:16
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10;
ll n,a,k,p,ans=0;
ll fpow(ll x,ll n){
    if(n==0) return 1;
    else if(n==1) return x%p;
    else{
        ll mid=n/2;
        ll t=fpow(x,mid)%p;
        if(n%2==0) return t%p*t%p;
        else return t%p*t%p*x%p;
    }
}
int main()
{
    freopen("oeis.in","r",stdin);
    freopen("oeis.out","w",stdout);
    
    cin>>n>>a>>k>>p;
    if(n<=1e6){
        ll po=a;
        for(int i=1;i<=n;i++){
            ll t=fpow(i,k)%p;
            ans=(ans+t%p*po%p)%p;
            po*=a;
            po%=p;
        }
    }else if(k==0){
        ans=a*(1-fpow(a,n))/(1-a)%p;
    }else if(a==1){
        for(int i=1;i<=n;i++){
            ans=(ans+fpow(i,k))%p;
        }
    }
    cout<<ans<<endl;
    return 0;
}