百度空间发公式太累,给个比较详细的解题链接吧
http://www.cnblogs.com/jianglangcaijin/archive/2013/08/13/3254314.html
注意M(k-1)<N是一个很重要的条件
1 var n,m,ans,s:int64; 2 p,k:longint; 3 4 function quick(x:int64; y:longint):int64; 5 begin 6 quick:=1; 7 while y>0 do 8 begin 9 if y mod 2=1 then quick:=quick*x mod p; 10 y:=y div 2; 11 x:=x*x mod p; 12 end; 13 end; 14 15 begin 16 readln(n,k,m,p); 17 n:=n mod p; 18 s:=quick(m mod p,k-2); 19 ans:=n*s mod p*m mod p-(m+1)*m div 2 mod p*s mod p*int64(k-1) mod p; 20 writeln((ans+p) mod p); 21 end.