CodeForces - 316D3 PE Lesson

Discription

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

51 2 2 1 2

Output

120

Input

81 2 2 1 2 1 1 2

Output

16800

比较基础的计数题。    首先称一个只能换一次球的人为 懒人,那么我们可以发现一个排列合法 当且仅当 -> 这个排列的置换中,每个环的懒人数都要<=2    考虑置换中的一个有x个元素的环,我们总能构造一种交换方式使得至多有两个元素只被交换一次。

然后dp + 组合就行啦、
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1e9+7,maxn=1000005;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
int n,m,ans=1,F[maxn];
char ch;

inline void dp(){
	F[0]=F[1]=1;
	for(int i=2;i<=m;i++) F[i]=add(F[i-1],F[i-2]*(ll)(i-1)%ha);
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		for(ch=getchar();ch!=‘1‘&&ch!=‘2‘;ch=getchar());
		if(ch==‘1‘) m++;
	}

	dp();

	for(int i=m+1;i<=n;i++) ans=ans*(ll)i%ha;
	ans=ans*(ll)F[m]%ha;
	printf("%d\n",ans);
	return 0;
}

  

 

原文地址:https://www.cnblogs.com/JYYHH/p/8981833.html

时间: 2024-10-10 16:21:09

CodeForces - 316D3 PE Lesson的相关文章

CodeForces 139C Literature Lesson

这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封闭的abba形式,第五种就是NO.题目的意思就是给我们四个原串,让我们counting from the end(从后往前数)找到第k个元音字母,从这个位置截取原串的suffixes(后缀),形成四个新串,判断这四个新串符合以上五中情况中的哪一个.如果原串不足k个元音字母,那情况直接就是no.在判断

CodeForces 842D Vitya and Strange Lesson

题目:http://codeforces.com/problemset/problem/842/D 题意:给你n个数,m次查询,每次将数组全部异或一个数后,求没出现过的最小自然数 要求异或后的最小值我们可以用字典树来解决 而每次对数组异或可以替换每次对异或值异或 之后贪心的选取 每次都走左子树,如果左子树满了,才走右子树,这样就能保证是最小 #include<iostream> #include<cstdio> #include<cstring> #include<

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

题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每组学生第一次上课的教室的编号小于等于第二次上课的教室的编号. 第$i$间教室在第一次上课时,恰好有$x_{i}$组学生在场. 第$i$间教室在某次上课时,中间包含的学生组数不能超过$y_{i}$. 输出答案模$10^{9} + 7$. 因为第一次上课恰好有多少人,所以这个方案数是可以直接用组合数,暂

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

Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string ss and a number kk (0≤k<|s|0≤k<|s|). At the beginning of the game, players are giv

Codeforces Round #277 E. LIS of Sequence(486E) 树状数组乱搞

http://codeforces.com/contest/486/problem/E E. LIS of Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The next "Data Structures and Algorithms" lesson will be about Longest I

Codeforces 66C

C - Petya and File System Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 66C Appoint description:  System Crawler  (Nov 8, 2016 10:35:35 PM) Description Recently, on a programming lesso

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas

Codeforces Round #296 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/527 A. Playing with Paper time limit per test:2 seconds memory limit per test:256 megabytes One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm ?×? b mm sheet

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When