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. 轉發和重定向-JSP

    最近在復習JSP,寫案例時遇到轉發和重定向的問題,忽然忘記了好多東西.趕緊搜索了下,感覺還是比較常用的. 轉:http://blog.csdn.net/CYHJRX/article/details/3 ...

  2. css 固定表头的表格,和 width:auto, margin:auto等 自计算方法

    实现思路: 外层用一个table,里面写好Header,然后里面再写一个table里面写好header.然后自己控制overflow的值使内部的tablemargin-top和外层的行高一致就可以实现 ...

  3. extjs 4.2 日期控件 选择时分秒功能

    因为不支持时分秒,然后在网上也找了一段时间的插件,但是感觉起来都不大方便,最后找一个插件,只需要引用js文件,然后修改类型,就可以实现extjs下面的datafield带时分秒功能了. 步骤: 只需要 ...

  4. 自定义FragmentTabHost--实现View重复加载问题

    1,接着上篇的Fragment+FragmentTabHost搭建简单的底部功能切换框架,效果如下: 结果在项目中用到的时候发现Fragment+FragmentTabHost实现的时候每一次切换底部 ...

  5. java冒泡排序算法

    /** * 冒泡排序算法:每次 * @author shaobn * @description 每次都会确定一个最大的值 即固定了,所以会每次-i; */ public class Test1 { p ...

  6. Envelope对象介绍

    Envelope也称包络线,是一个矩形区域,是每个几何形体的最小外接矩形.每个Geometry都拥有一个Envelope,包括Envelope自身. 它定义了XMax,XMin,YMax,YMin,H ...

  7. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...

  8. 夺命雷公狗---DEDECMS----28dedecms浏览次数的完成

    在页面里显示每部影视作品的浏览量,显示方法如下所示: 首先我们要在内容模板文件里增加显示区: 然后到后保存下文章页的内容页的模版,然后去看下对我们的样式有没有什么影响: 看到这里我们就已经成功一点点噢 ...

  9. zw版【转发·台湾nvp系列Delphi例程】HALCON 3D Position Of Circles

    zw版[转发·台湾nvp系列Delphi例程]HALCON 3D Position Of Circles procedure TForm1.action();var ho_Image, ho_Elli ...

  10. 【海岛帝国系列赛】No.3 海岛帝国:运输资源

    海岛帝国:运输资源 [试题描述] YSF考虑到“药师傅”帝国现在资源极度不平均,于是,商讨启用南水北调工程.YZM为首席工程师.现在,YSF由于工作紧张,准备军用物资和民用物资.但他要时时关注运输工程 ...