NPY and girls
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 792 Accepted Submission(s): 281
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
2 4 2 1 2 1 3 1 3 1 4 1 1 1 1 1
Sample Output
3 12 1
Source
题意:人生赢家有n个女朋友,这些女朋友有可能来自同一个班级,也有可能不同班级,现在给你1~n号女朋友所属的班级,有m个询问
每个询问给你一个区间,比如[L,R],表示人生赢家想约L~R号的女生,问人生赢家一共有几种方式可以约
比如 1 2 1 3 区间[1,3]一共有三种方式 分别是1 2 1,1 1 2,2 1 1
1 2 3 区间[1,3]有6种,1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1
思路:这里的组合方式是(R-L+1)!/C1!*C2!*...*Cx!(直接把C(m,c1)*(m-c1,c2)*...*(cn,cn)展开就可得到)
Ci分别为有女生的班级的数量的阶乘
然后把所有的询问保存下来,用莫队算法去求即可
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 52000
#define mod 1000000007
int a[N],p[N];
long long num[N];
long long inv[N];
long long ans;
long long l[N];
struct Node
{
int l,r,id;
} q[N];
bool cmp(Node a,Node b)
{
if(p[a.l]==p[b.l])
return a.r<b.r;
return p[a.l]<p[b.l];
}
long long pow_mod(long long a,long long n)
{
long long ans=1;
while(n)
{
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n>>=1;
}
return ans;
}
long long getinv(long long n)
{
return pow_mod(n,mod-2);
}
int main()
{
int T;
int n,m;
scanf("%d",&T);
for(long long i=1; i<N; i++)
inv[i]=getinv(i);
while(T--)
{
memset(num,0,sizeof(num));
scanf("%d %d",&n,&m);
int bk=ceil(sqrt(1.0*n));
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
p[i]=(i-1)/bk;
}
for(int i=0; i<m; i++)
{
scanf("%d %d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q,q+m,cmp);
int pl=1,pr=0;
ans=1;
for(int i=0; i<m; i++)
{
int id=q[i].id;
if(pr<q[i].r)
{
for(int j=pr+1; j<=q[i].r; j++)
{
num[a[j]]++;
ans=ans*(j-pl+1)%mod*inv[num[a[j]]]%mod;
}
}
else
{
for(int j=pr; j>q[i].r; j--)
{
ans=ans*inv[j-pl+1]%mod*num[a[j]]%mod;
num[a[j]]--;
}
}
pr=q[i].r;
if(pl<q[i].l)
{
for(int j=pl; j<q[i].l; j++)
{
ans=ans*inv[pr-j+1]%mod*num[a[j]]%mod;
num[a[j]]--;
}
}
else
{
for(int j=pl-1; j>=q[i].l; j--)
{
num[a[j]]++;
ans=ans*(pr-j+1)%mod*inv[num[a[j]]]%mod;
}
}
pl=q[i].l;
l[id]=ans;
}
for(int i=0; i<m; i++)
printf("%lld\n",l[i]);
}
return 0;
}