hdu-6333-莫队

Problem B. Harvest of Apples

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2970    Accepted Submission(s): 1153


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

 

Input
The first line of the input contains an integer T (1T105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1mn105).
 

 

Output
For each test case, print an integer representing the number of ways modulo 109+7.
 

 

Sample Input
2 5 2 1000 500
 

 

Sample Output
16 924129523
 

 

Source
 
  经过观察可以发现,设S(m,n)=C(0,n)+C(1,n)+.....+C(m,n)的话,S(m,n+1)=2*S(m,n)-C(m,n) , S(m,n-1)=(S(m,n)+C(m,n-1))/2。
  S(m-1,n)=S(m,n)-C(m,n) ,S(m+1,n)=S(m,n)+C(m+1,n),也就是说我们能在O(1)求出来这四个式子,这样就可以用莫队处理了。
分块的时候我错把 q[i].m/M写成了 i/M导致一直T ,是对mi所在的块作为关键字而不是输入的次序。
  
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long 
 4 #define eps 1e-6
 5 #define mod 1000000007
 6 //LL mod=1e9+7;
 7 const int maxn=100000+5;
 8 LL len,p[maxn]={1,1},inv[maxn]={0,1},p_inv[maxn]={1,1},ans[maxn];
 9 int t;
10 struct Query{
11     LL n,m,blo;
12     int id;
13     bool operator<(const Query&C)const{
14         if(blo==C.blo) return n<C.n;
15         return blo<C.blo;    
16     }
17 }q[maxn];
18 LL cal(LL a,LL b)  
19 {
20     if(b>a)
21         return 0;
22     return p[a]*p_inv[b]%mod*p_inv[a-b]%mod;
23 }
24 int main(){
25     int n,m,i,j,k;
26     len=sqrt(maxn);
27     for(i=2;i<=100000;++i){
28     p[i]=p[i-1]*i%mod;
29     inv[i]=(mod-mod/i)*inv[mod%i]%mod;
30     p_inv[i]=p_inv[i-1]*inv[i]%mod;
31     }
32     scanf("%d",&t);
33     for(i=1;i<=t;++i){
34         scanf("%lld %lld",&q[i].n,&q[i].m);
35         q[i].id=i;
36         q[i].blo=q[i].m/len;
37     }
38     sort(q+1,q+1+t);
39     int L=0,R=1;
40     LL res=1;
41     for(i=1;i<=t;++i){
42         while(L<q[i].m){
43             res=(res+cal(R,++L))%mod;
44         }
45         while(L>q[i].m){
46             res=(res+mod-cal(R,L--))%mod;
47         }
48         while(R<q[i].n){
49             res=(res*2+mod-cal(R++,L))%mod;
50         }
51         while(R>q[i].n){
52             res=(res+cal(--R,L))%mod*inv[2]%mod;
53         }
54         ans[q[i].id]=res;
55     }
56     for(i=1;i<=t;++i)printf("%lld\n",ans[i]);
57     return 0;
58 }

 

 

转载于:https://www.cnblogs.com/zzqc/p/9431991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值