CodeForces 316D3 PE Lesson [DP+Math]
来源:互联网 发布:mac上打开隐藏文件夹 编辑:程序博客网 时间:2024/05/19 16:28
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).
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.
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.
题意:
N个人站在一条线上传球,每个人传球次数的上限为Ai(值为1或者2),一开始每个人手上的球从左往右标为1..N,每一次传球,即是两个人手上的球互换,问传球后的所有的情况数(即最后产生的1-N的不同排列数)
范围:
N<=100W
解法:
对于所有人如果传球次数上限都为1的方案数话,是很容易想到dp方程的:F[I]=F[I-1]+(I-1)*F[I-2],即多出一个球不动或者从前面选一个交换。
如果有人传球次数上限为2的话,设1的数量为a,2的数量为b,那么先让这些次数为2的人先传一次,第一个人的选择数为b,第二个人的选择数为b-1,以此类推……最后发现正好剩下a个人的传球次数都为1,那么答案便是:
F[a]*b*b-1*b-2*……b-a+1
代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#include<iostream>#include<stdlib.h>#include<set>#include<map>#include<queue>#include<vector>#include<bitset>#pragma comment(linker, "/STACK:1024000000,1024000000")template <class T>bool scanff(T &ret){ //Faster Input char c; int sgn; T bit=0.1; if(c=getchar(),c==EOF) return 0; while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); if(c==' '||c=='\n'){ ret*=sgn; return 1; } while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10; ret*=sgn; return 1;}#define inf 1073741823#define llinf 4611686018427387903LL#define PI acos(-1.0)#define lth (th<<1)#define rth (th<<1|1)#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)#define mem(x,val) memset(x,val,sizeof(x))#define mkp(a,b) make_pair(a,b)#define findx(x) lower_bound(b+1,b+1+bn,x)-b#define pb(x) push_back(x)using namespace std;typedef long long ll;typedef pair<int,int> pii;ll mod=1000000007LL;ll f[1001000];void init(){ f[0]=1; f[1]=1; f[2]=2; rep(i,3,1000000){ f[i]=(f[i-1]+ll(i-1)*f[i-2])%mod; }}int _1,_2,n;main(){ init(); scanff(n); rep(i,1,n){ int x; scanff(x); if(x==1)_1++; else _2++; } ll ans=1; rep(i,1,_2){ ans=(ans*ll(n-i+1))%mod; } ans=(ans*f[_1])%mod; printf("%lld\n",ans);}
- CodeForces 316D3 PE Lesson [DP+Math]
- CodeForces 316D3 PE Lesson
- CodeForces 316D3 PE Lesson
- Codeforces 316D PE Lesson
- 【Codeforces316D3】【CF316D3】PE Lesson
- CodeForces 37 D.Lesson Timetable(组合数学+dp)
- DP + math 之 Codeforces 126D - Fibonacci Sums
- CodeForces 599D Spongebob and Squares(DP+math)
- CodeForces - 2B -The least round way (dp math)
- Literature Lesson CodeForces
- PE 521【DP】
- PE 290【数位DP】
- PE 240 【DP】
- codeforces-743C-math
- hdu4427 Math Magic dp
- Homework of PE dp 正难则反
- 【DP】CodeForces
- 【dp】CodeForces
- 改变text字体颜色
- android ListView几个重要属性
- Educational Codeforces Round 1 A. Tricky Sum
- 【数据结构】平衡二叉树之AVL树
- java项目中logger一般使用 static final
- CodeForces 316D3 PE Lesson [DP+Math]
- This is an A+B Problem
- 总结iOS 多线程学习过程五
- MySQL show processlist 中status
- MySQLdb数据库操作
- 传纸条
- 【问题】Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds
- 《iOS Human Interface Guidelines》——Integrating with iOS
- fiddler post 二进制数据