ZOJ 3557 How Many Sets II
来源:互联网 发布:space x 知乎 编辑:程序博客网 时间:2024/05/16 05:58
Given a set S = {1, 2, ..., n}, number m and p, your job is to count how many setT satisfies the following condition:
- T is a subset of S
- |T| = m
- T does not contain continuous numbers, that is to say x andx+1 can not both in T
Input
There are multiple cases, each contains 3 integers n ( 1 <= n <= 109 ),m ( 0 <= m <= 104,m <= 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 115 2 11
Sample Output
56
Author: QU, Zhe
Contest: ZOJ Monthly, October 2011
题意:给一个集合,一共n个元素,从中选取m个元素,满足选出的元素中没有相邻的元素,这样的选法一共有多少种?
说的高大上就是隔板法。其实就是。和正常做法没什么差别。
要从N个元素里面取M个元素。就是C(N,M)
应为题目要求没有相邻的元素,所以假设你已经取出了M个球,在取球的过程中,有M-1个球是肯定不能取得。
所以能取得球就是n-(m-1);
所以就是C(N-(M-1),M).
应为这里的数据范围特别大,所以要用到Lucas
#include<iostream>using namespace std;#define ll long longll n,m,p;long long make_pow(long long x,long long y,long long mod){long long res =1;while(y>0){if(y&1)res = res*x%mod;x=x*x%mod;y>>=1; } return res;}long long C(long long x,long long y) { if(y>x) return 0; else { long long a,b,ans=1; for(long long i=1;i<=y;i++) { a=(x+i-y)%p; b=i%p; ans=ans*(a*make_pow(b,p-2,p)%p)%p; } return ans; } } long long Lucas(long long x, long long y) { if(y==0) return 1; else return (C(x%p,y%p)*Lucas(x/p,y/p))%p; } int main(){while(cin >> n >> m >> p){ll ans;ans = Lucas(n-m+1,m)%p;cout<<ans<<endl;}return 0;}
0 0
- ZOJ 3557 How Many Sets II
- ZOJ 3557 How Many Sets II lucas 定理
- ZOJ 3557 How Many Sets II (组合计数,Lucas定理,费马小定理)
- ZOJ 3557-How Many Sets II(Lucas定理+插板法求组合数)
- zoj How Many Sets II 3557 (组合数学&&转换)好题
- zju 3557 How Many Sets II(Lucas 定理)
- zoj How Many Sets I(组合计数)
- [2014Contest_1I]How Many Sets II
- ZOJ 3556 How Many Sets I 解题报告(数论)
- [容斥原理] zoj 3556 How Many Sets I
- ZOJ 3556 How Many Sets I 二项式+容斥
- ZOJ 3556 How Many Sets I (容斥)
- zoj 3556 How Many Sets I - 容斥原理
- zoj 3556 How Many Sets I(容斥原理)
- [ZOJ3557]How Many Sets II(组合数学Lucas定理)
- [ZOJ3557]How Many Sets II(组合数学Lucas定理)
- How Many Sets
- How Many Sets I
- 南邮 OJ 2049 硬币正面数量
- hdu5358(尺取法)
- Android之——iptables常用命令
- JAVA8新特性
- VIM命令合集
- ZOJ 3557 How Many Sets II
- 安卓解决viewPager和scrollView和listView滑动冲突的问题
- android 反编译
- 开源驰骋工作流CCFlow的通过导入内部数据生成表单的示例
- android-----ANR
- 南邮 OJ 2056 万万没想到(1)
- android清理内存
- 3287POJ
- The Blocks Problem