zoukankan      html  css  js  c++  java
  • BZOJ3142 [Hnoi2013]数列 【组合数学】

    题目链接

    BZOJ3142

    题解

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

    题目中(K(M - 1) < N)的限制意味着,除了第一个数外,别的数可以随便选,然后第一个数就限制在(N - sum a_i)之间
    所以方案数为

    [sumlimits_{a_1 = 1}^{M} sumlimits_{a_2 = 1}^{M} sumlimits_{a_3 = 1}^{M} dots sumlimits_{a_{K - 1} = 1}^{M} (N - sumlimits_{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); return 0;}
    	printf("%lld
    ",((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;
    }
    
    
  • 相关阅读:
    增量+全量备份SVN服务器
    日常小命令集锦
    filebeat输出到kafka
    在Logstash的配置文件中对日志事件进行区分
    NFS服务器简易安装
    记录一次MySQL数据库CPU负载异常高的问题
    使用Spring的jdbcTemplate进一步简化JDBC操作
    Stream 和 byte[] 之间的转换
    C# 文件转byte数组,byte数组再转换文件
    groupbox里面添加Form
  • 原文地址:https://www.cnblogs.com/Mychael/p/9252632.html
Copyright © 2011-2022 走看看