题解:http://www.cnblogs.com/jianglangcaijin/archive/2013/08/13/3254314.html
先差分,每一个差分对答案的贡献
然后就很水啦
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(ll &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
ll n,m,K,P,Ans;
inline ll Pow(ll a,ll b){
ll ret=1;
for (;b;b>>=1,(a*=a)%=P)
if (b&1)
(ret*=a)%=P;
return ret;
}
int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(K); read(m); read(P);
Ans=m*(m+1)/2%P*Pow(m,K-2)%P*(K-1)%P;
Ans=((n%P*Pow(m,K-1)%P-Ans)%P+P)%P;
printf("%lld\n",Ans);
return 0;
}