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 c 信号量

    信号量(通过进程通信实现进程间的同步) 信号量(semaphore)信号灯 信号量是共享内存整数数组.根据需要定义指定的数组长度 信号量就是根据数组中的值,决定阻塞还是解除阻塞 编程模型: 1.    ...

  2. python SyntaxError: Non-ASCII character '\xd5' in file

    我使用的是python2.7, 在pycharm想运行程序,但是却报出了SyntaxError: Non-ASCII character '\xd5' in file 原因是因为源码中包含了中文注释, ...

  3. freemaker

    FreeMarker模板文件主要由如下4个部分组成:  1,文本:直接输出的部分  2,注释:<#-- ... -->格式部分,不会输出  3,插值:即${...}或#{...}格式的部分 ...

  4. Nginx+Tomcat7+Mencached负载均衡集群部署笔记

    Nginx+Tomcat+Memcached负载均衡集群服务搭建 操作系统:CentOS6.5 本文档主要解说,怎样在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群s ...

  5. Oracle expdp/impdp导出导入命令及数据库备份(转)

    使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...

  6. COM组件简介

    面向对象的思想难以适应这种分布式软件模型,于是组件化程序设计思想得到了迅速的发展. 按照组件化的程序设计的思想,复杂的应用程序被设计成一些小的,功能单一的组件模块,这些组件模块可以运行在同一台机器上, ...

  7. Unity3D嵌入WPF教程

    Unity3D嵌入WPF教程 创建一个 类库工程 添加 WindowForm 用户控件 (UserControl) 1).引入 UntiyWebPlayer COM 组件 在工具->选择工具箱中 ...

  8. Jquery中的delegate()使用方法介绍

    delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数 delegate定义和用法 delegate() 方法为指定的元素(属于被选 ...

  9. LeetCode 1. twoSums

    C++: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int&g ...

  10. Redis中各种方法的使用

    ①set ; i< ; i++) { // 不可以重复添加数据 client.AddItemToSet(KKey, "dong升-" + i); } client.Remov ...