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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BOI 2003 Problem. Spaceship

發布時間:2024/6/30 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.

分析

看完題目其實就會有一個第一感覺,這道題的本質是一道數學題。這時候就要掏出紙和筆打草稿做計算。翻譯過來,這道題就是:有一些人,先從前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 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 }

?

轉載于:https://www.cnblogs.com/OIerPrime/p/8313087.html

總結

以上是生活随笔為你收集整理的BOI 2003 Problem. Spaceship的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。