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 <= 104m <= 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定理]的更多相关文章

  1. 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 ...

  2. BZOJ 2982: combination( lucas )

    lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------- ...

  3. bzoj 2982 combination——lucas模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里  if ( n<m ) r ...

  4. BZOJ 2982: combination Lucas模板题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...

  5. bzoj2982: combination(lucas定理板子)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 664  Solved: 397[Submit][Status][Di ...

  6. 【BZOJ2982】combination Lucas定理

    [BZOJ2982]combination Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然, ...

  7. bzoj——2982: combination

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 611  Solved: 368[Submit][Status][Di ...

  8. BZOJ 2982 combination

    lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  9. BZOJ 2142: 礼物 [Lucas定理]

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1294  Solved: 534[Submit][Status][Discuss] ...

随机推荐

  1. js随机产生区间数

    function selectFrom(startNumber, endNumber) { //1.从几开始 2.到几结束 var choice = endNumber - startNumber + ...

  2. STM32小结

    1.GPIO 电灯 推挽输出 PB5 2.GPIO 按键 浮空输入 PA0 3.写IO高电平 HAL_GPIO_WritePin(GPIOB,GPIO_Pin_5,1); 4.读取IO电平 HAL_G ...

  3. light oj 1152 Hiding Gold

    题目: You are given a 2D board where in some cells there are gold. You want to fill the board with 2 x ...

  4. springcloud干活之服务消费者(feign)

    springcloud系列文章的第三篇 本章将继续讲述springcloud的消费者(feign) Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端. ...

  5. SVN报Previous operation has not finished; run 'cleanup'&

    做着项目突然SVN报Previous operation has not finished; run 'cleanup' if it was interrupted,进度又要继续,烦.百度一下发现很多 ...

  6. 【开发技术】Eclipse插件Call Hierarchy简介及设置

    Call Hierarchy 主要功能是 显示一个方法的调用层次(被哪些方法调,调了哪些方法) 在MyEclipse里Help - Software updates - Find and instal ...

  7. iOS开发中一些有用的小代码

    1.判断邮箱格式是否正确的代码: //利用正则表达式验证 -(BOOL)isValidateEmail:(NSString *)email {   NSString *emailRegex = @&q ...

  8. 让自己写的项目支持Cocoapods管理

    学会使用别人的 Pods 依赖库以后, 你一定对创建自己的依赖库很有兴趣吧,现在我们一起来制作自己的Pods依赖库. 1.创建自己的 github 仓库 上图中标识出了6处地方 Repository ...

  9. start tomcat with debugging mode

    For this, you must run your application in debug mode, which requires below parameters. -Xdebug -Xru ...

  10. python_如何对实例属性进行类型检查?

    案例: 在某项目中,我们实现了一些类,并希望能像静态语言那样对他们的实例属性进行类型检查 p = Person() p.name = 'xi_xi'         # 必须是str p.age = ...