CodeForces 316D3 PE Lesson

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

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #define LL long long
 7 using namespace std;
 8 const int mod=1e9+7;
 9 const int mxn=1000010;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14     return x*f;
15 }
16 int n;
17 int a[mxn];
18 int f[mxn];
19 int main(){
20 //    freopen("in.txt","r",stdin);
21     int i,j,cnt=0;
22     n=read();
23     for(i=1;i<=n;i++){a[i]=read();if(a[i]==1)cnt++;}
24     f[0]=1;f[1]=1;f[2]=2;
25     for(i=3;i<=cnt;i++)f[i]=((LL)f[i-1]+(LL)f[i-2]*(i-1)%mod)%mod;
26     for(i=n;i>cnt;i--)f[cnt]=(LL)f[cnt]*i%mod;
27     printf("%d\n",f[cnt]);
28     return 0;
29 }

 

posted @ 2017-05-03 22:12  SilverNebula  阅读(231)  评论(0编辑  收藏  举报
AmazingCounters.com