bzoj 3142 //3142: [Hnoi2013]数列 //在线测评地址https://www.lydsy.com/JudgeOnline/problem.php?id=3142
//在线测评地址https://www.luogu.org/problem/P3228
更多题解,详见https://blog.csdn.net/mrcrack/article/details/90228694BZOJ刷题记录
Accepted | 820 kb | 48 ms | C++/Edit | 439 B |
//3142: [Hnoi2013]数列
//在线测评地址https://www.lydsy.com/JudgeOnline/problem.php?id=3142
//通过样例,看懂题意,但却没什么感觉,解题思路在哪里。2019-11-7 21:21
//感觉是 组合数C(7,3)-差值超过M的情况,要用到容斥原理吗。2019-11-7 21:30
//此文https://www.cnblogs.com/ljh2000-jump/p/5907411.html当棒,摘抄如下
/*
*/
//此文http://m-sea-blog.com/archives/2231/公式证明写得不错,摘抄如下
笔者注释:2019-11-9 10:21
//此文https://www.cnblogs.com/jianglangcaijin/archive/2013/08/13/3254314.html公式解释得不错,摘抄如下
思路:设相邻两项的差值为a[i]=A[i+1]-A[i],那么每一个这样的序列对答案的贡献为:2019-11-8 22:06
//样例通过,提交Wrong_Answer 820 kb 8 ms C++/Edit 342 B
//printf("%lld\n",((n%p*qpow(m,k-1)%p-m*(m+1)/2%p*(k-1)%p*qpow(m,k-2)%p)+p)%p);//此处错写成printf("%lld\n",n%p*qpow(m,k-1)%p-m*(m+1)/2%p*(k-1)%p*qpow(m,k-2)%p);
//开始时,确实想过,运算结果可能为负,无奈编写过程中,忘记了,还是能力不足,
//样例通过,提交AC.2019-11-8 20:54
#include
#define LL long long
LL n,k,m,p;
LL qpow(LL a,LL b){
LL ret=1;
while(b){
if(b&1)ret=ret*a%p;
a=a*a%p;
b>>=1;
}
return ret;
}
int main(){
scanf("%lld%lld%lld%lld",&n,&k,&m,&p);
printf("%lld\n",((n%p*qpow(m,k-1)%p-m*(m+1)/2%p*(k-1)%p*qpow(m,k-2)%p)+p)%p);//此处错写成printf("%lld\n",n%p*qpow(m,k-1)%p-m*(m+1)/2%p*(k-1)%p*qpow(m,k-2)%p);
return 0;
}