bzoj 2982 combination——lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982
明明是lucas定理裸题……
非常需要注意C( )里 if ( n<m ) return 0; !!!!!
可以预处理阶乘和其逆元,也可以现求。现求阶乘逆元的话,可以把 jc[m] 和 jc[n-m] 乘起来再放到pw里。
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int mod=;
int T,n,m,jc[mod+],ans;
int pw(int x,int k)
{
int ret=;while(k){if(k&)(ret*=x)%=mod;x=(ll)x*x%mod;k>>=;}return ret;
}
void init()
{
jc[]=;
for(int i=;i<mod;i++)jc[i]=jc[i-]*i%mod;
}
int C(int n,int m)
{
if(n<m)return ;//
return (ll)jc[n]*pw(jc[m]*jc[n-m],mod-)%mod;//jc[m]*jc[n-m]一起求逆元
}
int lucas(int n,int m)
{
if(!m)return ;
if(n<mod&&m<mod)return C(n,m);
return lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod;
}
int main()
{
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
printf("%d\n",lucas(n,m));
}
return ;
}
现求阶乘逆元
#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int mod=;
ll n,m;
int T,jc[mod+],jcn[mod+],ans;
int pw(int x,int k)
{
int ret=;while(k){if(k&)(ret*=x)%=mod;(x*=x)%=mod;k>>=;}return ret;
}
void init()
{
jc[]=;
for(int i=;i<mod;i++)jc[i]=jc[i-]*i%mod;
jcn[mod-]=pw(jc[mod-],mod-);
for(int i=mod-;i>=;i--)jcn[i]=jcn[i+]*(i+)%mod;
}
int C(int n,int m)
{
if(n<m)return ;////
return jc[n]*jcn[m]%mod*jcn[n-m]%mod;
}
int lucas(ll n,ll m)
{
if(!m)return ;
if(n<mod&&m<mod)return C(n,m);
return lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod;
}
int main()
{
init();
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
printf("%d\n",lucas(n,m));
}
return ;
}
bzoj 2982 combination——lucas模板的更多相关文章
- BZOJ 2982: combination Lucas模板题
Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...
- 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 ...
- BZOJ 2982: combination( lucas )
lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------- ...
- BZOJ 2982 combination Lucas定理
题目大意:发上来就过不了审核了--总之大意就是求C(n,m) mod 10007 m,n∈[1,2*10^8] 卢卡斯定理:C(n,m)=C(n%p,m%p)*C(n/p,m/p) mod p 要求p ...
- BZOJ2982: combination Lucas模板
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 734 Solved: 437[Submit][Status][Di ...
- bzoj——2982: combination
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 611 Solved: 368[Submit][Status][Di ...
- BZOJ 2982 combination
lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- BZOJ 2982 combination 脑子+组合数学
可以发现,整个数列构成一个树形结构,并且是个完全二叉堆(小根堆). 并且这个堆的形态在给定$n$后是固定的,第1个位置上显然只能放1. 对子树的根来说,他自己是所分得的数集中最小的那个,所以从剩下$s ...
- 【BZOJ 2982】 2982: combination (卢卡斯定理)
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 510 Solved: 316 Description LMZ有n个 ...
随机推荐
- CSS3透明背景表单
在线演示 本地下载
- Go 文件操作
一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...
- DOCKER 简单操作
docker ps -a 列出所有镜像 docker start docder_name dcoker start docker_id 运行某个镜像 docker restart docker_nam ...
- hql join
文章一: 1.用hql语句 ` String hql="select student.id, student.name ,class.name from student映射实体类名 as s ...
- 3.Pycharm和navicate的使用
Pycharm的下载 进入到Pycharm官网,进入网页的最下边,下载企业版enterprise(可试用30天),企业版提供了创建项目.run等功能,而免费版没有这些功能 pycharm的使用: 在f ...
- win7下users用户文件转移到其他盘符
在安装完的系统中, 1.重启按F8键到修复系统界面: 2.下一步->下一步->运行dos窗口,打开dos窗口: 3.拷贝命令:robocopy "C:\Users" & ...
- 转:gdb相关学习
声明本文转自:http://www.cnblogs.com/rosesmall/archive/2012/04/12/2444431.html (查看内存部分有补充) 另推荐一个入门级的好文章:htt ...
- Java -- JDBC 操作mysql数据库
1. Demo1 导包时 不要导具体的mysql包, 为了兼容性,导JDBC 中 sql的包既可以了. public class Demo1 { /** * @param args * @throws ...
- 实例说明Java中的null(转)
让我们先来看下面的语句: String x = null; 1. 这个语句到底做了些什么? 让我们回顾一下什么是变量,什么是变量值.一个常见的比喻是 变量相当于一个盒子.如同可以使用盒子来储存物品一 ...
- 中安装rackspace private cloud --6 Deployment rpc
运行ansible playbook安装之前的准备工作: Perform deployment host initial setup Build containers on target hosts ...