CodeForces 316D3 PE Lesson
3 seconds
256 megabytes
standard input
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.
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.
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).
5
1 2 2 1 2
120
8
1 2 2 1 2 1 1 2
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的更多相关文章
- CodeForces 139C Literature Lesson(模拟)
这个题,读懂了就是水,读不懂就没办法下手,论英语阅读的重要性...只有五种形式,第一种万能型aaaa,是另外3种的特殊情况,第二种克莱里林四行打油诗aabb形式,第三种是交替的abab形式,第四种是封 ...
- 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 ...
- 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 ...
- Codeforces 37D Lesson Timetable - 组合数学 - 动态规划
题目传送门 神奇的门I 神奇的门II 题目大意 有$n$组学生要上课2次课,有$m$个教室,编号为$1$到$m$.要确定有多少种不同的安排上课的教室的方案(每组学生都是本质不同的),使得它们满足: 每 ...
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- Codeforces Round #430 (Div. 2) Vitya and Strange Lesson
D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...
- codeforces 842 D. Vitya and Strange Lesson(01字典树+思维+贪心)
题目链接:http://codeforces.com/contest/842/problem/D 题解:像这种求一段异或什么的都可以考虑用字典树而且mex显然可以利用贪心+01字典树,和线段树差不多就 ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson
因为抑或,一眼字典树 但是处理起来比较难 #include<iostream> #include<map> #include<iostream> #include& ...
随机推荐
- Android游戏音效实现
1. 游戏音效SoundPool 游戏中会根据不同的动作 , 产生各种音效 , 这些音效的特点是短暂(叫声,爆炸声可能持续不到一秒) , 重复(一个文件不断重复播放) , 并且同时播放(比如打怪时怪的 ...
- Java中的线程的优先级
Java 中线程优先级简介: 1. Java 提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程. 按照线程的优先级决定应该调度哪个线程来执行. 2. 线程的优先级用数字表示, 范围从 1 到 ...
- VS2013 “未找到与约束 ContractName Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService RequiredTypeIdentity Microsoft.Internal.VisualStudio.PlatformUI.ISolutionAttachedCollectionService 匹配的导出”
下面是我出错误的附加图片 这个错误导致无法打开项目. 解决方法: 解: C:\Users\Administrator\AppData\Local\Microsoft\VisualStudio\12.0 ...
- WinForm连续点击按钮只打开一次窗体
许多朋友,学习C#时,制作WinForm小程序总会有一个问题,如果我们在父窗体设置的是点击一个按钮,打开一个子窗体,连续点击总会连续出现一样窗体,可是我们有时只想打开一次窗体,怎么办? 呵呵,我来方法 ...
- server2003 必要的系统优化和安全设置
修改远程桌面端口: Windows 2003系统中的远程终端服务是一项功能非常强大的服务,同时也成了入侵者长驻主机的通道,入侵者可以利用一些手段得到管理员账号和密码并入侵主机.下面,我们来看看如何通过 ...
- 伟大的淘宝IP库的API接口竟然提示503挂掉了
1 淘宝IP库惊现503错误 吃完晚饭,大概6点半了,天色已暗,太阳早就落山了.回到宿舍打开博客一看,傻眼了:博客每篇文章的评论者的地理信息全部处于“正在查询中……”的状态.这神马情况,不会是被淘宝封 ...
- MATLAB中的randi函数
randi Pseudorandom integers from a uniform discrete distribution.来自一个均匀离散分布的伪随机整数 R = randi(IMAX,N) ...
- zoj 1298 Domino Effect (最短路径)
Domino Effect Time Limit: 2 Seconds Memory Limit: 65536 KB Did you know that you can use domino ...
- (七)Redis对键key的操作
key的全部命令如下: keys pattern # 查找所有符合给定模式pattern的key ,查找所有key 使用[keys *] del key1 key2 ... # 删除给定的一个或多个k ...
- 38 一次 redis 连接泄露的原因 以及 ShardedJedisPool
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011039332/article/details/85381051前言 这个是接着 上次的 这篇文 ...