Sicily 1011. Lenny's Lucky Lotto

Constraints
Time Limit: 1 secs, Memory Limit: 32 MB

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 N = 4 and M = 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

首先看标准样例中的数据 长度为 4,最大值为 10 则一共有 4 种
1 2 4 8
1 2 4 9
1 2 4 10
1 2 5 10
可以把这组数据分成两部分,一部分是结尾数字是给出的最大值,而另一部分
不是,我们可以用一个数组 dp[i][j]来存储长度为 i,最大值为 j 的数据所得的
方案数,则第一部分的值为 dp[i][j-1],后一部分由于是由最大值结尾,所以其
倒数第二个数字一定不大于 j/2,所以可以得到状态转移方程
dp[i][j]=dp[i][j-1]+dp[i][j/2]

#include<iostream>
#include<cstring>
using namespace std;
long long int a[11][2005];
int main(){
	int num;
	cin>>num;
	int times=1;
	while(num--){
		memset(a,0,sizeof(a));
		int n,m;
		cin>>n>>m;
		for(int i=1;i<2005;i++){
			a[1][i]=i;
		}
		for(int i=2;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(i>j){
					a[i][j]=0;
				}
				a[i][j]=a[i][j-1]+a[i-1][j/2];	
			}
		}
	cout<<"Case "<<times++<<": n = "<<n<<", m = "<<m<<", # lists = "<<a[n][m]<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/dcy19991116/article/details/89054908