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. linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客

    linux中读写锁的rwlock介绍-nk_ysg-ChinaUnix博客 linux中读写锁的rwlock介绍 2013-02-26 13:59:35 分类: C/C++   http://yaro ...

  2. word-wrap,white-space和text-overflow属性

    (1) //在断点处换行 word-wrap: normal; //允许在长单词进行换行 word-wrap: break-word; (2) white-space:怎么处理元素间的空白 white ...

  3. Codechef Nuclear Reactors 题解

    There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. ...

  4. 仅仅需手动添加一行代码就可以让Laravel4执行在SAE (v. 1.0.0)

    Github:https://github.com/chariothy/laravel4-sae (已更新至v1.1.0) laravel4-sae (v. 1.0.0) 仅仅需手动添加一行代码就可以 ...

  5. phpExcel在封装

    <?php /** * 数组生成Excel * @author zouhao zouhao619@gmail.com * 使用示例 * $excel =new Excel(); $data=ar ...

  6. C++编程技术之 异常处理(上)

    增强错误恢复能力是提高代码健壮性的最有力途径之一 之所以平时编写代码的时候不愿意去写错误处理,主要是由于这项工作及其无聊并可能导致代码膨胀,导致的结果就是本来就比较复杂的程序变得更加复杂.当然了,前面 ...

  7. gridview合并相同的行

    #region 方法:合并Gridview行    /// <summary>    /// 合并GridView指定行单元格    /// </summary>    /// ...

  8. JavaScript之面向对象学九(原型式继承和寄生式继承)

    一.原型式继承 该继承模式是由道格拉斯*克罗克福德在2006年提出的实现继承的方法. 模式的基本思路:借助原型可以基于已有的对象创建新的对象,同时还不必因此创建自定义类型. 代码如下: functio ...

  9. 全面修复IE,注册IE所有dll

    全面修复IE,注册IE所有dll 复制,粘贴到文本文档里,保存成.bat文件,双击运行. rundll32.exe advpack.dll /DelNodeRunDLL32 %systemroot%\ ...

  10. Android 创建目录

    //android 内部存储自定义目录写入文件 try{ File testDir = new File(this.getFilesDir().getAbsolutePath() + File.sep ...