首页 > 代码库 > NPY and girls
NPY and girls
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.
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(1≤T≤10).
For each test case,there are two integers n,m(0<n,m≤30000) 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<ai≤30000)
The following m lines,each line contains two integers l,r(1≤l≤r≤n),which indicates the interval NPY wants to visit.
For each test case,there are two integers n,m(0<n,m≤30000) 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<ai≤30000)
The following m lines,each line contains two integers l,r(1≤l≤r≤n),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
24 21 2 1 31 31 41 111 1
Sample Output
3121
思路:逆元+莫队算法;
假设每个区间中的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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。