题目连接:Harvest of Apples

题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n)。(样例组数为1e5)

题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将C(m,n)求出来了。但这样还是会超时,所以接下来要分块,每隔500个处理出C(1~m,n)的结果。然后还要知道一个性质 C(a,b) = ∑C(t1,x)*C(t2,y)  (x+y<=b),这样我们就可以将给出的m和n分为两个组,其中一个组中元素的个数为500的倍数。然后我们对于另一个组枚举可能的t2然后求和,每次询问最大的操作次数就变成了500次。

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 1e9 + ;
typedef pair<int,int> P;
const int MAX_N = 1e5+;
int N,M,T,S;
LL Jc[MAX_N];
LL ni_[MAX_N];
LL tran[][MAX_N];
LL tr[][];
//费马小定理求逆元
LL pow(LL a, LL n, LL p) //快速幂 a^n % p
{
LL ans = ;
while(n)
{
if(n & ) ans = ans * a % p;
a = a * a % p;
n >>= ;
}
return ans;
} LL niYuan(LL a, LL b) //费马小定理求逆元
{
return pow(a, b - , b);
} void calJc() //求maxn以内的数的阶乘
{
Jc[] = Jc[] = ;
for(LL i = ; i < MAX_N; i++)
Jc[i] = Jc[i - ] * i % mod;
}
void calni(){
for(int i=;i<MAX_N;i++){
ni_[i] = niYuan(Jc[i],mod);
}
} LL C(LL a, LL b) //计算C(a, b)
{
return Jc[a] * ni_[b] % mod
* ni_[a-b] % mod;
} void init(){
for(int i=;i<MAX_N/;i++){
for(int j=;j<MAX_N;j++){
tran[i][j] = C(*i,j);
if(j>) tran[i][j] = (tran[i][j] + tran[i][j-])%mod;
}
} for(int i=;i<;i++){
for(int j=;j<;j++){
tr[i][j] = C(i,j);
}
}
}
int main(){
calJc();
calni();
init();
//cout<<"OK"<<endl;
cin>>T;
while(T--){
LL a,b;
scanf("%lld%lld",&a,&b);
LL ans = ;
if(a < ){
for(int i=;i<=b;i++)
ans = (ans + tr[a][i])%mod;
}
else{
LL t1 = a / ;
LL t2 = a - t1*;
for(int i=;i<=min(b,t2);i++){
ans = (ans + tr[t2][i]*tran[t1][min(b - i,t1*)])%mod;
}
}
printf("%lld\n",ans); }
return ;
}

HDU 6333 Harvest of Apples (分块、数论)的更多相关文章

  1. HDU - 6333 Harvest of Apples

    题意: T次询问,每次给出n,m.求sigma(k:0->m)C(n, k). 题解: 用离线莫队来做. 令S(n,m) = sigma(k:0->m)C(n, k). S(n+1, m) ...

  2. HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))

    2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

  3. hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  4. hdu多校第4场 B Harvest of Apples(莫队)

    Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...

  5. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  6. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

  7. HDU 6333 莫队+组合数

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  8. hdu6333 Problem B. Harvest of Apples(组合数+莫队)

    hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m)    设 ...

  9. Problem B. Harvest of Apples HDU - 6333(莫队)

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

随机推荐

  1. C++二进制输入输出流接口设计

    提到输入输出流,作为CPPer很自然的就会想到std::iostream,对于文本流的处理,iostream可以说足够强大,应付一般复杂度的需求毫无压力.对二进制流处理却只能用“简陋”来形容,悲催的是 ...

  2. python终端总是无法删除字符

    yum install readline-devel

  3. 什么是 Azure 中的虚拟机规模集?

    虚拟机规模集是一种 Azure 计算资源,可用于部署和管理一组相同的 VM. 由于所有 VM 的配置都相同,因此无需对 VM 进行任何预先配置. 这样就可以更方便地构建面向大型计算.大数据.容器化工作 ...

  4. Oracle EBS OM 已存在的OM订单增加物料

    DECLARE l_header_rec OE_ORDER_PUB.Header_Rec_Type; l_line_tbl OE_ORDER_PUB.Line_Tbl_Type; l_action_r ...

  5. shell脚本常用技巧

    shell脚本常用技巧 1.获取随机字符串或数字 ~]#echo $RANDOM | md5sum | cut -c 1-6 ~]#openssl rand -base64 4 | cut -c 1- ...

  6. SpringBoot_Mybatis_Maven_BootStrap

    ​​​ 需要eclipse 配置maven 未完,待续 链接:https://pan.baidu.com/s/1vej2zEdJmKeMUJD9S1B5MA 密码:y5j9

  7. Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block 解决办法

    昨晚更新了一下内核,今晚开机就无法进系统了...提示如下图: 解决方案:开机启动时按Esc,然后选择下面的旧版本的内核启动即可. (成功进入系统后,你可以选择改变开机默认选择的内核). uname - ...

  8. 在Linux服务器上运行Jupyter notebook server教程

    在Linux服务器上运行Jupyter notebook server教程 很多deep learning教程都推荐在jupyter notebook运行python代码,方便及时交互.但只在本地运行 ...

  9. PyQt5---firstwindow

    # -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...

  10. linux结束程序内存不会马上释放的解决方法

      Linux下频繁读写文件时,内存资源被耗尽,当程序结束后,内存不会释放需要清除缓存.Linux缓存有dentry,buffer cache,page cache. 注:Dentry用来加速文件路径 ...