Uva--580(数学,动规)
2014-09-03 22:06:40
Critical Mass |
During the early stages of the Manhattan Project, the dangers of the new radioctive materials were not widely known. Vast new factory cities were built to manufacture uranium and plu- tonium in bulk. Compounds and solutions of these substances were accumulating in metal barrels, glass bottles and cardboard box piles on the cement floors of store rooms. Workers did not know that the substances they were handling could result in sickness, or worse, an explosion. The officials who new the danger assumed that they could ensure safety by never assembling any amount close to the critical mass estimated by the physicists. But mistakes were made. The workers, ignorant of the the dangers, often did not track these materials care- fully, and in some cases, too much material was stored together - an accident was waiting to happen.
Fortunately, the dangers were taken seriously by a few knowledgeable physicists. They drew up guidelines for how to store the materials to eliminate the danger of critical mass accumulations. The system for handling uranium was simple. Each uranium cube was marked ``U''. It was to be stacked with lead cubes (marked ``L'') interspersed. No more than two uranium cubes could be next to each other on a stack. With this simple system, a potential for the uranium reaching critical mass (three stacked next to each other) was avoided. The second constraint is that no more than thirty cubes can be stacked on top of each other, since the height of the storage room can only accommodate that many.
One of the physicists was still not completely satisfied with this solution. He felt that a worker, not paying attention or not trained with the new system, could easily cause a chain reaction. He posed the question: consider a worker stacking the radioactive cubes and non radioactive cubes at random on top of each other to a height of n cubes; how many possible combinations are there for a disaster to happen?
For example, say the stack is of size 3. There is one way for the stack to reach critical mass - if all three cubes are radioactive.
1: UUU
However, if the size of the stack is 4, then there are three ways:
1: UUUL
2: LUUU
3: UUUU
Input
The input is a list of integers on separate lines. Each integer corresponds to the size of the stack and is always greater than 0. The input is terminated with a integer value of 0.
Output
For each stack, compute the total number of dangerous combinations where each cube position in the linear stack can either be ``L'' for lead, or ``U'' for uranium. Output your answer as a single integer on a line by itself.
Sample Input
4 5 0
Sample Output
3 8
思路:数学题,用动规来写了。。。。dp[i][j]表示前 i 个盒子“没有3个U放在一起”,且第 i 个盒子前(包括 i)有连续 j 个盒子放 U 的方案数。
1 /************************************************************************* 2 > File Name: 580.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 03 Sep 2014 10:02:25 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 int dp[35][3]; 17 int n; 18 19 int main(){ 20 while(scanf("%d",&n) != EOF && n){ 21 dp[0][0] = 1; 22 dp[0][1] = dp[0][2] = 0; 23 for(int i = 1; i <= n; ++i){ 24 dp[i][0] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2]; 25 dp[i][1] = dp[i - 1][0]; 26 dp[i][2] = dp[i - 1][1]; 27 } 28 printf("%d\n",(1 << n) - dp[n][0] - dp[n][1] - dp[n][2]); 29 } 30 return 0; 31 }
第二种:g[i]表示前 i 个盒子“没有3个 U 放在一起”的方案数。
1 /************************************************************************* 2 > File Name: 580.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Wed 03 Sep 2014 10:02:25 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 16 int g[35]; 17 int n; 18 19 int main(){ 20 while(scanf("%d",&n) != EOF && n){ 21 g[0] = 1,g[1] = 2,g[2] = 4; 22 for(int i = 3; i <= n; ++i) 23 g[i] = g[i - 1] + g[i - 2] + g[i - 3]; 24 printf("%d\n",(1 << n) - g[n]); 25 } 26 return 0; 27 }