BZOJ3142 [Hnoi2013]数列 【组合数学】

题目链接

BZOJ3142

题解

题意:选一个正整数和\(K - 1\)\([1,M]\)中的数,使得总和小于等于\(N\),求方案数模\(P\)

题目中\(K(M - 1) < N\)的限制意味着,除了第一个数外,别的数可以随便选,然后第一个数就限制在\(N - \sum a_i\)之间
所以方案数为
\[\sum\limits_{a_1 = 1}^{M} \sum\limits_{a_2 = 1}^{M} \sum\limits_{a_3 = 1}^{M} \dots \sum\limits_{a_{K - 1} = 1}^{M} (N - \sum\limits_{i = 1}^{K - 1}a_i)\]
展开化简得
\[NM^{K - 1} - (K - 1)M^{K - 2}\]

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u]; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 0x3f3f3f3f;
const double eps = 1e-9;
inline LL read(){
    LL out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
    return flag ? out : -out;
}
LL N,M,K,P;
inline LL qpow(LL a,LL b){
    LL re = 1;
    for (; b; b >>= 1,a = a * a % P)
        if (b & 1) re = re * a % P;
    return re;
}
int main(){
    N = read(); K = read(); M = read(); P = read();
    if (K == 1){printf("%lld\n",N); return 0;}
    printf("%lld\n",((N % P * qpow(M,K - 1) % P - (M + 1) * M / 2 % P * (K - 1) % P * qpow(M,K - 2) % P) % P + P) % P);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Mychael/p/9252632.html