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. vue项目怎么运行

    因为要做毕业设计 ,准备用vue来做前端的界面.发现不知如何运行vue的项目,很尴尬.通过查阅网上教程,因此把环境搭建过程记录下来,以备不时之需.   言归正传~~   首先,列出来我们需要的东西:  ...

  2. VS2005、VS2008中的快捷键、组合键大全

    Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...

  3. laravel 可用验证规则

    accepted 验证的字段必须为 yes. on. 1.或 true.这在确认服务条款是否同意时相当有用. active_url 相当于使用了 PHP 函数 dns_get_record,验证的字段 ...

  4. PHPCMS登录后不是进入会员中心而是转入登录前页最新代码

    phpcms比如会员在登录前是停留在下载页面的,但是下载页面是要求会员登录后才能下载,所以会员就有这个登陆过程,但是一般的会员系统是登录进会员中心的,就会有点体验不好  这里教大家修改下 能达到登录后 ...

  5. 【bzoj4736/uoj#274】[清华集训2016]温暖会指引我们前行 语文题+LCT

    题目描述 http://uoj.ac/problem/274 题解 语文题+LCT 对于这种语文题建议还是自己读题好一些... 读懂题后发现:由于温度互不相同,最大生成树上的路径必须走(不走的话温度大 ...

  6. 【codevs1404】字符串匹配 KMP

    题目描述 给你两个串A,B,可以得到从A的任意位开始的子串和B匹配的长度.给定K个询问,对于每个询问给定一个x,求出匹配长度恰为x的位置有多少个.N,M,K<=200000 输入 第一行三个数 ...

  7. 前端基础:JavaScript对象

    JavaScript对象 在JavaScript中除了null和undefined以外,其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,数字型.布尔型.字符串.日期.数字和正则表达式. ...

  8. BZOJ4710 JSOI2011分特产(容斥原理+组合数学)

    显然可以容斥去掉每人都不为空的限制.每种物品分配方式独立,各自算一个可重组合乘起来即可. #include<iostream> #include<cstdio> #includ ...

  9. [SCOI2007]组队 差分

    题面:[SCOI2007]组队 题解: 一开始固定H然后找性质找了很久也没有找到任何有用的东西...... 然后大佬告诉我一个神奇的方法... 首先我们化一波式子: 设$H$表示高度的最小值,$V$表 ...

  10. java.util.Stack类简介(栈)

    Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用 ...