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

题目大意:
给一个集合,一共n个元素,从中选取m个元素,满足选出的元素中没有相邻的元素,一共有多少种选法(结果对p取模1 <= p <= 10^9) 
思路:好像是裸题、、、、
用插板法求出组合数。既然是从n个数中选择m个数,那么剩下的数为n-m,那么可以产生n-m+1个空,这道题就变成了把m个数插到这n-m+1个空中有多少种方法,即C(n-m+1,m)%p
但是求乘法逆元的已经行不通了,因为这里我们不确定他给出的p是否为素数,这样我们就只能用卢卡斯定理了卢卡斯定理不会的转:http://www.cnblogs.com/L-Memory/p/7352397.html不要预处理了,因为我们这里p不知道,而且如果要处理的话就要开10^9*long long空间。代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
ll t,n,m,p,ans;
ll read()
{
    ll x=,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
ll qpow(ll n,ll k,ll q)
{
    ll res=;
    while(k)
    {
        ) res=res*n%p;
        n=n*n%p; k>>=;
    }return res;
}
ll c(ll n,ll m,ll p)
{
    ll n1=,m1=;
    ;
    ;i<=m;i++)
     m1=m1*i%p;
    ;i<=n;i++)
     n1=n1*i%p;
    ,p)%p;
}
ll lus(ll n,ll m,ll p)
{
    ) ;
    return c(n%p,m%p,p)*lus(n/p,m/p,p)%p;
}
int main()
{
    while(scanf("%lld%lld%lld",&n,&m,&p)!=EOF)
    {
        ans=lus(n-m+,m,p);
        printf("%lld\n",ans);
    }
    ;
}

 

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. JavaScript--DOM节点属性

    节点属性 在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType ...

  2. Qt实现客户端与服务器消息发送

    这里用Qt来简单设计实现一个场景,即: ①两端:服务器QtServer和客户端QtClient ②功能:服务端连接客户端,两者能够互相发送消息,传送文件,并且显示文件传送进度. 环境:VS20013+ ...

  3. 02—IOC实现项目中的解耦

  4. HTML基础2——综合案例3——创建考试报名表格

    <html> <head> <title></title> </head> <body> <table width=&qu ...

  5. kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)

    首先,说明,我kafk的server.properties是 kafka的server.properties配置文件参考示范(图文详解)(多种方式) 问题详情 然后,我启动时,出现如下 [hadoop ...

  6. 6.13---example

    example如何使用?简单查询这个例子展示了如何用生成后的Example类去生成一个简单的where子句: TestTableExample example = new TestTableExamp ...

  7. CF842C Ilya And The Tree

    思路: 1. 如果根节点是0,那么可以通过一次dfs计算出所有节点的最大值. 2. 如果根节点不是0,那么其余各点的最大值一定是根节点的一个因子.首先计算出根节点的所有因子.在dfs到一个深度为d的节 ...

  8. P1823 音乐会的等待

    题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么他们是可以互相看得见的. ...

  9. ES6:Generator函数(1)

    Generator函数是ES6提供的一种异步编程解决方案.它会返回一个遍历器对象 function* helloWorldGenerator(){ yield “hello”; yield “worl ...

  10. 如何在linux使用nmap端口扫描工具扫描网段内开放的端口

    在另一个linux主机上,使用nmap命令即可 ,比如 我在1.1.1.2上开放了端口1111 -A -j ACCEPT 在1.1.1.1上执行 即可查到