题意:两匹马比赛有三种比赛结果,n匹马比赛的所有可能结果总数

解法:

设答案是f[n],则假设第一名有i个人,有C(n,i)种可能,接下来还有f(n-i)种可能性,因此答案为 ΣC(n,i)f(n-i)

另外这里给出两个求组合数的模板,卢卡斯定理的p是模数,并且要求是素数,第二个是递推式,适合于n<2000的情况

 #include<cstdio>
using namespace std;
const int maxn = 1e3;
const int mod = ;
typedef long long ll; /*--------------------------卢卡斯定理取模-----------------------*/
ll exp_mod(ll a, ll b, ll p) {
ll res = ;
while (b != ) {
if (b & ) res = (res * a) % p;
a = (a*a) % p;
b >>= ;
}
return res;
} ll Comb(ll a, ll b, ll p) {
if (a < b) return ;
if (a == b) return ;
if (b > a - b) b = a - b; ll ans = , ca = , cb = ;
for (ll i = ; i < b; ++i) {
ca = (ca * (a - i)) % p;
cb = (cb * (b - i)) % p;
}
ans = (ca*exp_mod(cb, p - , p)) % p;
return ans;
}
//Lucas定理对组合数取模
ll Lucas(int n, int m, int p) {
ll ans = ;
while (n&&m&&ans) {
ans = (ans*Comb(n%p, m%p, p)) % p;
n /= p;
m /= p;
}
return ans;
} /*----------------------组合数递推公式(适用n<2000)---------------------------*/
int C[maxn+][maxn+];
void Cal_C(int n) {
//传递的是一个二维的数组c
for (int i = ; i <= n; i++){
C[i][] = C[i][i] = ;
for (int j = ; j < i; j++)
C[i][j] = (C[i - ][j - ]%mod + C[i - ][j]%mod)%mod;
}
return;
}
/*--------------------------------------------------------------------------*/ int f[maxn+];
void generate() {
Cal_C(maxn);
f[] = ;
for (int i = ; i <= maxn; i++) {
f[i] = ;
for (int j = ; j <= i; j++)
f[i] = (f[i] + C[i][j] * f[i - j]) % mod;
}
return;
} int main() {
int T; scanf("%d", &T);
int kase = ;
generate();
while (T--) {
int n; scanf("%d", &n);
printf("Case %d: %d\n", kase++, f[n]);
}
return ;
}

Uva12034 (组合数取模)的更多相关文章

  1. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  2. 排列组合+组合数取模 HDU 5894

    // 排列组合+组合数取模 HDU 5894 // 题意:n个座位不同,m个人去坐(人是一样的),每个人之间至少相隔k个座位问方案数 // 思路: // 定好m个人 相邻人之间k个座位 剩下就剩n-( ...

  3. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  4. [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】

    题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...

  5. lucas定理解决大组合数取模

    LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...

  6. 2015 ICL, Finals, Div. 1 Ceizenpok’s formula(组合数取模,扩展lucas定理)

    J. Ceizenpok’s formula time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. BZOJ_2142_礼物_扩展lucas+组合数取模+CRT

    BZOJ_2142_礼物_扩展lucas+组合数取模 Description 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E 心目中的重要性不同 ...

  8. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

  9. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

随机推荐

  1. 使用自环接口的UDP服务器和客户端

    import argparse,socket from datetime import datetime MAX_BYTES = 65535 def server(port): sock = sock ...

  2. python学习记录(六)

    0903--https://www.cnblogs.com/fnng/archive/2013/04/21/3034442.html 基本语句的用法 使用逗号输出(想要同时输出文本和变量值,又不希望使 ...

  3. HYSBZ_2002_分块

    http://www.lydsy.com/JudgeOnline/problem.php?id=2002 分块基础题,初次接触,很巧妙. OJ好像挂了,没法提交. #include<iostre ...

  4. ubuntu 18. root登录图形界面

    修改文件 vim /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 增加两行: greeter-show-manual-login=true all-g ...

  5. 使用 Visual Studio 2015 + Python3.6 + tensorflow 构建神经网络时报错:'utf-8' codec can't decode byte 0xcc in position 78: invalid continuation byte

    使用 Visual Studio 2015 + Python3.6 + tensorflow 构建神经网络时报错:'utf-8' codec can't decode byte 0xcc in pos ...

  6. Burpsuite Pro 2020.1最新破解版

    简介 Burp Suite 是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻击应用程序的过程.所有工具都共享一个请求,并能处理对应的HTTP ...

  7. 苹果系统iOS、macOS应用管理机制

    iOS.macOS系统应用管理机制 苹果系统包括:iOS.macOS.watchOS.tvOS.应用软件的生命周期为:开发.上线.安装.使用.卸载.这篇文档将从应用生命周期的各个环节介绍苹果系统对应用 ...

  8. 【大白话系统】MySQL 学习总结 之 缓冲池(Buffer Pool) 如何支撑高并发和动态调整

    如果大家对我的 [大白话系列]MySQL 学习总结系列 感兴趣的话,可以点击关注一波. 一.上节回顾 在上节< 缓冲池(Buffer Pool) 的设计原理和管理机制>中,介绍了缓冲池整体 ...

  9. ELF文件之七——使用链接脚本-2个函数-data-bss-temp-call

    main.c int enable; ; int main() { int temp; add(); ; } int add() { ; } o反汇编的地址都是0起始,elf的地址都是映射后的地址. ...

  10. C语言快速排序函数------qsort();

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> ty ...