18121 rows of seats and watching movies
Time limit: 1000MS memory limit: 65535K
Number of submissions: 0 Number of Passes: 0
Question types: programming language: g++; GCC; Vc
Description
A science and technology school Class A is all boys, and the other has a normal course B all for girls. Once, two classes organized a networking event, watch the Movie "Mermaid", book tickets found all positions in the same row and from 1 to t (the total number of people), in order to let every girl participating in the event have the opportunity to have a dialogue with the boys, organizers in arranging seats, let girls left or right, at least one boy. Now tell you the number of boys N, the number of girls m, ask how many different ways of seating arrangements.
Input format
Each line one number W (w<=100), for Case number after W line, two numbers per line N and M
Output format
Output one result per case (using long Long)
Input sample
73 03 10 11 12 22 32 4
Output sample
62402163648
Tips
Note: There can be no girls next to boys
Author
Admin
scau-in rows and watch movies-recursion. There are n boys and M girls, asking each girl to have at least one boy on the left or right, while the boy's position is not required. Use the idea of recursion. Suppose that the number of methods for placing n boys and M females according to the topic is recur (n,m); At the beginning of a row in the most side of the position, this position can be put boys and girls, if put boys, because the remaining N boys have not been placed, so there will be N*recur (n-1,m) species of the method, if put girls, the same, is M*recur (n,m-1) species of the law. Then go to the next level of recursion, until the girls and boys have finished or met the boys have finished but girls still have the remaining number of the situation can not go on.
So, how to determine whether the current position can be put boys or boys and girls can be? My approach is to add two more parameters to the recursive function recur (): Recur (N,M,P,PP); where p is used to mark the position of the previous position is a girl or a boy, and PP is used to mark the ' previous position ' in the previous position is a boy or a girl, so in the function according to these two parameters can determine how the current position should be placed. It is also important to note that the termination condition of the function recursion.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cctype>6#include <cmath>7#include <algorithm>8#include <Set>9#include <map>Ten#include <queue> One#include <stack> A#include <utility> -#include <vector> - #definell Long Long the #defineINF 0x3f3f3f3f - #defineMale 1//a constant used to mark boys and girls. - #defineFemale 0 - using namespacestd; + - intAintN//calculates the factorial of n + { A intans=1; at for(intI=1; i<=n; i++) -ans*=i; - returnans; - } -ll recur (ll N,ll m,intPintpp) - { in if(n>0&&m>0)//if both boys and girls are more than 0 - { to if(P==male)//if the previous person in the current position is a boy, the location of a boy or a girl can + returnM*recur (n,m-1, female,p) +n*recur (n1, m,male,p); - Else //If the previous position was a girl the { * if(Pp==male)//and the former is a boy, the position is also can be put boys, or put girls $ returnM*recur (n,m-1, female,p) +n*recur (n1, m,male,p);Panax Notoginseng Else //the current one and the former is a girl, then the position must be put boys; form ' male and female male ' forms meet the requirements of the topic - returnN*recur (n1, m,male,p); the } + } A Else if(n>0)//If the girl is gone, the remaining N boys can form a n! seed release method. the { + returnA (n); - } $ Else if(m>0)//If you're thinking about the current position, it's just a girl. $ { - if(P==male)//If the previous position is a male, place the girl in that position - returnM*recur (n,m-1, female,p); the Else //If the previous position is a girl then the next placement must not meet the requirements of the topic - return 0;Wuyi } the Else if(!n&&!m)//all of them are placed . - return 1; Wu - } About intMain () $ { - //freopen ("Input.txt", "R", stdin); - intW; - ll N,m; A ll ans; +scanf"%d",&W); the while(w--) - { $scanf"%lld%lld",&n,&m); the if(M> (n<<1)||! N//if m>2n or n=0, direct output 0 the { theprintf"0\n"); the Continue; - } inAns=n*recur (n1, m,male,-1) +m*recur (n,m-1, female,-1); theprintf"%lld\n", ans); the } About return 0; the}
18121 rows of seats and watching movies