Game of Connections
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4246 Accepted Submission(s): 2467
Problem Description
This
is a small but ancient game. You are supposed to write down the numbers
1, 2, 3, ... , 2n - 1, 2n consecutively in clockwise order on the
ground to form a circle, and then, to draw some straight line segments
to connect them into number pairs. Every number must be connected to
exactly one another. And, no two segments are allowed to intersect.
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
It's still a simple game, isn't it? But after you've written down the 2n numbers, can you tell me in how many different ways can you connect the numbers into pairs? Life is harder, right?
Input
Each
line of the input file will be a single positive number n, except the
last line, which is a number -1. You may assume that 1 <= n <=
100.
Output
For each n, print in a single line the number of ways to connect the 2n numbers into pairs.
Sample Input
2
3
-1
Sample Output
2
5
Source
题意:
2n个数顺时针组成环,用一条线将两个相连,并且每个数只能与另外一个数相连,连线不能相交,问有几种不同的连线方案。
代码:
又是那个神奇的递推公式。
1 package luzhiyuan; 2 import java.util.Scanner; 3 import java.math.BigInteger; 4 public class java1 { 5 public static void main(String[] args){ 6 BigInteger [][]a=new BigInteger[102][102]; 7 BigInteger sta=BigInteger.valueOf(1); //把其他形式的数化为大整数 8 BigInteger zeo=BigInteger.valueOf(0); 9 for(int i=0;i<=100;i++) 10 for(int j=0;j<=100;j++) 11 a[i][j]=zeo; //如果想让后面的加法函数可用一定要给大整数赋初值 12 for(int i=1;i<=100;i++) 13 a[i][0]=sta; 14 for(int i=1;i<=100;i++) 15 for(int j=1;j<=i;j++){ 16 a[i][j]=a[i][j].add(a[i-1][j]); 17 a[i][j]=a[i][j].add(a[i][j-1]); 18 } 19 Scanner cin=new Scanner(System.in); 20 while(cin.hasNext()){ 21 int n=cin.nextInt(); 22 if(n==-1) break; 23 System.out.println(a[n][n]); 24 } 25 } 26 }
Buy the Ticket
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6361 Accepted Submission(s): 2661
Problem Description
The
"Harry Potter and the Goblet of Fire" will be on show in the next few
days. As a crazy fan of Harry Potter, you will go to the cinema and have
the first sight, won’t you?
Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).
Now the problem for you is to calculate the number of different ways of the queue that the buying process won't be stopped from the first person till the last person.
Note: initially the ticket-office has no money.
The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
Suppose the cinema only has one ticket-office and the price for per-ticket is 50 dollars. The queue for buying the tickets is consisted of m + n persons (m persons each only has the 50-dollar bill and n persons each only has the 100-dollar bill).
Now the problem for you is to calculate the number of different ways of the queue that the buying process won't be stopped from the first person till the last person.
Note: initially the ticket-office has no money.
The buying process will be stopped on the occasion that the ticket-office has no 50-dollar bill but the first person of the queue only has the 100-dollar bill.
Input
The
input file contains several test cases. Each test case is made up of
two integer numbers: m and n. It is terminated by m = n = 0. Otherwise,
m, n <=100.
Output
For
each test case, first print the test number (counting from 1) in one
line, then output the number of different ways in another line.
Sample Input
3 0
3 1
3 3
0 0
Sample Output
Test #1:
6
Test #2:
18
Test #3:
180
Author
HUANG, Ninghai
题意:
一群人排队买票,票价50元,有人拿着50元的,有人拿着100元的,售票员没有钱,问怎样排队才能让每个人都买到票,有多少种排队方案。
依然是那个递推公式,50元的人要永远多于100元的人,50元的人作为列,100元的人作为行,一个上三角方格阵,每个人的位置又有A(n,n)*A(m,m)种,再乘a[n][m].
代码:
1 package luzhiyuan; 2 import java.util.Scanner; 3 import java.math.BigInteger; 4 public class java1 { 5 public static void main(String[] args){ 6 BigInteger [][]a=new BigInteger[102][102]; 7 BigInteger sta=BigInteger.valueOf(1); //把其他形式的数化为大整数 8 BigInteger zeo=BigInteger.valueOf(0); 9 for(int i=0;i<=100;i++) 10 for(int j=0;j<=100;j++) 11 a[i][j]=zeo; //如果想让后面的加法函数可用一定要给大整数赋初值 12 for(int i=1;i<=100;i++) 13 a[i][0]=sta; 14 for(int i=1;i<=100;i++) 15 for(int j=1;j<=i;j++){ 16 a[i][j]=a[i][j].add(a[i-1][j]); 17 a[i][j]=a[i][j].add(a[i][j-1]); 18 } 19 Scanner cin=new Scanner(System.in); 20 int t=0; 21 while(cin.hasNext()){ 22 int n=cin.nextInt(); 23 int m=cin.nextInt(); 24 int nn=n,mm=m; 25 if(n==0&&m==0) break; 26 t++; 27 BigInteger x=BigInteger.valueOf(n); 28 BigInteger y=BigInteger.valueOf(m); 29 BigInteger ans=BigInteger.valueOf(1); 30 while(nn>1){ 31 ans=ans.multiply(x); 32 nn--; 33 x=x.subtract(sta); 34 } 35 while(mm>1){ 36 ans=ans.multiply(y); 37 mm--; 38 y=y.subtract(sta); 39 } 40 ans=ans.multiply(a[n][m]); 41 System.out.println("Test #"+t+":"); 42 System.out.println(ans); 43 } 44 } 45 }