Cf 167.d( full arrangement of multiple sets)

Source: Internet
Author: User

A poor high school math really cannot afford to hurt.

 

One formula:

Set multiple duplicatesS = {n1 × e1, n2 × e2 ,..., Nk × ek}, so that an is the full number of S, then

An = (n1 + n2 +... + nk )! /(N1! N2 !... Nk !) .

 

Then this question is easy to solve.

 

D. Dima and Two Sequencestime 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, period 1), period ),(A2, second 2), second..., second ),...,(AN, Bytes,N) And sequence (B1, period 1), period ),(B2, second 2), second..., second ),...,(BN, Bytes,N).

Now Dima wants to count the number of distinct sequences of points of length 2 ·NThat can be assembled from these sequences, such thatX-Coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences shocould be used exactly once in the assembled sequence.

Dima considers two assembled sequences (P1, bytes,Q1), round ),(P2, bytes,Q2), wait..., wait ),...,(P2 ·N, Bytes,Q2 ·N) And (X1, bytes,Y1), round ),(X2, bytes,Y2), wait..., wait ),...,(X2 ·N, Bytes,Y2 ·N) Distinct, if there is suchI(1 digit ≤ DigitILimit ≤ limit 2 ·N), That (PI, Bytes,QI) ≠ = (XI, Bytes,YI).

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

Input

The first line contains integerN(1 digit ≤ DigitNLimit ≤ limit 105). The second line containsNIntegersA1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limit 109). The third line containsNIntegersB1, bytes,B2, middle..., middle ,...,BN(1 digit ≤ DigitBILimit ≤ limit 109). The numbers in the lines are separated by spaces.

The last line contains integerM(2 cores ≤ CoresMLimit ≤ limit 109 rows + rows 7 ).

Output

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

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, latency 1), latency (2, latency 1 ).

In the second sample you can get such sequences: (1, interval 1), values (2, interval 2), values (2, interval 1), values (3, limit 2); (1, limit 1), round (2, limit 1), round (2, limit 2), round (3, limit 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;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.