Timus 1114. Boxes requires calculating the number of combinations of balls of the two colors placed in the box.
1114. Boxes
Time Limit: 0.6 second
Memory limit: 16 MB
NBoxes are lined up in a sequence (1 ≤
N≤ 20). You Have
ARed bils and
BBlue bils (0 ≤
A≤ 15, 0 ≤
B
≤ 15). The red bils (and the blue ones) are exactly the same. You can
Place the Ballin the boxes. It is allowed to put in a box, ballof
The two kinds, or only from one kind. You can also leave some of
Boxes empty. It's not necessary to place all the bils in the boxes.
Write a program, which finds the number of different ways to place
Bils in the boxes in the described way. inputinput contains one line with three integeres
N,
AAnd
BSeparated by space. outputthe result of your program must be an integer writen on the only line of output. Sample
Problem Source:First competition for selecting the Bulgarian IOI team.
The answer is as follows (C # language ):
1 using system;
2
3 namespace skyiv. Ben. timus
4 {
5 // http://acm.timus.ru/problem.aspx? Space = 1 & num = 1114
6 Sealed class t1114
7 {
8 Static void main ()
9 {
10 string [] Ss = console. Readline (). Split ();
11 ulong n = ushort. parse (ss [0]);
12 ulong A = ushort. parse (ss [1]);
13 ulong B = ushort. parse (ss [2]);
14 console. writeline (f (n, a) * F (n, B ));
15}
16
17 static ulong F (ulong N, ulong m)
18 {
19 ulong v = 1;
20 For (ulong I = 1; I <= N; I ++) V = V * (m + I)/I;
21 return V;
22}
23}
24}
This is a combination of mathematical questions. You have n (1 ≤N≤ 20) boxes, A (0 ≤A≤ 15) red balls and B (0 ≤B≤ 15) blue balls. These balls can be placed in the box, or not in the box. A box can tolerate multiple balls. Ask how many different situations are there.
Because the ball can be placed in the box or not, the red ball and the box have a total of C (n + A, A) different situations. Similarly, the blue ball and the box have a total of C (n + B, B) different situations. Therefore, the final answer is C (n + A, A) * C (n + B, B ). Here, C (n, m) indicates the number of combinations of m from n different items. The formula is C (n, m) = n! /(N-m )! /M !. F (n, m) = C (n + M, m) in the above program ).
Note that when the input is n = 20, A = 15, B = 15, the output is 10549134770590785600, and the number is already greater than 263-1. Therefore, the ulong data type is used. If you use the C/C ++ language, because timus online judge uses the Visual C ++ compiler, you must use the unsigned _ int64 data type. To answer questions in sphere online judge, the unsigned long data type should be used because the compiler is GCC. The C ++ program is as follows (if it is a GCC compiler, remove "//" starting with line 1):
1 # include <iostream>
2 // # DEFINE _ int64 long
3
4 unsigned _ int64 F (unsigned _ int64 N, unsigned _ int64 m)
5 {
6 unsigned _ int64 v = 1;
7 For (unsigned _ int64 I = 1; I <= N; I ++) V = V * (m + I)/I;
8 return V;
9}
10
11 int main ()
12 {
13 unsigned _ int64 N, A, B;
14 STD: CIN> N> A> B;
15 STD: cout <F (n, a) * F (n, B) <STD: Endl;
16}
The running time of this program is as follows:
It is strange that the visual C ++ compiler can also use unsigned long, but the speed is 15 times slower than that of unsinged _ int64. As shown in, records with ID = 2154601 use unsinged long and the running time is 0.015 seconds. The record with ID = 2135962 uses unsinged _ int64 and the running time is 0.001 seconds. I don't know why. It is reasonable to say that __int64 should be the same as long.