码迷,mamicode.com
首页 > 其他好文 > 详细

BOI 2003 Problem. Spaceship

时间:2018-01-18 22:06:37      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:lin   member   i++   ogr   pos   att   rate   color   which   

题目

The Romanian Space Association (RSA) discovered a new asteroid, which they called BOI2003. RSA is eager to
explore the asteroid BOI2003, so they built a spaceship to carry scientists to the asteroid. The spaceship they built
has some peculiar features. When the spaceship leaves Earth for the first time it should carry exactly S scientists.
When the spaceship arrives at the asteroid BOI2003, a single scientist lands and the spaceship returns to take over
other S scientists.
The first scientist that landed on BOI2003 suspects the existence of a dangerous virus on the asteroid. Thus he
suggests that, until the research is over, not a single scientist that traveled to BOI2003 should leave the spaceship
when it arrives to Earth.
To be more specific, let’s assume that the scientists are denoted by integers starting at 1. The spaceship works in
the following way:
– the first time it leaves Earth, the spaceship carries the scientists 1, 2, ..., S; one of these scientists lands on the
asteroid and the other S-1 scientists return to Earth (but do not get off the ship);
– the second time the spaceship leaves Earth, the scientists S+1, S+2, ..., 2S gather the S-1 scientists that came
back, and the spaceship carries these 2S-1 scientists to BOI2003; one of them lands on the asteroid and the
spaceship returns to Earth with 2S-2 scientists (again, they do not get off the ship);
– the third time, the scientists 2S+1, 2S+2, ..., 3S together with the other 2S-2 scientists who already are on
the spaceship, travel to BOI2003 and one of them lands on the asteroid;
– and so on.
After N rides, a research team consisting of N scientists has been carried over and is working on the asteroid
BOI2003.
Task
Write a program that determines the number of possibilities to form the research team.
Input data (file: ship.in)
The single line of the input file contains the integers S and N, separated by a single space.
Output data (file: ship.out)
The single line of the output file contains an integer K, representing the number of possibilities to form the
research team.
Constraints
1 ≤ S ≤ 10
1 ≤ N ≤ 40
The spaceship has unlimited capacity.
The order in which the members of the team arrive on the asteroid does not matter.
Example
ship.in ship.out
2 3 14
Time limit: 0.4 seconds/test.

分析

看完题目其实就会有一个第一感觉,这道题的本质是一道数学题。这时候就要掏出纸和笔打草稿做计算。翻译过来,这道题就是:有一些人,先从前s个人里选一个,再从前2s个人里选一个……求这样选的方法数。

这题的精髓在于首先要找到这道题目与组合之间的关系。然后再发现这些数字组成的序列其实是一个杨辉三角。

在我的程序当中,我用一个init函数来计算C数组当中存储的数字(杨辉三角第s行),C[i]对应的是Cis

init函数的写法是有点技巧的。因为对于计算一个C[j]需要用到的两个数字是上一行的C[j]和C[j-1],而如果从左往右计算,就会发现需要用的数字已经被更新过了。所以要做的是从右往左算。这样可以让本来需要开一个二维数组或者要写几行代码的函数变得非常简洁明了。

接下来有一个f数组,f[i][j]表示i个s人小组里选出j个人的可能情况数量。想到就是C[k]*f[i-1][j-k]的和。也就是i-1个小组选j-k个人的情况数。

(千万别忘了初始化数组233,调了好久)

程序

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 long long C[11], s, f[41][41], p, n;
 4 void init(int n)
 5 {
 6     for (int i = 2; i <= n; i++)
 7         for (int j = i; j >= 1; j--)
 8             C[j] = C[j] + C[j-1];
 9 }
10 int main()
11 {
12     freopen("spaceship.in","r",stdin);
13     freopen("spaceship.out","w",stdout);
14     cin >> s >> n;
15     C[0] = 1, C[1] = 1;
16     
17     init(s);
18     f[0][0] = 1;
19     for (int i = 1; i <= n; i++)
20     {
21         for (int j = 0; j <= i; j++)
22         {
23             if (j < i)
24                 p = 0;
25             else
26                 p = 1;
27             for (int k = p; k <= j; k++)
28                 f[i][j] += C[k]*f[i-1][j-k];
29         }
30     }
31     cout << f[n][n] << endl;
32     return 0;
33 }

 

BOI 2003 Problem. Spaceship

标签:lin   member   i++   ogr   pos   att   rate   color   which   

原文地址:https://www.cnblogs.com/OIerPrime/p/8313087.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!