码迷,mamicode.com
首页 > 其他好文 > 详细

NPY and girls

时间:2016-11-04 13:59:45      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:void   ret   oom   miss   求逆   strong   str   content   integer   

NPY and girls

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1155    Accepted Submission(s): 401


Problem Description
NPY‘s girlfriend blew him out!His honey doesn‘t love him any more!However, he has so many girlfriend candidates.Because there are too many girls and for the convenience of management, NPY numbered the girls from 1 to n.These girls are in different classes(some girls may be 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 into 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 to learn how to use excavator),he wonders how many different ways there can be in which he can visit his girls.The different ways are different means he visits these classrooms in different order.
 

 

Input
The first line contains the number of test cases T(1T10).
For each test case,there are two integers n,m(0<n,m30000) in the first line.N is the number of girls,and M is 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<ai30000)
The following m lines,each line contains two integers l,r(1lrn),which indicates the interval NPY wants to visit.
 

 

Output
For each visit,print how many ways can NPY visit his girls.Because the ans may be too large,print the ans mod 1000000007.
 

 

Sample Input
2 4 2 1 2 1 3 1 3 1 4 1 1 1 1 1
 

 

Sample Output
3 12 1
 
思路:逆元+莫队算法;
假设每个区间中的a1,a2,a3...an,an表示区间种类为n的个数,那么区间长度为m的话那么答案就是m!/(a1!*a2!...*an!);
然后用莫队求下,求模时费马小定理求逆元;
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<math.h>
  6 #include<queue>
  7 #include<stack>
  8 using namespace std;
  9 typedef long long LL;
 10 const LL mod = 1e9+7;
 11 LL quick(LL n,LL m);
 12 typedef struct node
 13 {
 14     int l;
 15     int r;
 16     int id;
 17 } ss;
 18 ss ask[300005];
 19 LL cnt[300005];
 20 int ans[300005];
 21 LL N[300005];
 22 LL Ni[300005];
 23 LL ck[300005];
 24 bool cmp(node p,node q)
 25 {
 26     return p.l < q.l;
 27 }
 28 bool cmp1(node p,node q)
 29 {
 30     return p.r < q.r;
 31 }
 32 void slove_mo(int n,int m);
 33 int main(void)
 34 {
 35     int T;
 36     int i,j;
 37     N[0] = 1;
 38     Ni[0] = 1;
 39     for(i = 1; i <= 300000; i++)
 40     {
 41         N[i] = N[i-1]*(LL)i%mod;
 42         Ni[i] = quick(N[i],mod-2);
 43     }
 44     scanf("%d",&T);
 45     while(T--)
 46     {
 47         int n,m;
 48         scanf("%d %d",&n,&m);
 49         memset(cnt,0,sizeof(cnt));
 50         for(i = 1; i <= n; i++)
 51         {
 52             scanf("%d",&ans[i]);
 53         }
 54         for(i = 0; i < m; i++)
 55         {
 56             scanf("%d %d",&ask[i].l,&ask[i].r);
 57             ask[i].id = i;
 58         }
 59         sort(ask,ask+m,cmp);
 60         int ak = sqrt(1.0*n)+1;
 61         int v = ak;
 62         int id = 0;
 63         for(i = 0; i < m; i++)
 64         {
 65             if(ask[i].l > v)
 66             {
 67                 v+=ak;
 68                 sort(ask+id,ask+i,cmp1);
 69                 id = i;
 70             }
 71         }
 72         sort(ask+id,ask+m,cmp1);
 73         slove_mo(n,m);
 74         for(i = 0; i < m; i++)
 75         {
 76             printf("%lld\n",ck[i]);
 77         }
 78     }
 79     return 0;
 80 }
 81 void slove_mo(int n,int m)
 82 {
 83     int xl = ask[0].l;
 84     int xr = ask[0].r;
 85     int i,j;
 86     LL sum = xr-xl+1;
 87     LL ak = 1;
 88     for(i = xl; i <= xr; i++)
 89     {
 90         ak = ak*Ni[cnt[ans[i]]]%mod;
 91         cnt[ans[i]]++;
 92         ak = ak*N[cnt[ans[i]]]%mod;
 93     }
 94     sum = N[sum]*quick(ak,mod-2)%mod;
 95     ck[ask[0].id] = sum;
 96     for(i = 1; i < m; i++)
 97     {
 98         sum = ask[i].r-ask[i].l+1;
 99         while(xr > ask[i].r)
100         {
101             ak = ak*Ni[cnt[ans[xr]]]%mod;
102             cnt[ans[xr]]--;
103             ak = ak*N[cnt[ans[xr]]]%mod;
104             xr--;
105         }
106         while(xr < ask[i].r)
107         {
108             xr++;
109             ak = ak*Ni[cnt[ans[xr]]]%mod;
110             cnt[ans[xr]]++;
111             ak = ak*N[cnt[ans[xr]]]%mod;
112         }
113         while(xl < ask[i].l)
114         {
115             ak = ak*Ni[cnt[ans[xl]]]%mod;
116             cnt[ans[xl]]--;
117             ak = ak*N[cnt[ans[xl]]]%mod;
118             xl++;
119         }
120         while(xl > ask[i].l)
121         {
122             xl--;
123             ak = ak*Ni[cnt[ans[xl]]]%mod;
124             cnt[ans[xl]]++;
125             ak = ak*N[cnt[ans[xl]]]%mod;
126         }
127         ck[ask[i].id] = N[sum]*quick(ak,mod-2)%mod;
128     }return ;
129 }
130 LL quick(LL n,LL m)
131 {
132     LL ask = 1;
133     n%=mod;
134     while(m)
135     {
136         if(m&1)
137             ask = ask*n%mod;
138         n = n*n%mod;
139         m>>=1;
140     }
141     return ask;
142 }

 

NPY and girls

标签:void   ret   oom   miss   求逆   strong   str   content   integer   

原文地址:http://www.cnblogs.com/zzuli2sjy/p/6029790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!