Harvest of Apples

Source: Internet
Author: User

Issue B:harvest of Apples time limit: 1 Sec memory limit: MB
Submitted: Resolution: 11
Submitted State [Discussion Version] [Propositional person:Admin]
Title Description There is n apples on a tree, numbered from 1 to N.
Count the number of ways to pick at most m apples.

Enter the first line of the input contains an integer T (1≤t≤105) denoting the number of test cases.
Each test case consists the one line with the integers n,m (1≤m≤n≤105).

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

Sample input
25 21000 500

Sample output
16924129523

Mo Team (sub-block), combinatorial mathematics.

AC Code:

#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intmaxn=1e5+Ten;Const intmod=1e9+7; ll Fac[maxn],inv[maxn],ans[maxn];ll Rev2,res;intPos[maxn];ll Qpow (ll B,intN) {LL res=1;  while(n) {if(n&1) res=res*b%MoD; b=b*b%MoD; N>>=1; }    returnRes;} ll Comb (intNintk) {    returnfac[n]*inv[k]%mod*inv[n-k]%MoD;}voidPre () {Rev2=qpow (2, mod-2); fac[0]=fac[1]=1;  for(intI=2; i<maxn; ++i) fac[i]=i*fac[i-1]%MoD; INV[MAXN-1]=qpow (fac[maxn-1],mod-2);  for(inti=maxn-2; i>=0; i--) inv[i]=inv[i+1]* (i+1)%MoD;}structquery{intL,r,id; BOOL operator< (Constquery& p)Const    {        if(POS[L]==POS[P.L])returnr<P.R; returnl<P.L; }} q[maxn];inlinevoidADDN (intPOSL,intPOSR) {Res=(2*res%mod-comb (posl-1, POSR) +mod)%MoD;} InlinevoidAddM (intPOSL,intPOSR) {Res= (Res+comb (POSL,POSR))%MoD;} InlinevoidDeln (intPOSL,intPOSR) {Res= (Res+comb (posl-1, POSR))%mod*rev2%MoD;} InlinevoidDelm (intPOSL,intPOSR) {Res= (Res-comb (POSL,POSR) +mod)%MoD;}intMain () {intT,curl,curr; intBlock= (int) sqrt (1.0*MAXN);    Pre (); scanf ("%d",&T);  for(intI=1; i<=t;++i) {scanf ("%d%d", &q[i]. l,&Q[i].        R); Pos[i]=i/Block; Q[i].id=i; } sort (Q+1, q+t+1); Res=2; CurL=1, curr=1;  for(intI=1; i<=t;++i) { while(Curl<q[i]. L) Addn (+ +Curl,curr);  while(Curr<q[i]. R) AddM (curl,++CurR);  while(Curl>q[i]. L) Deln (curl--, CurR);  while(Curr>q[i]. R) Delm (curl,curr--); Ans[q[i].id]=Res; }     for(intI=1; i<=t;++i) printf ("%lld\n", Ans[i]); return 0;}
View Code

Harvest of Apples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.