sicily 1011 Lenny's Lucky Lotto

Description

Lenny likes to play the game of lotto. In the lotto game, he picks a list of N unique numbers in the range from 1 to M. If his list matches the list of numbers that are drawn, he wins the big prize.

Lenny has a scheme that he thinks is likely to be lucky. He likes to choose his list so that each number in it is at least twice as large as the one before it. So, for example, if = 4 and = 10, then the possible lucky lists Lenny could like are:

1 2 4 8

1 2 4 9

1 2 4 10

1 2 5 10

 Thus Lenny has four lists from which to choose.

Your job, given N and M, is to determine from how many lucky lists Lenny can choose.

Input

There will be multiple cases to consider from input. The first input will be a number C (0 < C <= 50) indicating how many cases with which you will deal. Following this number will be pairs of integers giving values for N and M, in that order. You are guaranteed that 1 <= N <= 10, 1 <= M <= 2000, and N <= M. Each N M pair will occur on a line of its own. N and M will be separated by a single space.

Output

For each case display a line containing the case number (starting with 1 and increasing sequentially), the input values for N and M, and the number of lucky lists meeting Lenny’s requirements. The desired format is illustrated in the sample shown below.

Sample Input

3
4 10
2 20
2 200

Sample Output

Case 1: n = 4, m = 10, # lists = 4
Case 2: n = 2, m = 20, # lists = 100
Case 3: n = 2, m = 200, # lists = 10000

分析:

本题目为动态规划应用,或者说是递推求值(因为并非典型DP题目,并没有求某种最优解)。关键是分析问题找出递推公式,再潜入循环中求解即可。这里特别要注意题目要求对第i个选择数字,第i+1个数字应该大于或者等于第i个的2倍;同时,本题最后答案可能会很大,所以存储结果的数组数据类型要调整为usigned long long较好,防止发生溢出。

代码:

 1 // Problem#: 1011
 2 // Submission#: 1794222
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 #include <cstring>
 8 using namespace std;
 9 
10 #define N 11
11 #define M 2001
12 unsigned long long dp[N][M];
13 
14 int main(){
15     int t,n,m,count;
16     unsigned long long re;
17     count = 0;
18     cin >> t;
19     while(t--){
20         cin >> n >> m;
21         re = 0;
22         memset(dp,0,sizeof(dp));  
23         for ( int i=1 ; i<=m ; i++ ) dp[1][i]=1;  
24         for ( int i=2 ; i<=n ; i++ ){
25             for (int j=1 ; j<=m ; j++){
26                 for ( int k=i-1 ; k<=j/2 ; k++ )
27                     dp[i][j] += dp[i-1][k];
28             }
29         }
30         for ( int i=n ; i<=m ; i++ ) re += dp[n][i];
31         cout << "Case " << ++count << ": n = " << n << ", m = " << m << ", # lists = " << re << endl;
32     }
33     return 0;
34 }                                 
原文地址:https://www.cnblogs.com/ciel/p/2876775.html