【BZOJ】2982: combination(lucas定理+乘法逆元)
http://www.lydsy.com/JudgeOnline/problem.php?id=2982
少加了特判n<m return 0就wa了QAQ
lucas定理:C(n, m)%p=(C(n%p, m%p)*C(n/p, m/p))%p
等英语好一点去wiki看一下证明吧QAQhttp://en.wikipedia.org/wiki/Lucas%27_theorem
然后这是网上搜到的关于lucas的一些内容
首先给出这个Lucas定理:
A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) mod p同余
即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
这个定理的证明不是很简单,我一直想找个很好的张明,但是,没找到,昨天看到了一个解题报告,基本上可以说明白这个Lucas定理是怎么回事了,具体的是说:
以求解n! % p为例,把n分段,每p个一段,每一段求的结果是一样的。但是需要单独处理每一段的末尾p, 2p, ...,把p提取出来,会发现剩下的数正好又是(n / p)!,相当于划归成了一个子问题,这样递归求解即可。
这个是单独处理n!的情况,当然C(n,m)就是n!/(m!*(n-m)!),每一个阶乘都用上面的方法处理的话,就是Lucas定理了,注意这儿的p是素数是有必要的。
Lucas最大的数据处理能力是p在10^5左右,不能再大了,hdu 3037就是10^5级别的!
对于大组合数取模,n,m不大于10^5的话,用逆元的方法,可以解决。对于n,m大于10^5的话,那么要求p<10^5,这样就是Lucas定理了,将n,m转化到10^5以内解。
然后左边暴力加逆元就行了,右边就是lucas。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int MD=10007;
int mpow(int a, int b) {
int ret=1;
for(; b; b>>=1, a=(a*a)%MD) if(b&1) ret=(ret*a)%MD;
return ret;
}
int getc(int n, int m) {
if(n<m) return 0;
int up=1, down=1;
for1(i, m+1, n) up=(up*i)%MD;
for1(i, 1, n-m) down=(down*i)%MD;
return (up*mpow(down, MD-2))%MD;
}
int lucas(int n, int m) {
return m?(getc(n%MD, m%MD)*lucas(n/MD, m/MD))%MD:1;
} int main() {
int t=getint();
while(t--) {
int n=getint(), m=getint();
printf("%d\n", lucas(n, m));
}
return 0;
}
Description
Input
Output
Sample Input
5 1
5 2
7 3
4 2
Sample Output
10
35
6
HINT
Source
【BZOJ】2982: combination(lucas定理+乘法逆元)的更多相关文章
- ZOJ 3557 & BZOJ 2982 combination[Lucas定理]
How Many Sets II Time Limit: 2 Seconds Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...
- BZOJ 2982 combination Lucas定理
题目大意:发上来就过不了审核了--总之大意就是求C(n,m) mod 10007 m,n∈[1,2*10^8] 卢卡斯定理:C(n,m)=C(n%p,m%p)*C(n/p,m/p) mod p 要求p ...
- bzoj1272 Gate Of Babylon(计数方法+Lucas定理+乘法逆元)
Description Input Output Sample Input 2 1 10 13 3 Sample Output 12 Source 看到t很小,想到用容斥原理,推一下发现n种数中选m个 ...
- HDU3037 Saving Beans(Lucas定理+乘法逆元)
题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...
- bzoj 2982 combination——lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里 if ( n<m ) r ...
- BZOJ 2982: combination( lucas )
lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------- ...
- BZOJ 2982: combination Lucas模板题
Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...
- hihocoder #1698 假期计划 (排列组合+费马小定理+乘法逆元)
Description 小Ho未来有一个为期N天的假期,他计划在假期中看A部电影,刷B道编程题.为了劳逸结合,他决定先拿出若干天看电影,再拿出若干天刷题,最后再留若干天看电影.(若干代指大于0) 每 ...
- bzoj2982: combination(lucas定理板子)
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 664 Solved: 397[Submit][Status][Di ...
随机推荐
- 正则表达式匹配a标签或div标签
这里以a标签为例 a标签的href var a='<P><A href=\'~abc/ccg/ab.jpg\' width="3">文字</A> ...
- PageRank学习
喜欢手写学习,记忆深刻(字丑勿喷!). 计算过程的代码如下: public class PageRank { private static double m[][]={ { 0 , 0.5 , 1 , ...
- python的__init__和__new__
本文所有实例代码在python3.7下 一.__new__和__init__区别 1.__new__先于__init__执行;__new__是相当于其他OOP语言的构造方法,负责创建实例:之后,__i ...
- ant-design 设置 DatePicker 默认值
1.代码 render() { const { value } = this.props; return ( <React.Fragment> { value ? <DatePick ...
- 【VBA编程】01.第一个VBA程序Hello world
[程序1] 所有程序语言的开始都源于Hello world,那么我们也使用Hello world进行第一个VBA编程 新建Excle文件-----文件-------选项-----自定义功能区域---- ...
- 07-hibernate进阶
1,hibernate.cfg.xml常用配置 2,session简介 3,transaction简介 4,session详解 5,对象关系映射常用配置 hibernate.cfg.xml常用配置 s ...
- LNMP环境搭建——MySQL篇
The world's most popular open source database 1.Install MySQL root@kallen:~# apt-get install mysql-s ...
- Python 爬虫之 BeautifulSoup
简介 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出 ...
- 在windows 2012中安装sharepoint 2013时遇到问题的处理办法
众所周知,sharepoint 2013是早于windows 2012的,所以在安装的时候,总会出现各种奇怪的问题,也就是所谓的一个个坑,为了减少大家掉到坑里的次数和排除故障的时间,我在这里记录下我曾 ...
- kettle--组件(1)--值映射
组件:值映射 如下如所示: 首先,给出官方给出的文档: 个人理解: Target field name:可以理解为将source column的字段复制为另一个target column的名字. De ...