ZOJ3557 How Many Sets II( Lucas定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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 <= 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
这是个组合数的基础问题了,从n个数中挑出m个数,要求不相邻。显然公式就是C(n-m+1,m),然后就可以直接用Lucas定理做了
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define rep(X, N) for(int X=0;X<N;X++)
typedef long long ll; //
// Created by xyiyy on 2015/8/15.
// #ifndef ICPC_LUCAS_HPP
#define ICPC_LUCAS_HPP //
// Created by xyiyy on 2015/8/5.
// #ifndef ICPC_INV_HPP
#define ICPC_INV_HPP
typedef long long ll; void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a;
x = ;
y = ;
}
else {
extgcd(b, a % b, d, y, x);
y -= x * (a / b);
}
} ll inv(ll a, ll mod) {
ll x, y, d;
extgcd(a, mod, d, x, y);
return d == ? (x % mod + mod) % mod : -;
} #endif //ICPC_INV_HPP ll C(int n, int m, ll mod) {
if (n < m)return ;
if (m == )return ;
ll ret = ;
rep(i, m) {
ret = ret * (n - i) % mod * inv(i + , mod) % mod;
}
return ret;
} ll Lucas(ll n, ll m, ll mod) {
if (m == )return ;
else return (C(n % mod, m % mod, mod) * Lucas(n / mod, m / mod, mod)) % mod;
} #endif //ICPC_LUCAS_HPP class TaskI {
public:
void solve(std::istream &in, std::ostream &out) {
int n, m, p;
while (in >> n >> m >> p) {
out << Lucas(n - m + , m, p) % p << endl;
}
}
}; int main() {
std::ios::sync_with_stdio(false);
std::cin.tie();
TaskI solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return ;
}
ZOJ3557 How Many Sets II( Lucas定理)的更多相关文章
- 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 ...
- HDU3037 Saving Beans(Lucas定理+乘法逆元)
题目大概问小于等于m个的物品放到n个地方有几种方法. 即解这个n元一次方程的非负整数解的个数$x_1+x_2+x_3+\dots+x_n=y$,其中0<=y<=m. 这个方程的非负整数解个 ...
- 51nod 1120 机器人走方格 V3 卡特兰数 lucas定理
N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只能向右或向下走.并要求只能在这条线的上面或下面走,不能穿越这条线,有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 100 ...
- hdu3037 Lucas定理
Lucas定理 Lucas(n,m,p)=c(n%p,m%p)* Lucas(n/p,m/p,p),其中lucas(n,m,p)=C(n,m)%p (这里的除号是整除) 证明——百度百科 题意:求n个 ...
- Bzoj 4403: 序列统计 Lucas定理,组合数学,数论
4403: 序列统计 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 328 Solved: 162[Submit][Status][Discuss] ...
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- Lucas定理及其应用
Lucas定理这里有详细的证明. 其实就是针对n, m很大时,要求组合数C(n, m) % p, 一般来说如果p <= 10^5,那么就能很方便的将n,m转化为10^5以下这样就可以按照乘法逆元 ...
- BZOJ 3782: 上学路线 [Lucas定理 DP]
3782: 上学路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 192 Solved: 75[Submit][Status][Discuss] ...
随机推荐
- HTML滚动条
水平没有滚动条 <body scroll="no" style="overflow-x:hidden"> 垂直没有滚动条 <body scro ...
- 微软office MIME类型
后缀 MIME 類型 .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document .docm app ...
- 实现单实例多线程安全API问题
前阵子写静态lib导出单实例多线程安全API时,出现了CRITICAL_SECTION初始化太晚的问题,之后查看了错误的资料,引导向了错误的理解,以至于今天凌晨看到另一份代码,也不多想的以为singl ...
- [Python 3.x 官方文档翻译]The Python Tutorial Python教程
Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...
- 在centos服务器上配置gitlab钩子引发的一系列问题
为了给公司的服务器上搭建gitlab环境并且配置钩子(实现在本地git push之后服务器自动git pull),整了好久,最后终于把问题解决了,下面是记录安装gitlab之后引发的一系列问题: 首先 ...
- Centos7 打开80端口防火墙命令
iptables -I INPUT -p TCP --dport 80 -j ACCEPT
- 转:Qt编写串口通信程序全程图文讲解
转载:http://blog.csdn.net/yafeilinux/article/details/4717706 作者:yafeilinux (说明:我们的编程环境是windows xp下,在Q ...
- Android 自定义View(button)
很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义View上面花一些功夫,多写一些文章.先总结下自定义View的步骤 ...
- BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 47 Solve ...
- 【转】s3c2440 按键驱动 — 字符设备
原文网址:http://www.xuebuyuan.com/632893.html 主机:VM - redhat 9.0 开发板:FL2440,linux-2.6.12 arm-linux-gcc:3 ...