银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
  268 随笔 :: 2 文章 :: 2616 评论 :: 140万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
Timus 1114. Boxes 要求计算出将两种颜色的球放到盒子中的各种组合的数目。

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#语言):

复制代码
 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 }
复制代码

这道题就是一个组合数学问题。题目说,你有 N (1 ≤ N ≤ 20) 个盒子,A (0 ≤ A ≤ 15) 个红色的球和 B (0 ≤ B ≤ 15) 个蓝色的球。这些球可以放到盒子里面,也可以不放到盒子里面,一个盒子可以放任意多个球。问总共有多少种不同情形。

由于球可以放到盒子里面,也可以不放到盒子里面,所以红球和盒子总共有 C( N + A, A ) 种不同的情形。同样,蓝球和盒子总共有 C( N + B, B ) 种不同的情形。所以,最终的答案就是 C( N + A, A) * C( N + B, B ) 。这里,C( n, m ) 表示从 n 个不同的物品中任意取出 m 个的组合数,公式是 C( n, m ) = n! / (n - m)! / m!。以上程序中的 F( n, m ) = C( n + m, m)。

要注意的是,当输入为 N = 20,A = 15,B = 15 时,输出是 10549134770590785600,这个数已经大于 263 - 1,所以要使用 ulong 数据类型。如果使用 C/C++ 语言,因为 Timus Online Judge 使用的是 Visual C++ 编译器,所以要使用 unsigned __int64 数据类型。如果 在 Sphere Online Judge 答题的话,因为其编译器是 gcc,所以要使用 unsigned long long 数据类型。C++ 语言的程序如下所示(如果是 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 }
复制代码

本程序的运行时间如下:

有点奇怪的是,Visual C++ 编译器也可以使用 unsigned long long,但是速度却比使用 unsinged __int64 慢了 15 倍。如上图所示,ID = 2154601 的记录就是使用 unsinged long long,运行时间为 0.015 秒。而 ID = 2135962 的记录使用 unsinged __int64,运行时间为 0.001 秒。不知是什么原因。照理说,__int64 应该和 long long 是一样的。

posted on   银河  阅读(892)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示