ZOJ 3557 How Many Sets II
2015-08-08 10:41
211 查看
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
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.
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
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 <= 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 11 5 2 11
Sample Output
5 6
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 long ll 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; }
相关文章推荐
- Android菜单menu控件大全
- 【HDU】5362 Just A String【生成函数dp】
- app和app后端的通讯
- HDOJ1004
- SQL SERVER 2008 R2 数据库下载
- linux系统的文件和文件类型
- (算法)二叉树的第m层第k个节点
- 需要学习的工具推荐,以及我需要具备的知识
- android清理内存
- cocos2dx-33种场景切换
- PHP执行系统命令简介
- OC循环渐进:KVC的运算
- 10. ibatis数据访问对象
- Java基础-IO-字节输入输出流(笔记)
- iOS UI08_UITableView
- iOS UITableViewCell的性能问题
- 【Win 10 应用开发】Toast通知激活应用——前台&后台
- 【Win10 应用开发】自适应Toast通知的XML文档结构
- 实现Activity的滑动返回效果(二)
- Eratosthenes筛法