Hdu5145npy and girls-mo team algorithm

Source: Internet
Author: User

Problem Descriptionnpy ' s girlfriend blew him out! His honey doesn ' t love him any more! However, he had so many girlfriend candidates. Because There is too many girls and for the convenience of management, NPY numbered the girls from 1 to N.these girls is In different classes (some girls is in the same class). And the i-th girl is in class AI. NPY wants to visit his girls frequently. Each time he visits some girls numbered consecutively from L to R in some order. He can only visit one girl every time he goes to a classroom,otherwise the girls may fight with each other (-_-!). And he can visit the class in any order.
Here comes the problem, (NPY doesn ' t want-learn how-to use excavator), he wonders how many different ways there can is in Which he can visit his girls. The different ways is different means he visits these classrooms in different order.

Inputthe First line contains the number of test casesT(1≤t≤) .
For each test case,there is integersn,m(0<n,m≤30000) In the first line. N is the number of Girls,and M are the number of times that NPY want to visit his girls.
The following single line contains N integers,a1,a2,a3,... ,an , which indicates the class number of each girl.(0<ai≤30000)
The following M Lines,each line contains and integers l,R(1≤l≤r≤n) , which indicates the interval NPY wants to visit.

Outputfor each Visit,print how many ways can NPY visit his girls. Because the ANS may too large,print the ANS mod 1000000007.

Sample Input24 21 2 1 31 31 41 111 1

Sample Output3121 Test instructions: Give a sequence of length n, then m query, interval [Li,ri] The number of categories arranged in the topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=5145 is to find the whole permutation of the number of intervals in the range of the same number in the interval. Because of the number of more, you can use the multiplication of inverse element. Multiplication inverse: axb≡1 (mod m) is called B is a multiplication inverse of M 1. is to seek AXB-KXM = 1. It is known that ax + by = t must have a solution when T = gcd (A, B) xk, K is an integer. So the original equation has a solution only when a and M coprime. This can be used EXGCD, any set of conditions to satisfy the solution can be. 2. By Fermat theorem x^ (m-1) ≡1 (mod m), xxx^ (m-2) ≡1 (mod m) so x^ (M-2) mop m is the multiplication inverse of X. The rest of the interval query using the MO team algorithm to divide an interval into sqrt (n) blocks, and then press sqrt (L), R sort. The time is approximately sqrt (n) * (n).
#include <cstdio>#include<iostream>#include<cmath>#include<cstring>#include<cstdlib>#include<queue>#include<algorithm>using namespaceStd;typedefLong LongLL;ConstLL mod = 1e9+7;ConstLL MAXN =33333; LL n,m; LL ANS[MAXN]; LL COLOR[MAXN]; LL CNT[MAXN]; LL D[MAXN]; LL ND[MAXN];structnode{LL l; LL R; LL ID;} NODE[MAXN]; LL Unit; ll Quick (ll A,ll b) {ll sum=1;  while(b) {if(b&1) sum*=A; Sum%=MoD; A*=a;a%=MoD; b>>=1; }    returnsum;}voidinit () {d[0] = nd[0] =1;  for(LL i =1; i<=maxn;i++) D[i] = i*d[i-1] %MoD;  for(LL i =1; i<=maxn;i++) Nd[i] = Quick (d[i],mod-2);}intcmpConstNode &a,ConstNode &b) {    if(A.l/unit = = B.l/unit)returnA.R <B.R; returna.l/unit<b.l/Unit;}voidGao () {LL temp=1; LL L=1; LL r =0;//because the initial interval L >R, so the following loop from R, if the query interval is not starting from 1 will appear l after a period, R repeats through this paragraph. memset (CNT,0,sizeof(CNT));  for(LL i =0; i<m;i++){         while(r>NODE[I].R) {Temp*= Nd[cnt[color[r]]; temp%=MoD; CNT[COLOR[R]]--; Temp*= d[cnt[color[r]]];temp%=MoD; R--; }         while(r<NODE[I].R) {R++; Temp*=nd[cnt[color[r]]];temp%=MoD; CNT[COLOR[R]]++; Temp*= d[cnt[color[r]]];temp%=MoD; }         while(l<NODE[I].L) {Temp*= Nd[cnt[color[l]]; Temp%=MoD; CNT[COLOR[L]]--; Temp*= D[cnt[color[l]]; Temp%=MoD; L++; }         while(l>NODE[I].L) {L--; Temp*= Nd[cnt[color[l]]; Temp%=MoD; CNT[COLOR[L]]++; Temp*= d[cnt[color[l]]];temp%=MoD; } Ans[node[i].id]= d[r-l+1] * Quick (temp,mod-2) %MoD; }}intMain () {LL T;    Init (); CIN>>T;  while(t--) {cin>>n>>m;  for(LL i =1; i<=n;i++) scanf ("%i64d",&Color[i]);  for(LL i =0; i<m;i++) {scanf ("%i64d%i64d",&node[i].l,&NODE[I].R); Node[i].id=i; } Unit=(LL) sqrt (n); Sort (Node,node+m,cmp);        Gao ();  for(LL i =0; i<m;i++) {printf ("%i64d\n", Ans[i]); }    }    return 0;}

Hdu5145npy and girls-mo team algorithm

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.