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. ios

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 ide

1 2 4 9 ui

1 2 4 10 this

1 2 5 10 spa

 Thus Lenny has four lists from which to choose. code

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

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較好,防止發生溢出。
ip

代碼:

// Problem#: 1011
// Submission#: 1794222
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstring>
using namespace std;

#define N 11
#define M 2001
unsigned long long dp[N][M];

int main(){
    int t,n,m,count;
    unsigned long long re;
    count = 0;
    cin >> t;
    while(t--){
        cin >> n >> m;
        re = 0;
        memset(dp,0,sizeof(dp));  
        for ( int i=1 ; i<=m ; i++ ) dp[1][i]=1;  
        for ( int i=2 ; i<=n ; i++ ){
            for (int j=1 ; j<=m ; j++){
                for ( int k=i-1 ; k<=j/2 ; k++ )
                    dp[i][j] += dp[i-1][k];
            }
        }
        for ( int i=n ; i<=m ; i++ ) re += dp[n][i];
        cout << "Case " << ++count << ": n = " << n << ", m = " << m << ", # lists = " << re << endl;
    }
    return 0;
}
相關文章
相關標籤/搜索