题目链接:

http://codeforces.com/problemset/problem/272/D

D. Dima and Two Sequences

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> 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 2·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), ..., (p2·n, q2·n) and (x1, y1), (x2, y2), ..., (x2·n, y2·n) 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.
#### 输入
> The first line contains integer n (1 ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109). The third line contains n 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).
#### 输出
> In the single line print the remainder after dividing the answer to the problem by number m.
####样例输入
> 1
> 1
> 2
> 7

样例输出

1

题意

给两个长度为n的序列,让你把它们合成2*n的非递减序列,问有多少种可能(两个数完全相同当且仅当他们的值相等,且在a,b中出现的位置相同。

题解

就考虑下相同的数中有多少对数是完全相同的,然后总的阶乘除相同的对数,由于取模m比较特别,所以2不能直接求逆,需要单独把2拉出来考虑下。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9; const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2e5+20; LL fac[maxn],two[maxn];
int num[maxn];
int n,m;
VPII arr; void init(){
clr(num,0);
fac[0]=fac[1]=1;
for(int i=2;i<maxn;i++){
int tmp=i;
while(tmp%2==0){
tmp/=2; num[i]++;
}
fac[i]=tmp*fac[i-1]%m;
num[i]+=num[i-1];
} two[0]=1;
rep(i,1,maxn) two[i]=two[i-1]*2%m;
} int main() {
scf("%d",&n);
rep(i,0,n){
int x; scf("%d",&x);
arr.pb(mkp(x,i+1));
}
rep(i,0,n){
int x; scf("%d",&x);
arr.pb(mkp(x,i+1));
}
scf("%d",&m);
init(); sort(all(arr));
LL ans=1;
rep(i,0,arr.sz()){
int ed=i;
while(ed<arr.sz()&&arr[ed].X==arr[i].X) ed++; ed--; int cnt=0;
for(int j=i;j<ed;j++){
if(arr[j]==arr[j+1]) cnt++;
} ///这里有可能re
ans=ans*fac[ed-i+1]%m*two[num[ed-i+1]-cnt]%m; i=ed;
} prf("%I64d\n",ans); return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合的更多相关文章

  1. Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合

    C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. Codeforces Round #167 (Div. 2)

    C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...

  3. Codeforces Round #167 (Div. 1 + Div. 2)

    C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...

  4. Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想

    D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  5. Codeforces Round #208 (Div. 2) 358D Dima and Hares

    题目链接:http://codeforces.com/problemset/problem/358/D 开始题意理解错,整个就跪了= = 题目大意:从1到n的位置取数,取数的得到值与周围的数有没有取过 ...

  6. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

    C. Dima and Salad   Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...

  8. Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)

                                                     D. Dima and Lisa Dima loves representing an odd num ...

  9. Codeforces Round #208 (Div. 2) A.Dima and Continuous Line

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

随机推荐

  1. Storm wordcount Read from file

    source code: package stormdemo; import java.io.BufferedReader; import java.io.BufferedWriter; import ...

  2. 五个有用的jquery小技巧

    1.禁用鼠标右键 $(document).ready(function() { $(document).bind("contextmenu", function(e) { retu ...

  3. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  4. ZOJ 2674 Strange Limit

    欧拉函数. #include<iostream> #include<stdio.h> #include<string.h> #include<algorith ...

  5. C# explicit与implicit

    1.它们解决什么问题? 考虑下面的需求,Person类有个字段age.我想使用Person p = (Person) 18 来创建一个age为18的Person对象,怎么办? 更进一步,我想使用Per ...

  6. MonoDevelop Debug Unity

    环境 Unity 4.3.x MonoDevelop 4.0.1 资料 更新Unity4.3.X之后的版本,MonoDevelop的版本也进行了升级,IDE的界面发生了比较大的改变. 查阅了Unity ...

  7. Android Studio下载与安装

    Android Studio下载与安装 1 2 3 4 5 分步阅读 百度经验:jingyan.baidu.com 自从Google宣布Android Studio将取代Eclipse,正式成为官方集 ...

  8. MySQL 约束的讲解

    MySQL 约束 作用:保证数据的完整性和一致性按照约束的作用范围分为:表级约束和行级约束.常见的约束类型包括: Not null(非空约束) Primary key (主键约束) Unique ke ...

  9. ESLint 检查代码质量

    利用 ESLint 检查代码质量 其实很早的时候就想尝试 ESLint 了,但是很多次都是玩了一下就觉得这东西巨复杂,一执行检查就是满屏的error,简直是不堪入目,遂放弃.直到某天终于下定决心深入看 ...

  10. HashMap 中的 entrySet()使用方法 2016.12.28

    package map; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import ...