886. 求组合数 II(模板)

数据范围较大, a,b都是1e5
直接根据公式预处理

1/i就是求i的逆元(逆元求法:mod为质数,逆元就是 i^(mod-2)%mod )
O(N*logN)
import java.util.Scanner;
public class Main{
static final int N=100005;
static final int mod=(int)1e9+7;
static long fact[]=new long[N];
static long infact[]=new long[N];
static long quick_pow(long a,long b){
long res=1;
while(b>0){
if((b&1)==1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res%mod;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
fact[0]=infact[0]=1;
for(int i=1;i<N;i++){
fact[i]=fact[i-1]*i%mod;
infact[i]=infact[i-1]*quick_pow(i,mod-2)%mod;
}
int t=scan.nextInt();
while(t-->0){
int a=scan.nextInt();
int b=scan.nextInt();
System.out.println(fact[a]*infact[a-b]%mod*infact[b]%mod);
}
}
}
886. 求组合数 II(模板)的更多相关文章
- lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...
- 求组合数、求逆元、求阶乘 O(n)
在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- 牛客小白月赛14 -B (逆元求组合数)
题目链接:https://ac.nowcoder.com/acm/contest/879/B 题意:题目意思就是求ΣC(n,i)pi(MOD+1-p)n-i (k<=i<=n),这里n,i ...
- URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...
- N!分解质因子p的个数_快速求组合数C(n,m)
int f(int n,int p) { ) ; return f(n/p,p) + n/p; } https://www.xuebuyuan.com/2867209.html 求组合数C(n,m)( ...
- pku1365 Prime Land (数论,合数分解模板)
题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h. 先把合数分解模板乖乖放上: ; ans != ; ++i) { ) { num ...
- HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解
题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...
- hdu 2519 求组合数
求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...
随机推荐
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)
题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...
- BZOJ1257 [CQOI2007]余数之和 (数论分块)
题意: 给定n, k,求$\displaystyle \sum_{i=1}^nk\;mod\;i$ n,k<=1e9 思路: 先转化为$\displaystyle \sum_{i=1}^n(k- ...
- uniapp单页面配置无导航栏
{ "path": "pages/login/login", "style": { "navigationStyle": ...
- 13.深度学习(词嵌入)与自然语言处理--HanLP实现
笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 13. 深度学习与自然语言处理 13.1 传统方法的局限 前面已经讲过了隐马尔可夫 ...
- k8s 安装ELK(6.7.0版本)和EFK
一.Elasticsearch安装 helm安装的也行,而且helm安装的stable/elasticsearch可用性更高,但是使用资源更多. 1.安装elasticsearch(线上环境千万记得把 ...
- centos7利用系统镜像修复grub
1 故障描述 由于错误操作,导致grub配置文件失效,系统开机后一直卡在下面的画面. 2 解决办法 这时候,就要利用系统镜像光盘,进入修复模式,然后按下面图示操作 进入镜像的shell环境,如下图所示 ...
- HDU 2018 DP
A - 母牛的故事 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- vue中阻止事件穿透的方法
默认情况下,事件在h5页面会穿透传递,比如一div里面套一个div,点击上层div,下层div也会响应 要阻止事件穿透,使用event.stopPropagation(); 代码示例: <div ...
- StackExchange.Redis 之 String 类型示例
String类型很简单,就不做示例演示了,这里只贴出Helper类 /// <summary> /// 判断key是否存在 /// </summary> /// <par ...
- 记录zabbix4.0升级4.2
系统环境 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.9 (Final) 官方网站 官方文档升级其实很简单如果 ...