time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning. The balls are numbered from 1 to n (by the demand of the inventory commission).

Figure 1. The initial position for n = 5.

After receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped.

Figure 2. The example of a throw.

In this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2.

Note that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the "ball order". The ball order is a sequence of n numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise.

Input

The first line contains a single number n — the number of students in the line and the number of balls. The next line contains exactly n space-separated integers. Each number corresponds to a student in the line (the i-th number corresponds to the i-th from the left student in the line) and shows the number of throws he can participate in.

The input limits for scoring 30 points are (subproblem D1):

  • 1 ≤ n ≤ 10.

The input limits for scoring 70 points are (subproblems D1+D2):

  • 1 ≤ n ≤ 500.

The input limits for scoring 100 points are (subproblems D1+D2+D3):

  • 1 ≤ n ≤ 1000000.
Output

The output should contain a single integer — the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (109 + 7).

Examples
Input
5
1 2 2 1 2
Output
120
Input
8
1 2 2 1 2 1 1 2
Output
16800

数学问题 脑洞题 组合数

有的人可以交换两次,有的人可以交换一次。

脑洞一下可以注意到只要交换的方案不同,最终的排列就不同。

如果所有人都只能交换一次,设f[i]表示有i个只能交换一次的人的交换方案数。

f[i]=f[i-1]+(i-1)*f[i-2]  (自己跟自己玩,或者找一个人换)

考虑能交换两次的人,若这类人有a个,那么他们可选择的交换方案共有$C(n,a)*A(a,a)=n*(n-1)*(n-2)*...*(n-a+1)$种,交换完后正好剩下n-a个只能交换一次的,所以再乘上f[n-a]即可

具体考虑方法的话……假设x和y交换,如果x剩2,y剩1,那么剩下一个1一个0;如果x剩2,y剩2,换了以后剩下两个1,但y还是要当做能换两次的,去考虑和其他某个人交换,这样的交换关系形成一条链(这条链的起始点是一个原本只能换一次的人),于是连续的一串2会消到只剩下一个1

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
const int mod=1e9+;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[mxn];
int f[mxn];
int main(){
// freopen("in.txt","r",stdin);
int i,j,cnt=;
n=read();
for(i=;i<=n;i++){a[i]=read();if(a[i]==)cnt++;}
f[]=;f[]=;f[]=;
for(i=;i<=cnt;i++)f[i]=((LL)f[i-]+(LL)f[i-]*(i-)%mod)%mod;
for(i=n;i>cnt;i--)f[cnt]=(LL)f[cnt]*i%mod;
printf("%d\n",f[cnt]);
return ;
}

CodeForces 316D3 PE Lesson的更多相关文章

  1. CodeForces 139C Literature Lesson(模拟)

    这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封 ...

  2. Codeforces Round #586 (Div. 1 + Div. 2) C. Substring Game in the Lesson

    链接: https://codeforces.com/contest/1220/problem/C 题意: Mike and Ann are sitting in the classroom. The ...

  3. Codeforces Round #430 D. Vitya and Strange Lesson

    Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of numbers is ...

  4. Codeforces 37D Lesson Timetable - 组合数学 - 动态规划

    题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...

  5. D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...

  6. Codeforces Round #430 (Div. 2) Vitya and Strange Lesson

    D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...

  7. codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)

    题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...

  8. Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点

    题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...

  9. Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson

    因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...

随机推荐

  1. Java中Collection和Collections的区别(转载)

    转载来源:http://www.cnblogs.com/dashi/p/3597937.html 1.java.util.Collection 是一个集合接口(集合类的一个顶级接口).它提供了对集合对 ...

  2. 多线程Worker初尝试

    多线程这个概念,不知道听了多少遍.但是真滴没有去实操过. 前几天看视频听到作者说道关注技术本身,而不是总写业务代码.这几天依然思考着这个问题.于是从头开始重现了html文件的堵塞问题,重现了html文 ...

  3. 【Docker 命令】- run命令

    docker run :创建一个新的容器并运行一个命令 语法: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: -a stdin: 指 ...

  4. How To Disable MacBook ProTrackpad

    How To Disable MacBook Pro Trackpad how to close macbook pro touchpad? https://www.wikihow.com/Chang ...

  5. SQL SERVER 存储过程中SELECT 返回值如何赋值给变量

    今天在处理一个问题时,使用到一个存储过程,是用于更新并获取最新ID的.在使用过程中,需要获取到这个ID并赋值给变量,结果用EXEC @ID = 存储过程的方式获取失败了.具体情况如下: 为了还原整个情 ...

  6. servlet的service特性就是http协议的特性 即连接完就断开

    servlet的service特性就是http协议的特性 即连接完就断开

  7. BZOJ 1875 HH去散步(矩阵快速幂)

    题意: 给定一张无向图,每条路的长度都是1,没有自环,可能有重边,给定起点与终点,求从起点走t步到达终点的方案数. 每一步走的时候要求不能走上一条刚刚走的路. 解析: 显然需要搞出个矩阵之后矩乘. 然 ...

  8. CodeForces 632E Thief in a Shop

    题意:给你n种物品,每种无限个,问恰好取k个物品能组成哪些重量.n<=1000,k<=1000,每种物品的重量<=1000. 我们搞出选取一种物品时的生成函数,那么只要对这个生成函数 ...

  9. IntellIJ IDEA 配置 Maven

    一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本 2.解压1所下载文件,E:\apache-maven-3.5.4 3.配置Maven环境变量 a. MAVEN_HOM ...

  10. [洛谷P5174]圆点

    题目大意:给你$R(R\leqslant10^{14})$,求:$$\sum\limits_{x\in\mathbb{Z}}\sum\limits_{y\in\mathbb{Z}}[x^2+y^2\l ...