很有趣的一道题目呀,涨知识了。。。。。
答案=n*m^(k-1)-(m*(m+1))/2*m^(k-2)*(k-1)
推导过程:点击打开链接
#include<cstdio>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
LL n,m,p,k;
LL mull(LL x,LL y){
LL ans=1;
for(;y;y>>=1){
if(y&1)ans=ans*x%p;
x=x*x%p;
}
return ans;
}
int main(){
scanf("%lld%lld%lld%lld",&n,&k,&m,&p);n%=p;
LL ans=n*mull(m,k-1)%p-((m*(m+1))/2)%p*mull(m,k-2)%p*(k-1)%p;
while(ans<0)ans+=p;
printf("%lld",ans%p);
return 0;
}