HDU 6333 Harvest of Apples (分块、数论)
题目连接: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 (分块、数论)的更多相关文章
- 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) ...
- 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- hdu多校第4场 B Harvest of Apples(莫队)
Problem B. Harvest of Apples Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- 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 ...
- Harvest of Apples
问题 B: Harvest of Apples 时间限制: 1 Sec 内存限制: 128 MB提交: 18 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...
- HDU 6333 莫队+组合数
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 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) 设 ...
- 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 ...
随机推荐
- Apache,PHP,MySQL独立安装
最近在工作中常常接触到PHP,自己也写过一些简单的PHP页面.我们知道PHP是在服务器端运行的脚本语言,因此我们需要配置服务器环境.之前为了省事直接使用的是wamp集成环境,但是突然某一天领导要求我们 ...
- python自学——函数-strftime
strftime()函数的用法 strftime()函数可以把YYYY-MM-DD HH:MM:SS格式的日期字符串转换成其它形式的字符串. strftime()的语法是strftime(格式, ...
- SQL Server 查询表的字段对应描述、数据类型、长度
create procedure proc_view_cloumns ( ) ) AS select b.name,a.value,c.name, b.max_length from sys.exte ...
- MFC 常用功能总结
目录 ◆ 获取启动参数◆ 调整窗体大小◆ 关闭Dialog窗体◆ Dialog启动时指定控件为焦点◆ Dialog中禁止ESC回车关闭窗体◆ 回车后焦点自动跳到下一个控件◆ 窗体中某个控件捕获右键菜单 ...
- HashSet集合的add()方法的源码
interface Collection { ... } interface Set extends Collection { ... } class HashSet implements Set { ...
- burpsuite联合sqlmap扫描注入点
其实我们在众测的时候完全可以使用burpsuite联合sqlmap测试目标的注入漏洞.对get和post型注入都支持. 先来记录proxy的log , 记住路径 把proxy拦截关掉 接下来浏 ...
- Android (争取做到)最全的底部导航栏实现方法
本文(争取做到)Android 最全的底部导航栏实现方法. 现在写了4个主要方法. 还有一些个人感觉不完全切题的方法也会简单介绍一下. 方法一. ViewPager + List<View> ...
- CentOS中安装Azkaban 2.5
必备软件 yum install git -y 单机安装步骤 git clone https://github.com/azkaban/azkaban.git cd azkaban; ./gradle ...
- Linux中Shell
Linux中Shell Shell是什么 Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...
- Python2.7-Queue
Queue 模块,python3中为queue,一般和threading模块同时使用,用于处理多任务队列,模块定义了3种队列类,先进先出(FIFO),后进先出(LIFO),优先级队列 Queue.Qu ...