Live2d Test Env

BZOJ- 3142:数列 (数学)

题意:给出N,K,M,P。求有多少长度为K的序列A,满足:(1)首项为正整数;(2)递增数列;(3)相邻两项的差小于等于m;(4)最后一个数小于等于N。

思路:根据差分来算数量。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll N,M,K,P,ans,ans2;
ll qpow(ll a,ll x){
    ll res=1;while(x){
        if(x&1) res=(ll)res*a%P;
        a=(ll)a*a%P; x>>=1;
    }return res;
}
int main()
{
    scanf("%lld%lld%lld%lld",&N,&K,&M,&P); N%=P;
    ans=(ll)N*qpow(M,K-1)%P;
    ll a=M,b=M+1;
    if(a&1) b/=2; else a/=2;
    ans2=(ll)a*b%P;
    ans2=(ll)ans2*qpow(M,K-2)%P*(K-1)%P;
    ans=(((ans-ans2)%P)+P)%P;
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2018-09-19 16:43  nimphy  阅读(153)  评论(0编辑  收藏  举报