HDU-6333 problem B. Harvest of Apples Mo Team

Source: Internet
Author: User

HDU-6333

Test instructions: There are n different apples, you can use up to M, ask how many kinds of ways, multiple sets of data, groups and n,m are 1e5, so the table can not play.

Idea: This problem to use the combination of the properties of the number of S (n,m) from N to the maximum number of M-method total, is obviously C (n,0), C (n,1) ... C (N,m) and.

Obviously s (n,m+1) = S (n, m) + C (n,m+1);

Another equation is less obvious, s (n+1,m) = 2 * S (n,m)-C (N,M);

I also understand under the guidance of Wang Shinji.

Now that we know a group (N,M) can be transferred to (N+1,M) in a very fast time, (N-1,m), (n,m+1), (n,m-1), this time to think of Mo team. It is not magical to convert each group of N and M into the right and left endpoints of the interval.

How do you find the combination number C (n,m)? You can pre-preprocess the prefix factorial of n, each time except a bit, you can get, of course, because it is to take the meaning of the modulus, here in addition to multiply this number of inverse.

Another detail of this question is to update the right endpoint as N, in order to prevent the right endpoint from being smaller than the left endpoint, where n is smaller than M.

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<map>#include<Set>#include<queue>#include<list>#include<cstdlib>#include<iterator>#include<cmath>#include<iomanip>#include<bitset>#include<cctype>#include<iostream>using namespacestd;//#pragma COMMENT (linker, "/stack:102400000,102400000")//C + +#defineLson (L, Mid, RT << 1)#defineRson (mid + 1, R, RT << 1 | 1)#defineDebug (x) cerr << #x << "=" << x << "\ n";#definePB Push_back#definePQ Priority_queuetypedefLong Longll;typedef unsignedLong LongUll;typedef pair<ll, LL >Pll;typedef pair<int,int>PII;//priority_queue<int> Q;//This is a big root heap Q//priority_queue<int,vector<int>,greater<int> >q;//This is a little Gan q#defineFi first#defineSe Second//#define Endl ' \ n '#defineOKC Ios::sync_with_stdio (false); Cin.tie (0)#defineFT (A,B,C) for (int a=b; A <= C;++a)//used to press the line#defineREP (I, J, K) for (int i = j; i < K; ++i)//Priority_queue<int, Vector<int>, greater<int> >que;Constll mos =0x7FFFFFFF;//2147483647Constll nMOS =0x80000000;//-2147483648Const intINF =0x3f3f3f3f;Constll INFF =0x3f3f3f3f3f3f3f3f;// -Template<typename t>inline T read (t&x) {x=0;intf=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') f|= (ch=='-'), ch=GetChar ();  while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); returnx=f?-x:x;}//#define _DEBUG; //*//#ifdef _debugfreopen ("input","R", stdin);//freopen ("Output.txt", "w", stdout);#endif/*-----------------------Show Time----------------------*/                #defineBel (x) ((x-1)/b+1)Const intMAXN = 1e5+9; Const intB =233; Const intMOD = 1e9+7;                ll ANS[MAXN]; structNode {intn,m; intID;                } P[MAXN];                ll x, y; voidEXGCD (ll a,ll b) {if(b==0) {X=1; Y =0; return; } exgcd (B,a%b); LL TMP=X; X=Y; Y= tmp-a/b*Y; }                BOOLcmpConstNode &a,ConstNode &b) {                    if(BEL) = =Bel (B.M))returnA.N <B.N; returnBel (a.m.) <Bel (B.M);       } ll PM[MAXN],TWO,NY[MAXN]; //NY is preprocessing the inverse, not coming out will tle                voidinit () {pm[0] =1; EXGCD (1, MOD); ny[0] = (X + MOD)%MOD;  for(intI=1; i<maxn; i++) {Pm[i]= (pm[i-1] * i + MOD)%MOD;                        EXGCD (PM[I],MOD); Ny[i]= (X + MOD)%MOD; } EXGCD (2, MOD); both= (X + MOD)%MOD; } llGet(intNintx) {                        if(N-x <0)return 0; ll Res= (Pm[n] *ny[n-x])%MOD; Res= (res * ny[x])%MOD; returnRes; } ll sum=0; voidDel1 (intXintN) {Sum= (sum-Get(n,x) +mod)%MOD; }                voidADD1 (intXintN) {Sum= (sum +Get(n,x) +mod)%MOD; }                voidDel2 (intXintN) {Sum= ((sum +Get(n,x)) *two + MOD)%MOD; }                voidADD2 (intXintN) {Sum= (SUM *2-Get(n,x) +mod)%MOD; }intMain () {init (); intQ; scanf ("%d", &q);  for(intI=1; i<=q; i++) {scanf ("%d%d",&p[i].n,&p[i].m); P[i].id=i; } sort (P+1, p+1+q,cmp); intPL = p[1].M, PR = p[1].N;  for(intI=0; i<=pl; i++) {sum= (sum +Get(pr,i) + MOD)%MOD; } ans[p[1].id] =sum; //cout<< "* *" <<endl;                 for(intI=2; i<=q; i++){                     while(PR < P[I].N) ADD2 (PL,PR), pr++;//This is to update the right interval as n to prevent m>n;                     while(pr > P[I].N) pr--, Del2 (PL,PR);  while(PL < P[I].M) pl++, Add1 (PL,PR);  while(pl > p[i].m) del1 (PL,PR), pl--; Ans[p[i].id]= sum%MOD; }                 for(intI=1; i<=q; i++) {printf ("%lld\n", Ans[i]); }        return 0;}
HDU6333

HDU-6333 problem B. Harvest of Apples Mo Team

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.