• HDU 5145 NPY and girls 莫队+逆元


    NPY and girls


    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
     
    题意: 
     
      给你n个数,m次询问,每次询问l,r之间有多少种排列
     
    题解:  
     
      典型的莫队
      只不过这个数有点大
      需要预处理n这么大 的 逆元
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    using namespace std;
    typedef long long ll;
    const int N = 5e4+20, M = 4e4+10, mod = 1e9+7, inf = 0x3f3f3f3f;
    
    int belong[N], a[N], m, n, T;
    struct ss{int l,r,id;}Q[N];
    bool operator < (ss s1,ss s2) {
        if(belong[s1.l] == belong[s2.l]) return s1.r < s2.r;
        else return belong[s1.l] < belong[s2.l];
    }
    ll quick_pow(ll x,ll p) {
        if(!p) return 1;
        ll ans = quick_pow(x,p>>1);
        ans = ans*ans%mod;
        if(p & 1) ans = ans*x%mod;
        return ans;
    }
    
    ll inv(ll x)
    {
        ll mo = mod;
        return quick_pow(x,mo-2);
    }
    
    ll an[N], Inv[N];
    ll mp[N];
    int main()
    {
        for(ll i = 1; i <= 30000; ++i) Inv[i] = inv(i);
        scanf("%d",&T);
        while(T--) {
            scanf("%d%d",&n,&m);
            for(int i = 1; i <= n; ++i) scanf("%d",&a[i]);
            int t = sqrt(n);
            for(int i = 1; i <= n; ++i) belong[i] = (i-1) / t + 1;
            for(int i = 1; i <= m; ++i) {
                scanf("%d%d",&Q[i].l,&Q[i].r);Q[i].id = i;
            }
            memset(mp,0,sizeof(mp));
            sort(Q + 1, Q + m + 1);
            ll l = 1, r = 0, len = 0;
            ll ans = 1;
            for(int i = 1; i <= m; ++i) {
                for(;r<Q[i].r;r++) {
                    ++len;
                    ++mp[a[r+1]];
                    ans = ans * len % mod;
                    ans = ans * Inv[mp[a[r+1]]] % mod;
                }
                for(;l>Q[i].l;l--) {
                    ++len;
                    ++mp[a[l-1]];
                    ans = ans * len % mod;
                    ans = ans * Inv[mp[a[l-1]]] % mod;
                }
                for(;r>Q[i].r;r--) {
                    --len;
                    --mp[a[r]];
                    ans = ans * Inv[len+1] % mod;
                    ans = ans * (mp[a[r]] + 1ll) % mod;
                }
                for(;l<Q[i].l;l++) {
                    --len;
                    --mp[a[l]];
                    ans = ans * Inv[len+1] % mod;
                    ans = ans * (mp[a[l]] + 1ll) % mod;
                }
                an[Q[i].id] = ans;
            }
            for(int i = 1; i <= m; ++i) printf("%I64d
    ",an[i] % mod);
        }
    }
  • 相关阅读:
    KingPaper初探Java之初学者编码遇到的问题
    KingPaper初探redis之redis数据类型解析(String类型)
    KingPaper初探Java之面向对象对象的声名和实例化(一)
    KingPaper初探百度应用之百度地图API
    MYSQL之用户授权
    nginx入门到精通目录
    nginx入门篇负载均衡策略
    nginx入门篇功能特性
    开博啦
    ubuntu14.04下pycharm的安装及破解
  • 原文地址:https://www.cnblogs.com/zxhl/p/5710346.html
Copyright © 2020-2023  润新知