bzoj2982: combination(lucas定理板子)
2982: combination
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 664 Solved: 397
[Submit][Status][Discuss]Description
LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样。那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值。(1<=m<=n<=200,000,000)Input
第一行一个整数t,表示有t组数据。(t<=200)接下来t行每行两个整数n, m,如题意。Output
T行,每行一个数,为C(n, m) mod 10007的答案。Sample Input
4
5 1
5 2
7 3
4 2Sample Output
5
10
35
6HINT
Source
组合数裸题,一般我们选用lucas定理实现
人话定义环节——
组合数:从n个数中取m个数,一共的方法数,记作C(n,m)
lucas定理:C(n,m)=C(n%mod,m%mod)*C(n/mod,m/mod)%mod
这里直接给出实现代码
#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<limits.h>
#include<ctime>
#define N 100001
typedef long long ll;
const int inf=0x3fffffff;
const int maxn=2017;
using namespace std;
inline ll read()
{
ll f=1,x=0;char ch=getchar();
while(ch>'9'|ch<'0')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return f*x;
}
const int mod=10007;
ll fac[mod<<1],inv[mod<<1];//fac函数存储n的阶乘,inv数组存放逆元的阶乘
void init()
{
fac[0]=fac[1]=1,inv[0]=inv[1]=1;
for(int i=2;i<mod;i++)fac[i]=fac[i-1]*i%mod;//预处理阶乘
for(int i=2;i<mod;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;//递推求逆元
for(int i=2;i<mod;i++)inv[i]=inv[i-1]*inv[i]%mod;//预处理逆元的阶乘
}
ll lucas(ll n,ll m)
{
if(m>n)return 0;
if(n<=mod&&m<=mod)return fac[n]*inv[m]*inv[n-m]%mod;
return lucas(n%mod,m%mod)*lucas(n/mod,m/mod)%mod;
}
int main()
{
int t=read();
init();
while(t--)
{
ll n=read(),m=read();
printf("%d\n",lucas(n,m));
}
}
bzoj2982: combination(lucas定理板子)的更多相关文章
- [BZOJ2982]combination Lucas定理
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982 $C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\ ...
- 【BZOJ2982】combination Lucas定理
[BZOJ2982]combination Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然, ...
- 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 ...
- BZOJ2982: combination Lucas模板
2982: combination Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 734 Solved: 437[Submit][Status][Di ...
- BZOJ2982: combination Lucas
Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案 ...
- bzoj2982 combination——卢卡斯定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 卢卡斯定理裸题: 原准备1A来着,结果输出忘了加回车! 预处理阶乘或者现求都可以,感觉 ...
- 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 ...
- 2018.09.14 bzoj2982: combination(Lucas定理)
传送门 貌似就是lucas的板子题啊. 练一练手感觉挺舒服的^_^ 代码: #include<bits/stdc++.h> #define mod 10007 #define ll lon ...
- Lucas定理模板【bzoj2982】【combination】
(上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...
随机推荐
- Java EE 开发环境搭建
1 Windows 1.1 JDK 下载: 下载地址:https://developer.oracle.com/java 安装文件:jdk-8u201-windows-x64.exe JDK 并不是越 ...
- Python读写文件的几种方式
一.pandas pandas模块是数据分析的大杀器,它使得对于文件相关的操作变得简单. 看一下它的简单使用 import pandas as pd # 读取 df = pd.read_csv('al ...
- Hadoop基础-镜像文件(fsimage)和编辑日志(edits)
Hadoop基础-镜像文件(fsimage)和编辑日志(edits) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看日志镜像文件(如:fsimage_00000000000 ...
- IDEA打印gc日志,设置JVM参数方法
打印gc日志 1.对指定运行程序输出GC日志: 点击edit configurations... 在vm options处加入-XX:+PrintGCDetails 测试:代码调用system.gc后 ...
- JSON 之 SuperObject(11): TSuperTableString、TSuperAvlEntry
JSON 之 SuperObject(11): TSuperTableString.TSuperAvlEntry - 万一 - 博客园http://www.cnblogs.com/del/archiv ...
- ansible入门及组件介绍
Ansible简介 Ansible是自动化运维的工具,基于Python开发,实现了批量系统配置.批量程序部署.批量运行命令等功能.Ansible是基于模块工作的,ansible提供一个框架,通过模块实 ...
- 论文笔记:Cross-Domain Visual Matching via Generalized Similarity Measure and Feature Learning
Cross-Domain Visual Matching,即跨域视觉匹配.所谓跨域,指的是数据的分布不一样,简单点说,就是两种数据「看起来」不像.如下图中,(a)一般的正面照片和各种背景角度下拍摄的照 ...
- mysql 备份报错mysqldump: [Warning] Using a password on the command line interface can be insecure.
-------------------------------------------------------------------------------- mysql 备份报错mysqldump ...
- OpenCV中Mat总结
一.数字图像存储概述 数字图像存储时,我们存储的是图像每个像素点的数值,对应的是一个数字矩阵. 二.Mat的存储 1.OpenCV1基于C接口定义的图像存储格式IplImage*,直接暴露内存,如果忘 ...
- Flow Problem
Flow Problem TimeLimit:5000MS MemoryLimit:32768KB 64-bit integer IO format:%I64d Problem Descript ...