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. 因Window服务器自动更新并重启导致WebSphere服务停止服务故障一例

    最近公司购买了两台Windows Server 2008 R2服务器用于提供提供Web服务,A机器安装了IHS+DM+WAS8.5集群,B机器安装了Oracle11gR2用于数据存储,两台机器均可连接 ...

  2. C#基础 特殊集合

    //stack 干草堆 //先进后出 进 push 出 pop //初始化 //Stack ss = new Stack(); //ss.Push(1); //ss.Push(2); //ss.Pus ...

  3. Java 遍历Map对象的4种方法

    http://blog.csdn.net/tjcyjd/article/details/11111401

  4. LN : leetcode 3 Longest Substring Without Repeating Characters

    lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...

  5. OKHTTP 简单分析

    内部使用了OKIO库, 此库中Source表示输入流(相当于InputStream),Sink表示输出流(相当于OutputStream) 特点: ·既支持同步请求,也支持异步请求,同步请求会阻塞当前 ...

  6. 关于ds添加datarow

    有一个dataset DS.如果我想将DS中的某一行复制,得到新的一行,添加到DS中. 可能就会想到:DS.Tables[0].Rows.Add(DS.Tables[0].Rows[i])但是这样程序 ...

  7. python学习笔记(6)——字典(Dictionary)

    dict= {key1 : value1, key2 : value2 ...} 关键词:字典中元素成对出现- key:value 格式- 两端{ } ,键:值,每对键值间用 ,隔开. 键key-唯一 ...

  8. Win10 “此环境变量太大。此对话框允许将值设置为最长2047个字符。" 解决方法。

    打开注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment 双击右边的 Path (RE ...

  9. IOS 11,UIWebView内容随状态栏高度下移,导致状态栏不透明

    解决方案: 方法1:在html中设置 <meta name="viewport" content="viewport-fit=cover,maximum-scale ...

  10. 梦想CAD控件图块COM接口知识点

    梦想CAD控件图块COM接口知识点 图块是将多个实体组合成一个整体,并给这个整体命名保存,在以后的图形编辑中图块就被视为一个实体.一个图块包括可见的实体如线.圆.圆弧以及可见或不可见的属性数据.图块的 ...