BOI 2003 Problem. Spaceship
題目
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.
分析
看完題目其實(shí)就會(huì)有一個(gè)第一感覺,這道題的本質(zhì)是一道數(shù)學(xué)題。這時(shí)候就要掏出紙和筆打草稿做計(jì)算。翻譯過來,這道題就是:有一些人,先從前s個(gè)人里選一個(gè),再?gòu)那?s個(gè)人里選一個(gè)……求這樣選的方法數(shù)。
這題的精髓在于首先要找到這道題目與組合之間的關(guān)系。然后再發(fā)現(xiàn)這些數(shù)字組成的序列其實(shí)是一個(gè)楊輝三角。
在我的程序當(dāng)中,我用一個(gè)init函數(shù)來計(jì)算C數(shù)組當(dāng)中存儲(chǔ)的數(shù)字(楊輝三角第s行),C[i]對(duì)應(yīng)的是Cis。
init函數(shù)的寫法是有點(diǎn)技巧的。因?yàn)閷?duì)于計(jì)算一個(gè)C[j]需要用到的兩個(gè)數(shù)字是上一行的C[j]和C[j-1],而如果從左往右計(jì)算,就會(huì)發(fā)現(xiàn)需要用的數(shù)字已經(jīng)被更新過了。所以要做的是從右往左算。這樣可以讓本來需要開一個(gè)二維數(shù)組或者要寫幾行代碼的函數(shù)變得非常簡(jiǎn)潔明了。
接下來有一個(gè)f數(shù)組,f[i][j]表示i個(gè)s人小組里選出j個(gè)人的可能情況數(shù)量。想到就是C[k]*f[i-1][j-k]的和。也就是i-1個(gè)小組選j-k個(gè)人的情況數(shù)。
(千萬別忘了初始化數(shù)組233,調(diào)了好久)
程序
(第一個(gè)程序有高精度算法,添加了運(yùn)算符重載。當(dāng)然如果你只想了解核心算法,只要看第二段程序就可以了。兩段程序在本質(zhì)上是相同的。)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int MAX = 100; 4 struct BIG 5 { 6 int len, s[MAX]; 7 BIG() 8 { 9 memset(s, 0, sizeof(s)); 10 len = 1; 11 } 12 BIG(int num) 13 { 14 *this = num; 15 } 16 BIG(const char* num) 17 { 18 *this = num; 19 } 20 BIG operator = (int num) 21 { 22 char s[MAX]; 23 sprintf(s, "%d", num); 24 *this = s; 25 return *this; 26 } 27 string str() const 28 { 29 string res = ""; 30 for(int i = 0; i < len; i++) 31 res = (char)(s[i] + '0') + res; 32 if(res == "") res = "0"; 33 return res; 34 } 35 void clean() 36 { 37 while(len > 1 && !s[len-1]) 38 len--; 39 } 40 BIG operator = (const char* num) 41 { 42 len = strlen(num); 43 for(int i = 0; i < len; i++) 44 s[i] = num[len-i-1] - '0'; 45 return *this; 46 } 47 BIG operator + (const BIG& b) const 48 { 49 BIG c; 50 c.len = 0; 51 for(int i = 0, g = 0; g || i < max(len, b.len); i++) 52 { 53 int x = g; 54 if(i < len) x += s[i]; 55 if(i < b.len) x += b.s[i]; 56 c.s[c.len++] = x % 10; 57 g = x / 10; 58 } 59 return c; 60 } 61 BIG operator * (const BIG& b) 62 { 63 BIG c; c.len = len + b.len; 64 for(int i = 0; i < len; i++) 65 for(int j = 0; j < b.len; j++) 66 c.s[i+j] += s[i] * b.s[j]; 67 for(int i = 0; i < c.len-1; i++) 68 { 69 c.s[i+1] += c.s[i] / 10; 70 c.s[i] %= 10; 71 } 72 c.clean(); 73 return c; 74 } 75 BIG operator - (const BIG& b) { 76 BIG c; 77 c.len = 0; 78 for(int i = 0, g = 0; i < len; i++) 79 { 80 int x = s[i] - g; 81 if(i < b.len) 82 x -= b.s[i]; 83 if(x >= 0) 84 g = 0; 85 else 86 { 87 g = 1; 88 x += 10; 89 } 90 c.s[c.len++] = x; 91 } 92 c.clean(); 93 return c; 94 } 95 bool operator < (const BIG& b) const 96 { 97 if(len != b.len) 98 return len < b.len; 99 for(int i = len-1; i >= 0; i--) 100 if(s[i] != b.s[i]) 101 return s[i] < b.s[i]; 102 return false; 103 } 104 bool operator > (const BIG& b) const 105 { 106 return b < *this; 107 } 108 bool operator <= (const BIG& b) 109 { 110 return !(b > *this); 111 } 112 bool operator == (const BIG& b) 113 { 114 return !(b < *this) && !(*this < b); 115 } 116 BIG operator += (const BIG& b) 117 { 118 *this = *this + b; 119 return *this; 120 } 121 }; 122 ostream& operator << (ostream &out, const BIG& x) 123 { 124 out << x.str(); 125 return out; 126 } 127 long long s, p, n; 128 BIG f[60][60], C[20]; 129 void init(int n) 130 { 131 for (int i = 2; i <= n; i++) 132 for (int j = i; j >= 1; j--) 133 C[j] = C[j] + C[j-1]; 134 } 135 int main() 136 { 137 freopen("spaceship.in","r",stdin); 138 freopen("spaceship.out","w",stdout); 139 cin >> s >> n; 140 C[0] = 1, C[1] = 1; 141 142 //Initiate combination array 143 init(s); 144 f[0][0] = 1; 145 for (int i = 1; i <= n; i++) 146 { 147 for (int j = 0; j <= i; j++) 148 { 149 if (j < i) 150 p = 0; 151 else 152 p = 1; 153 for (int k = p; k <= j; k++) 154 f[i][j] += C[k]*f[i-1][j-k]; 155 } 156 } 157 cout << f[n][n] << endl; 158 return 0; 159 }?
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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/OIerPrime/p/8313087.html
總結(jié)
以上是生活随笔為你收集整理的BOI 2003 Problem. Spaceship的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux新手入门:Unable to
- 下一篇: UVA11468 Substring