18121 排排坐看电影
时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC;VC
Description
某理工学校A班全为男生,另有某师范学校B班全为女生。一次,两个班组织了一次联谊活动,观看电影 《美人鱼》,订完票发现所有位置为同一排且从1到T号(T为人的总数),为了让参加活动的每一个女生都有机会同 男生有对话的机会,组织者在安排座位时,让女生左或右,至少有一个男生。现在告诉你男生人数n,女生 人数m,问一共有多少种不同的座位安排方法。
输入格式
每一行一个数W(W<=100),为case数量 此后W行,每行两个数n和m
输出格式
每个case输出一个结果(使用long long)
输入样例
7 3 0 3 1 0 1 1 1 2 2 2 3 2 4
输出样例
6 24 0 2 16 36 48
提示
注意:男生旁边可以没有女生
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <string.h> #include <algorithm> #include <queue> #include <stack> #include <vector> using namespace std;long long jie(int n)
{
long long s=1; int i; for(i=1;i<=n;++i){ s=s*i; } return s; } long long solve(int n,int w,int p) { if(n==0&&w>1) return 0; if(n>0&&w>0){ switch(p){ case 1:return n*solve(n-1,w,1)+w*solve(n,w-1,2);break; case 2:return n*solve(n-1,w,1)+w*solve(n,w-1,3);break; case 3:return n*solve(n-1,w,1);break; } } if(n==0&&w==1){ if(p==1) return 1; else return 0; } return jie(n); } int main() { int t,n,w; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&w); printf("%lld\n",solve(n,w,2)); } return 0; }