(上不了p站我要死了,侵权度娘背锅)

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 2

Sample Output

5

10

35

6

就是一道裸的组合数题,挂上自己的Lucas模板。

Lucas其实相当于将n、m按p进制分解求组合数,再乘起来。

当n < m时组合数的值为零,于是我们发现一个数按p进制分解后的C(a,b)中a有一半的几率小于b,而一旦出现这种情况答案就是0。所以在用Lucas定理时会有很大的概率答案为0。

当然这并不是Lucas定理有问题。仔细想想,如果我们把C(n,m)化为阶乘的形式,由于n和m都大于p(模数),所以阶乘的项里面很可能包含p或p的倍数。一旦出现p的倍数,答案就是0了。

模板

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#ifdef WIN32
#define RIN "%I64d"
#else
#define RIN "%lld"
#endif
#define ll long long template <typename T>inline void read(T &res){
T k=1,x=0;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')k=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
res=k*x;
} const ll mod=10007; ll jiec[10010],niy[10010];
ll n,m; void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1,y=0;
return ;
}
ll x0,y0;
exgcd(b,a%b,x0,y0);
x=y0;
y=x0-(a/b)*y0;
}
ll inverse(ll a){
ll x,y;
exgcd(a,mod,x,y);
return (x%mod+mod)%mod;
}
void init(){
jiec[0]=niy[0]=1;
for(int i=1;i<mod;i++) jiec[i]=jiec[i-1]*i%mod;
niy[mod-1]=inverse(jiec[mod-1]);
for(int i=mod-2;i>=1;i--) niy[i]=niy[i+1]*(i+1)%mod;
}
ll comb(ll a,ll b){
return jiec[a]*niy[b]%mod*niy[a-b]%mod;
}
ll lucas(ll a,ll b){
if(a<b) return 0;
if(a==0&&b==0) return 1;
if(a<mod&&b<mod) return comb(a,b);
return lucas(a/mod,b/mod)*lucas(a%mod,b%mod)%mod;
}
void solve(){
read(n),read(m);
printf(RIN"\n",lucas(n,m));
}
int main(){
init();
int t;
read(t);
while(t--) solve();
return 0;
}

Lucas定理模板【bzoj2982】【combination】的更多相关文章

  1. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  2. 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数

    typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...

  3. lucas定理 模板

    lucas定理 (nm)&VeryThinSpace;mod&VeryThinSpace;p=(⌊np⌋⌊mp⌋)(n&VeryThinSpace;mod&VeryTh ...

  4. 【组合数+Lucas定理模板】HDU 3037 Saving

    acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...

  5. BZOJ 4403 2982 Lucas定理模板

    思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...

  6. Lucas定理模板

    用于大组合数对p取模的计算. #include <cstdio> #include <iostream> #include <cmath> #include < ...

  7. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  8. xdoj-1057(Lucas定理的证明及其模板)

    Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式:    ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...

  9. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

随机推荐

  1. ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor 【转】

    原文链接:https://www.cnblogs.com/RainingNight/p/strongly-typed-options-ioptions-monitor-in-asp-net-core. ...

  2. PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名

    题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...

  3. php数组循环的三种方式

    PHP 的遍历数组的三种方式:for循环.foreach循环.while.list().each()组合循环 PHP当中数组分为:索引数组[转换成json是数组]和关联数组[转换成json是对象] f ...

  4. php学习ing

    cmd运行,表示在本地d:/php/workspace下文件映射在127.0.0.1的8080端口下,-S -t不要忘记 php -S 127.0.0.1:8080 -t E:\class_manag ...

  5. poj 1840 枚举

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13967   Accepted: 6858 Description ...

  6. Nginx和Squid配合搭建的Web服务器前端系统

    这个架构是目前我个人觉得比较稳妥并且最方便的架构,易于多数人接受: 前端的lvs和squid,按照安装方法,把epoll打开,配置文件照搬,基本上问题不多. 这个架构和app_squid架构的区别,也 ...

  7. SheetJS & Error: Sheet names cannot exceed 31 chars

    SheetJS Error: Sheet names cannot exceed 31 chars title + version https://github.com/SheetJS/js-xlsx ...

  8. NetScaler Active-Active模式

    NetScaler Active-Active模式 NetScaler Active-Active模式 (此文档基于版本:NS9.3: Build 55.6 nc) By ShingTan Activ ...

  9. POJ 3974 Palindrome | 马拉车模板

    给一个字符串,求最长回文字串有多长 #include<cstdio> #include<algorithm> #include<cstring> #define N ...

  10. 【转发】Build Squid with SSL Bump and ICAP Client

    原文文档:http://docs.diladele.com/administrator_guide_3_4/installation_and_removal/filtering_https.html ...