lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p).

-----------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
const int MOD = 10007;
 
int Inv[MOD], fac[MOD], N, M;
 
void gcd(int a, int b, int &d, int &x, int &y) {
if(!b) {
d = a;
x = 1;
y = 0;
} else {
gcd(b, a % b, d, y, x);
y -= x * (a / b);
}
}
 
int INV(int v) {
int d, x, y;
gcd(v, MOD, d, x, y);
return (x + MOD) % MOD;
}
 
void Init() {
fac[0] = 1;
for(int i = 1; i < MOD; i++)
fac[i] = i * fac[i - 1] % MOD;
for(int i = 0; i < MOD; i++)
Inv[i] = INV(fac[i]);
}
 
int C(int m, int n) {
if(m > n) return 0;
return fac[n] * Inv[m] % MOD * Inv[n - m] % MOD;
}
 
int Lucas(int m, int n) {
int ret = 1;
while(m) {
(ret *= C(m % MOD, n % MOD)) %= MOD;
n /= MOD;
m /= MOD;
}
return ret;
}
 
int main() {
Init();
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &N, &M);
printf("%d\n", Lucas(M, N));
}
return 0;
}

-----------------------------------------------------------------------------------------

2982: combination

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 245  Solved: 153
[Submit][Status][Discuss]

Description

LMZn个不同的基友,他每天晚上要选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 2

Sample Output

5
10
35
6

HINT

Source

BZOJ 2982: combination( lucas )的更多相关文章

  1. 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 ...

  2. bzoj 2982 combination——lucas模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里  if ( n<m ) r ...

  3. BZOJ 2982: combination Lucas模板题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...

  4. 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 ...

  5. bzoj——2982: combination

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 611  Solved: 368[Submit][Status][Di ...

  6. BZOJ 2982 combination

    lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  7. BZOJ 2982 combination 脑子+组合数学

    可以发现,整个数列构成一个树形结构,并且是个完全二叉堆(小根堆). 并且这个堆的形态在给定$n$后是固定的,第1个位置上显然只能放1. 对子树的根来说,他自己是所分得的数集中最小的那个,所以从剩下$s ...

  8. 【BZOJ 2982】 2982: combination (卢卡斯定理)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 510  Solved: 316 Description LMZ有n个 ...

  9. bzoj2982: combination(lucas定理板子)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 664  Solved: 397[Submit][Status][Di ...

随机推荐

  1. AspectJ AOP例子

    最近在学习Spring AOP,其中涉及到AspectJ的AOP框架.主要参考:http://howtodoinjava.com/spring/spring-aop/spring-aop-aspect ...

  2. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  3. QDialog弹出一个窗口,改变窗口大小

    创建一个QT应用 文件->新建文件或项目 Application->Qt Widgets Application 其他下一步 基类选择QDialog 其他下一步 resize() 改变窗口 ...

  4. 同一TextView上内容的不同显示

    首先请原谅我不会给文章起名字. . .不能准确的表达出究竟要讲什么,真实智商捉急. 直接上图 如图所看到的显示的是两个textview 第一个实现的是,在同一个textview中给不同内容赋予不同的颜 ...

  5. 开源 免费 java CMS - FreeCMS1.9 简历管理

    项目地址:http://code.google.com/p/freecms/ 简历管理 管理当前管理网站的简历数据. 1. 回复简历 选择须要回复的简历.然后点击"回复". 注意: ...

  6. OkHttp–支持SPDY协议的高效HTTP库

    Android为我们提供了两种HTTP交互的方式: HttpURLConnection 和 Apache HTTP Client,虽然两者都支持HTTPS,流的上传和下载,配置超时,IPv6和连接池, ...

  7. Swift学习笔记:类和结构

    一.类和结构的异同 类和结构有一些相似的地方.它们都能够: 1. 定义一些能够赋值的属性: 2. 定义具有功能性的方法 3. 定义下标.使用下标语法 4. 定义初始化方法来设置初始状态 5. 在原实现 ...

  8. xcode KVC:Key Value Coding 键值编码

    赋值 // 能修改私有成员变量 - (void)setValue:(id)value forKey:(NSString *)key; - (void)setValue:(id)value forKey ...

  9. Ueditor和CKeditor 两款编辑器的使用与配置

    一丶ueditor 百度编辑器 1.官方文档,演示,下载地址:http://ueditor.baidu.com/website/index.html 2.百度编辑器的好:Editor是由百度web前端 ...

  10. 基于HTTP协议下载文件的实现

    最近在开发文件下载的程序,该程序是基于HTTP开发的. 首先是了解了文件传输到客户端的大概格式,然后分析该格式,实现写入文件的功能. 自己构造的HTTP包如下: GET /*********.rar ...