hdu3037(lucas定理)
2024-08-30 00:43:41
给定n,m,p 表示<=m个豆子放在n棵树上,一共有多少种方案数, 总的方案书mod p
如果将m个豆子放在n棵树上, 可以使用插板法
得到方案数是C(n+m-1,n-1)
那么将0<=i<=m个豆子放在n棵上的方案数是 C(n+i-1,n-1)
即
其中C(n,k)=C(n-1,k)+C(n-1,k-1) 的意思是从n个数中取出k个的组合, 那么对于一个数来说,它要么不取,转为C(n-1,k), 要么取转为C(n-1,k-1)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */ LL fact[]; LL pow(LL a, LL k, LL p)
{
LL ret = ;
while (k)
{
if (k & )
ret = ret * a %p;
a = a * a % p;
k >>= ;
}
return ret;
}
LL C(LL n, LL m, LL p)
{
if (n < m || m < ) return ;
LL a = fact[n], b = fact[n - m] * fact[m] % p;
return a * pow(b, p - , p) % p;
}
LL lucas(int n, int m, int p)
{
if (m == ) return ;
return C(n%p, m%p,p) * lucas(n / p, m / p, p) % p;
}
int main()
{
int t, n, m, p;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d", &n, &m, &p);
fact[] = ;
for (int i = ; i <= p; ++i)
fact[i] = fact[i - ] * i % p;
printf("%I64d\n", lucas(n + m, n, p)); }
return ;
}
最新文章
- 在Linux上使用Nginx为Solr集群做负载均衡
- 在虚拟机下安装hadoop集成环境(centos7+hadoop-2.6.4+jdk-7u79)
- app上线具体流程
- Linux centOS下搭建RTMP服务器的具体步骤
- Linux新建用户无法使用tab补全的修改办法
- mac(linux) 上如何安装ant
- Unity入门知识
- 解读zookeeper的配置项
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
- How many - HDU 2609 (trie+最小表示)
- JSONObject和JSONArray
- [vue最新实战] gank客户端(vue2 + vue-router2 + vuex +webpace + es6)新手福利,干货多多
- MySQL运维工具
- [CF1041F Ray in the tube][数学]
- eclipse项目环境搭建(做了好多遍,老是忘记,以此文帮助记忆)
- MySQL Cursor Demo
- maven web+spring mvc项目没有出现src/main/java路径
- PAT甲级 1129. Recommendation System (25)
- java多线程下的所的概念
- python---使用pycharm运行py文件
热门文章
- 细说UI线程和Windows消息队列
- Hook任务栏时钟窗口(原理其实很简单,就是注入DLL到时钟窗口进程(explorer.exe))
- haproxy 看到的是https,后台是http的原因
- ACM-计算几何之Quoit Design——hdu1007 zoj2107
- Cocos2d-x 3.1.1 lua-tests 开篇
- JavaScript 中的事件类型1(读书笔记思维导图)
- [置顶] iframe使用总结(实战)
- 超人学院Hadoop大数据技术资源分享
- drupal form 中图片上传
- A Game of Thrones(13) - Tyrion