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.

Sample Input

5 1 11
5 2 11

Sample Output

5
6

由于m<=10^4,所以只需要一个for循环计算sum1,sum2就可以了。

和fzu 2020是一样的。

方案数目为C(n-k+1,k)种。

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; LL pow_mod(LL a,LL n,LL p)
{
LL ans=;
while(n)
{
if(n&) ans=(ans*a)%p;
n=n>>;
a=(a*a)%p;
}
return ans;
}
LL C(int n,int m,int p)/**Cnm %p **/
{
int i;
LL sum1=,sum2=;
for(i=;i<=m;i++)
{
sum1=(sum1*(n-i+))%p;
sum2=(sum2*i)%p;
}
sum1=(sum1*pow_mod(sum2,p-,p))%p;
return sum1;
}
void solve(int n,int m,int p)
{
LL ans=;
while(n&&m&&ans)
{
ans=(ans*C(n%p,m%p,p))%p;
n=n/p;
m=m/p;
}
printf("%lld\n",ans);
}
int main()
{
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)>)
{
n=n-m+;
if(n<m){
printf("0\n");
continue;
}
else if(n==m){
printf("1\n");
continue;
}
if(m>n-m) m=n-m;
solve(n,m,p);
}
return ;
}

zoj 3557 How Many Sets II的更多相关文章

  1. zoj——3557 How Many Sets II

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  2. ZOJ3557 How Many Sets II( Lucas定理)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud How Many Sets II Time Limit: 2 Seconds    ...

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

  4. [容斥原理] zoj 3556 How Many Sets I

    主题链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4535 How Many Sets I Time Limit: 2 ...

  5. ZOJ 3556 How Many Sets I

    How Many Sets I Time Limit: 2 Seconds      Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...

  6. zoj——3556 How Many Sets I

    How Many Sets I Time Limit: 2 Seconds      Memory Limit: 65536 KB Give a set S, |S| = n, then how ma ...

  7. Zoj 3535 Gao the String II (AC自己主动机+dp)

    题目大意: 用集合A中的串构造出一个串,使之让很多其它的setB中的串成为他的子串. 思路分析: 和 Codeforces 86C 几乎相同. 只是这里是要用A中的构造. 先用A 和 B的串构造一个自 ...

  8. 组合数们&&错排&&容斥原理

    最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...

  9. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

随机推荐

  1. sdutoj 2607 Mountain Subsequences

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Demacia of the Ancients

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5504  The 12th Zhejiang Provincial ...

  3. char 和 varchar2 区别

    char 与 varchar2 区别 a:char长度固定而varchar2长度可变 b:char的遍历效率要比varchar2的效率稍高 c:char 浪费空间节省时间 varchar2浪费时间节省 ...

  4. nginx的基本配置和虚拟主机的配置

    在Nginx配置文件(nginx.conf)中,一个最简化的虚拟主机配置代码如下: 跟Apache -样,Nginx也可以配置多种类型的虚拟圭机:一是基于IP的虚拟主机,二是基于域名的虚拟主机,三是基 ...

  5. 好用的调色软件 ColorSchemer Studio

    软件名叫:ColorSchemer Studio 这是windows平台下的软件

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON OverpaintRegion1

    zw版[转发·台湾nvp系列Delphi例程]HALCON OverpaintRegion1 unit Unit1;interfaceuses Windows, Messages, SysUtils, ...

  7. Delphi XE的firemonkey获取当前文件所在路径的方法

    Delphi XE的firemonkey获取当前文件所在路径的方法 在之前,我们知道有三种方法: ExtractFilePath(ParamStr(0)) ExtractFilePath(Applic ...

  8. spin_lock、spin_lock_irq、spin_lock_irqsave区别【转】

    转自:http://blog.csdn.net/luckywang1103/article/details/42083613 void spin_lock(spinlock_t *lock); voi ...

  9. struts2结果类型

    struts2结果类型: 结果类型 描述 前request域属性是否丢失 1 dispatcher 用于与jsp整合的结果类型.默认结果类型. 2 chain Action链式处理结果类型.前一个Ac ...

  10. 错误代码:ERR_UNSAFE_PORT

    修改下应用的端口为7788就好了:http://localhost:7788/taiping-sol-insu-vehicle/vehicleEntrance.action. 这个主要是chrome的 ...