BZOJ3142 HNOI2013数列(组合数学)

  考虑差分序列。每个差分序列的贡献是n-差分序列的和,即枚举首项。将式子拆开即可得到n*mk-1-Σi*cnt(i),cnt(i)为i在所有差分序列中的出现次数之和。显然每一个数出现次数是相同的,所以cnt(i)即等于(k-1)*mk-2。于是就很好算了。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
int m,k,p;ll n;
ll ksm(ll a,ll k)
{
    if (k==0) return 1;
    ll tmp=ksm(a,k>>1);
    if (k&1) return tmp*tmp%p*a%p;
    else return tmp*tmp%p;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj3142.in","r",stdin);
    freopen("bzoj3142.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    cin>>n>>k>>m>>p;k--;
    cout<<(n%p*ksm(m,k)%p-(k?1ll*m*(m+1)/2%p*ksm(m,k-1)%p*k%p:0)+p)%p;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Gloid/p/9664935.html