cf 167.d( 多重集全排列 )

高中数学不好的娃实在是伤不起.


  一个公式:

设多重集 S={ne1 , n2×e2,...nk×ek},令an为S的全排列数,则

an=(n1+n2+...+nk)! / (n1!n2!...nk!) .


然后这题就好做了.

D. Dima and Two Sequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n).

Now Dima wants to count the number of distinct sequences of points of length n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.

Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (pn, qn) and (x1, y1), (x2, y2), ..., (xn, yn) distinct, if there is such i (1 ≤ i ≤ 2·n), that (pi, qi) ≠ (xi, yi).

As the answer can be rather large, print the remainder from dividing the answer by number m.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line containsn integers b1, b2, ..., bn (1 ≤ bi ≤ 109). The numbers in the lines are separated by spaces.

The last line contains integer m (2 ≤ m ≤ 109 + 7).

Output

In the single line print the remainder after dividing the answer to the problem by number m.

Sample test(s)
input
1
1
2
7
output
1
input
2
1 2
2 3
11
output
2
Note

In the first sample you can get only one sequence: (1, 1), (2, 1).

In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2)(1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

typedef __int64 ll;

struct node
{
    int x,y;
}g[1001000];
ll sum;
int n;
ll MOD;
int cmp(node x,node y)
{
    if(x.x!=y.x) return x.x<y.x;
    else return x.y<y.y;

}

void fuc(int s,int t)
{
    if(t-s==1) return ;
    
    ll cnt=0;
    for(int i=s;i<t;i++)
    {
        if(i==t-1)
        {
            cnt++;
            break;
        }
        if(g[i].y==g[i+1].y)
        {
            i++;
        }
        cnt++;
    }
    ll tmp=t-s-cnt;
    ll ttmp=1;
    for(int i=1;i<=t-s;i++)
    {
        ll ti=i;
        if(i%2==0&&tmp!=0)
        {
            ti=i/2;
            tmp--;
        }
        ttmp=(ttmp*ti)%MOD;
    }
    sum=(sum*ttmp)%MOD;
}
int main()
{
    sum=1;
    int cnt=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        g[cnt].x=tmp;
        g[cnt++].y=i;
    }
    for(int i=1;i<=n;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        g[cnt].x=tmp;
        g[cnt++].y=i;
    }
    scanf("%I64d",&MOD);
    sort(g,g+cnt,cmp);
    int tmp=g[0].x;
    int f=0;
    for(int i=1;i<=cnt;i++)
    {
        if(g[i].x!=tmp||i==cnt)
        {
            fuc(f,i);
            if(i==cnt) break;
            f=i;
            tmp=g[i].x;
        }
    }
    printf("%I64d",sum);
    return 0;
}
原文地址:https://www.cnblogs.com/chenhuan001/p/2911228.html