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}, number m and p, your job is to count how many set T satisfies the following condition:
- T is a subset of S
- |T| = m
- T does not contain continuous numbers, that is to say x and x+1 can not both in T
Input
There are multiple cases, each contains 3 integers n ( 1 <= n <= 109 ), m ( 0 <= m <= 104, m <= n ) and p ( p is prime, 1 <= p <= 109 ) in one line seperated by a single space, proceed to the end of file.
Output
Output the total number mod p.
Lucas定理p为质数情况裸题
因为是选的元素不能连续,我们先把选的元素拿出来,剩下的元素有n-m+1个空,选m个插进去行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,m,P;
ll Pow(ll a,ll b){
ll ans=;
for(;b;b>>=,a=a*a%P)
if(b&) ans=ans*a%P;
return ans;
}
ll Inv(ll a){return Pow(a,P-);}
ll C(ll n,ll m){
if(n<m) return ;
ll x=,y=;
for(ll i=n-m+;i<=n;i++) x=x*i%P;
for(ll i=;i<=m;i++) y=y*i%P;
return x*Inv(y)%P;
}
ll Lucas(ll n,ll m){
if(n<m) return ;
ll re=;
for(;m;n/=P,m/=P) re=re*C(n%P,m%P)%P;
return re;
}
int main(){
//freopen("in","r",stdin);
while(scanf("%lld%lld%lld",&n,&m,&P)!=EOF)
printf("%lld\n",Lucas(n-m+,m));
}
BZOJ 2982: combination
模数10007很小,可以直接线性预处理阶乘和逆元,48ms-->4ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=;
inline ll read(){
char c=getchar();ll x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
ll n,m,P=;
ll inv[N],fac[N],facInv[N];
void getInv(int n){
inv[]=fac[]=facInv[]=;
for(int i=;i<=n;i++){
if(i!=) inv[i]=-P/i*inv[P%i]%P;
inv[i]+=inv[i]<?P:;
fac[i]=fac[i-]*i%P;
facInv[i]=facInv[i-]*inv[i]%P;
}
} ll C(ll n,ll m){
if(n<m) return ;
return fac[n]*facInv[m]%P*facInv[n-m]%P;
}
ll Lucas(ll n,ll m){
if(n<m) return ;
ll re=;
for(;m;n/=P,m/=P) re=re*C(n%P,m%P)%P;
return re;
}
int main(){
freopen("in","r",stdin);
getInv(N-);
int T=read();
while(T--){
n=read();m=read();
printf("%lld\n",Lucas(n,m));
}
}
ZOJ 3557 & BZOJ 2982 combination[Lucas定理]的更多相关文章
- 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 ...
- BZOJ 2982: combination( lucas )
lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------- ...
- bzoj 2982 combination——lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里 if ( n<m ) r ...
- BZOJ 2982: combination Lucas模板题
Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...
- bzoj2982: combination(lucas定理板子)
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 664 Solved: 397[Submit][Status][Di ...
- 【BZOJ2982】combination Lucas定理
[BZOJ2982]combination Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然, ...
- bzoj——2982: combination
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 611 Solved: 368[Submit][Status][Di ...
- BZOJ 2982 combination
lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- BZOJ 2142: 礼物 [Lucas定理]
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1294 Solved: 534[Submit][Status][Discuss] ...
随机推荐
- MySQL的ibdata1文件占用过大
处理MySQL的ibdata1文件过大问题 本人遇到一次在安装zabbix监控的时候,yum安装的MySQL数据库,后面用了一段时间发现data目录下的ibdata1的空间特别大,反而我的zabbix ...
- Django REST framework 中 3 种类视图的对比
相较于使用基于方法(function based)的视图,我们更加倾向使用基于类(class based)的视图.接下来,你将看到这是一个强大的模式,是我们能够重用公共的功能,并且,帮我们减少重复的造 ...
- Hadoop2.7.5+Hbase1.4.0完全分布式
Hadoop2.7.5+Hbase1.4.0完全分布式一.在介绍完全分布式之前先给初学者推荐两本书:<Hbase权威指南>偏理论<Hbase实战>实战多一些 二.在安装完全分布 ...
- 创建jedis对象
1.先在taotao-parent的pom.xml中复制 以下内容到rest的pom.xml中 <!-- Redis客户端 --> <dependency> <group ...
- JS实现单选按钮回显时页面效果出现,但选中单选框的值为空
最近做了很多前端页面的工作,遇到的一个感觉很头疼的问题在这里记一下: 经常用JS回显单选框,但是明明从页面效果上来看,单选框已经被选中了,可是却不能触发单选框的change事件,取值的时候用某种方法取 ...
- 分布式监控系统--zabbix
1Zabbix简介 Zabbix 是一个企业级的分布式开源监控方案. 2.监控系统架构 C/S架构 客户端/服务器端,这种架构适合规模较小,处于同一地域的环境 C/P/S 客户端/代理端/服务器端/, ...
- 风险案例-28期-项目Leader与团队成员缺乏沟通,问题响应度较慢导致团队士气低落,工作效率低
典型案例: A公司某C类项目目前进入开发高峰期,项目组的三个leader预计在项目的实际task投入占比为70%,剩30%工作时间用于指导组员进行作业实施并担当部分管理工作.从项目实施过程中发现Lea ...
- jQuery 函数位于一个 document ready 函数中
在我们的实例中的所有 jQuery 函数位于一个 document ready 函数中: $(document).ready(function(){ --- jQuery functions go h ...
- PHP 环境搭建及zabbix安装遇到的一些坑.
参考https://segmentfault.com/a/1190000003409708 进行php环境搭建. 下载 php5 http://windows.php.net/download 下载 ...
- Python3基础知识之元组、集合、字典
1.元组 元组特点元组是不可变的两个元组可以做加法,不能做减法 元组的方法 >>> S('a', 'b', 'c', 'd', 'e')>>> S=('a','b' ...