hdu5145NPY and girls

NPY and girls

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


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
 

Source

BestCoder Round #22


题目大意:

每组两个数 n m分别表示人数n和询问次数m

之后n个数 表示n个女孩所在教室

对于m次询问 每次一个[L,R]1 <= L <= R <= n) 问为了访问到每个女孩 访问教室的方案有几种(会出现几个女孩在一个教室的情况 但每次访问教室只能找一个女孩 同一个编号的教室是相同的)


思路:

莫队+逆元

设将要进入的人的教室号是i,已经进去的人有y,num[x]表示的是当前这个区间[L,R]中在x教室的有多少人,则当这个人进去之后总的方案数可这样思考:

总共有y+1个位置,先放num[i]+1个教室,C(y+1,num[i]+1),原来先放num[i]个教师,C(y,num[i]),所以会扩大C(y+1,num[i]+1)/C(y,num[i])

每个人出去的时候也是同样的考虑方法

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=40100;
const __int64 MOD=1e9+7;
int n,m;
int num[maxn],a[maxn];
__int64 ans[maxn],c[maxn];
struct node{
    int l,r,id;
}Q[maxn];
int unit;

bool cmp(node u,node v){
    if(u.l/unit!=v.l/unit)
        return u.l/unit<v.l/unit;
    return u.r<v.r;
}

__int64 inv(__int64 a,__int64 M){
    if(a==1)
        return 1;
    return inv(M%a,M)*(M-M/a)%M;
}

void solve(){
    int L=1,R=0;
    __int64 temp=1;
    for(int i=1;i<=m;i++){
        while(R>Q[i].r){
            temp=temp*num[a[R]]%MOD*c[R-L+1]%MOD;
            num[a[R]]--;
            R--;
        }
        while(R<Q[i].r){
            R++;
            num[a[R]]++;
            if((R-L+1)%num[a[R]]==0)
                temp=temp*(R-L+1)/num[a[R]]%MOD;
            else
                temp=temp*(R-L+1)%MOD*c[num[a[R]]]%MOD;
        }
        while(L>Q[i].l){
            L--;
            num[a[L]]++;
            if((R-L+1)%num[a[L]]==0)
                temp=temp*(R-L+1)/num[a[L]]%MOD;
            else
                temp=temp*(R-L+1)%MOD*c[num[a[L]]]%MOD;
        }
        while(L<Q[i].l){
            temp=temp*num[a[L]]%MOD*c[R-L+1]%MOD;
            num[a[L]]--;
            L++;
        }
        ans[Q[i].id]=temp;
    }
}

int main(){
    int _;
    for(int i=1;i<=30000;i++)
        c[i]=inv(i,MOD);
    scanf("%d",&_);
    while(_--){
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&Q[i].l,&Q[i].r);
            Q[i].id=i;
        }
        unit=(int)sqrt(n);
        sort(Q+1,Q+m+1,cmp);
        solve();
        for(int i=1;i<=m;i++)
            printf("%I64d\n",ans[i]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值