日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Timus 1114. Boxes

發(fā)布時(shí)間:2025/3/8 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Timus 1114. Boxes 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Timus 1114. Boxes 要求計(jì)算出將兩種顏色的球放到盒子中的各種組合的數(shù)目。

1114. Boxes

Time Limit: 0.6 second
Memory Limit: 16 MB

N boxes are lined up in a sequence (1 ≤ N ≤ 20). You have A red balls and B blue balls (0 ≤ A ≤ 15, 0 ≤ B ≤ 15). The red balls (and the blue ones) are exactly the same. You can place the balls in the boxes. It is allowed to put in a box, balls of the two kinds, or only from one kind. You can also leave some of the boxes empty. It's not necessary to place all the balls in the boxes. Write a program, which finds the number of different ways to place the balls in the boxes in the described way.

Input

Input contains one line with three integeres N, A and B separated by space.

Output

The result of your program must be an integer writen on the only line of output.

Sample

inputoutput
2 1 1 9
Problem Source: First competition for selecting the bulgarian IOI team.

解答如下(C#語(yǔ)言):

?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?}

這道題就是一個(gè)組合數(shù)學(xué)問(wèn)題。題目說(shuō),你有 N (1 ≤ N ≤ 20) 個(gè)盒子,A (0 ≤ A ≤ 15) 個(gè)紅色的球和 B (0 ≤ B ≤ 15) 個(gè)藍(lán)色的球。這些球可以放到盒子里面,也可以不放到盒子里面,一個(gè)盒子可以放任意多個(gè)球。問(wèn)總共有多少種不同情形。

由于球可以放到盒子里面,也可以不放到盒子里面,所以紅球和盒子總共有 C( N + A, A ) 種不同的情形。同樣,藍(lán)球和盒子總共有 C( N + B, B ) 種不同的情形。所以,最終的答案就是 C( N + A, A) * C( N + B, B ) 。這里,C( n, m ) 表示從 n 個(gè)不同的物品中任意取出 m 個(gè)的組合數(shù),公式是 C( n, m ) = n! / (n - m)! / m!。以上程序中的 F( n, m ) = C( n + m, m)。

要注意的是,當(dāng)輸入為 N = 20,A = 15,B = 15 時(shí),輸出是 10549134770590785600,這個(gè)數(shù)已經(jīng)大于 263 - 1,所以要使用 ulong 數(shù)據(jù)類型。如果使用 C/C++ 語(yǔ)言,因?yàn)?Timus Online Judge 使用的是 Visual C++ 編譯器,所以要使用 unsigned __int64 數(shù)據(jù)類型。如果 在 Sphere Online Judge 答題的話,因?yàn)槠渚幾g器是 gcc,所以要使用 unsigned long long 數(shù)據(jù)類型。C++ 語(yǔ)言的程序如下所示(如果是 gcc 編譯器則去掉第 2 行開頭的“//”):

?1?#include?<iostream>
?2?//#define?__int64?long?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?}

本程序的運(yùn)行時(shí)間如下:

有點(diǎn)奇怪的是,Visual C++ 編譯器也可以使用 unsigned long long,但是速度卻比使用 unsinged __int64 慢了 15 倍。如上圖所示,ID = 2154601 的記錄就是使用 unsinged long long,運(yùn)行時(shí)間為 0.015 秒。而 ID = 2135962 的記錄使用 unsinged __int64,運(yùn)行時(shí)間為 0.001 秒。不知是什么原因。照理說(shuō),__int64 應(yīng)該和 long long 是一樣的。

總結(jié)

以上是生活随笔為你收集整理的Timus 1114. Boxes的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。