Hdoj:6333-problem B. Harvest of Apples (combinatorial math + MO team algorithm + inverse)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=6333

Tips for solving problems:

  • This problem can be said to be very wonderful, first push combinatorial mathematical formula, one of the most important formula is CNm = Cmn-1 + Cm-1n-1 This formula know Yang Hui Triangle is understood, but a look found nothing to use. However, the formula can be used as a basis for further deduction.
  • Set snm = cn1 + CN2 + CN3 + ... CnM then continue using the basic formula above can be converted into
  • sn m-1 = sn m-cn m
    sn m+1 = sn m + Cn m+1
    sn+1 m = 2Sn m-cn m
    Sn m = (sn+1 m + Cn m)/2
  • These formulas do not seem to work, but know that the relationship between s can be transformed by the complexity of O (1), so that 105 queries can be taken offline, that is, with Mo team to maintain. Magic, to break the head is not to think of AH. The division in the inside of the operation with the inverse of the element to deal with, the inverse can be the first full and pre-processing out.

////┏┛┻━━━━━┛┻┓//┃┃//┃━┃//┃┳┛┗┳┃//┃┃//┃┻┃//┃┃//┗━┓┏━━━┛//┃┃ God beast bless//┃┃ Code No bug! //┃┗━━━━━━━━━┓//┃┣┓//┃┏┛//┗━┓┓┏━━━┳┓┏━┛//┃┫┫┃┫┫//┗━┻━┛┗━┻━┛#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5+ -;Const intMOD = 1e9+7; typedefLong Longll;structQuery {ll n, M, B, POS, ans;} Q[maxn];ll unit, ans, FAC[MAXN], INV[MAXN], T, Rev2;ll Quick_pow (ll A, ll b) {ll res=1;  while(b) {if(b&1) Res= Res * A%MOD; A= A * A%MOD; b>>=1; }    returnRes;}voidGET_FAC () {//preprocessing the factorial and inverse elementsfac[0] =1; fac[1] =1;  for(intI=2; i<maxn;i++) {Fac[i]= i * fac[i-1] %MOD; }    //Magical Operationinv[maxn-1] = Quick_pow (fac[maxn-1], mod-2);  for(inti=maxn-2; i>=0; i--) {Inv[i]= inv[i+1] * (i +1) %MOD; }}BOOLcmp1 (query A, query B) {if(A.B = =b.b)returna.m. <B.M; returnA.B <b.b;}BOOLcmp2 (query A, query B) {returnA.pos <B.pos;}voidinit () {Unit=sqrt (MAXN);    GET_FAC (); scanf ("%lld", &t);  for(intI=0; i<t;i++) {scanf ("%lld%lld", &AMP;Q[I].N, &q[i].m); Q[i]. B= q[i].n/Unit; Q[i].pos=i; } Rev2= Quick_pow (2, mod-2); Sort (q, q+T, CMP1);} ll C (ll N, ll m) {//get a combination of C (n, m)ll ans = fac[n] * inv[n-m]% MOD * Inv[m]%MOD; returnans;}voidAddl (ll L, ll R) {ans= ((2* ans% MOD) + mod-c (L, R))%MOD;}voidCutl (ll L, ll R) {ans= (ans + C (L-1, r)% MOD) * Rev2%MOD;}voidAddR (ll L, ll R) {ans= (ans + C (l, r+1)) %MOD;}voidCutr (ll L, ll R) {ans= (ans + mod-c (l, R))%MOD;}intMain () {init (); ll L=1, r =1; Ans=2;  for(intI=0; i<t;i++) {//offline MO Team processing        intL =Q[I].N; intR =q[i].m;  while(L < L) Addl (l++, R);  while(L > L) cutl (l--, R);  while(R > R) Cutr (L, r--);  while(R < R) AddR (L, r++); Q[i].ans=ans; } sort (q, q+T, CMP2);  for(intI=0; i<t;i++) {printf ("%lld\n", Q[i].ans); }    return 0;}

Hdoj:6333-problem B. Harvest of Apples (combinatorial math + MO team algorithm + inverse)

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.