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}, 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
由于m<=10^4,所以只需要一个for循环计算sum1,sum2就可以了。
和fzu 2020是一样的。
方案数目为C(n-k+1,k)种。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; LL pow_mod(LL a,LL n,LL p)
{
LL ans=;
while(n)
{
if(n&) ans=(ans*a)%p;
n=n>>;
a=(a*a)%p;
}
return ans;
}
LL C(int n,int m,int p)/**Cnm %p **/
{
int i;
LL sum1=,sum2=;
for(i=;i<=m;i++)
{
sum1=(sum1*(n-i+))%p;
sum2=(sum2*i)%p;
}
sum1=(sum1*pow_mod(sum2,p-,p))%p;
return sum1;
}
void solve(int n,int m,int p)
{
LL ans=;
while(n&&m&&ans)
{
ans=(ans*C(n%p,m%p,p))%p;
n=n/p;
m=m/p;
}
printf("%lld\n",ans);
}
int main()
{
int n,m,p;
while(scanf("%d%d%d",&n,&m,&p)>)
{
n=n-m+;
if(n<m){
printf("0\n");
continue;
}
else if(n==m){
printf("1\n");
continue;
}
if(m>n-m) m=n-m;
solve(n,m,p);
}
return ;
}
zoj 3557 How Many Sets II的更多相关文章
- 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 ...
- ZOJ3557 How Many Sets II( Lucas定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud How Many Sets II Time Limit: 2 Seconds ...
- 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 ...
- [容斥原理] zoj 3556 How Many Sets I
主题链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4535 How Many Sets I Time Limit: 2 ...
- 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 ...
- 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 ...
- Zoj 3535 Gao the String II (AC自己主动机+dp)
题目大意: 用集合A中的串构造出一个串,使之让很多其它的setB中的串成为他的子串. 思路分析: 和 Codeforces 86C 几乎相同. 只是这里是要用A中的构造. 先用A 和 B的串构造一个自 ...
- 组合数们&&错排&&容斥原理
最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...
- AC自动机-算法详解
What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...
随机推荐
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Missing artifact com.sun:tools:jar 1.5.0 终极解决方法
在使用m2eclipse插件时,在pom.xml中添加struts2-core.jar包后,需要依赖java运行时的tools.jar进行依赖.但是,此时eclipse无法读取tools包,出现如下错 ...
- LED流水灯(二)
记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8 // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...
- 安装Eclipse并配置JacORB插件
前人成果 • eclipse中开发corba完整说明(jacORB版) http://blog.csdn.net/hq0927/article/details/8129534 • ...
- 深入剖析PHP输入流 php://input (转载 http://www.nowamagic.net/academy/detail/12220520)
http://www.nowamagic.net/academy/detail/12220520
- 夺命雷公狗ThinkPHP项目之----企业网站19之网站配置信息的修改
我们这个其实也是很简单的,思路是直接将提交过来的cf_id 改成我们自己定义好的 “1” 即可,因为1配置只能有一个,所以永久都是该id 为1的: 先来完成我们的控制器,代码如下所示: public ...
- c 语言 结构体
一:结构体定义结构体类型变量 三种方式1st:先声明结构体类型,再定义该类型的变量struct student liming,zhangle;2nd:声明类型的同时定义变量struct student ...
- 几款python集成开发环境
以下软件的测试环境为ArchLinux64位系统.对软件的介绍很粗略,详细介绍参考官网. 1-名称:eric 官网:http://eric-ide.python-projects.org/ 特点:该软 ...
- Floyd算法核心代码证明
Flody 大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...
- SQL Server 索引视图 聚簇索引
创建示例: 朋友的网站速度慢,让我帮忙看下,他用的SQL Server里面 有一个文章表里面有30多万条记录 还有一个用户表里面也差不多17万记录 偏偏当初设计的时候没有冗余字段 很多帖子信息需要JO ...