[sicily online]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 = 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

34 102 202 200

Sample Output

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

题目分析:

按照题意,可以用递归,但是中间过程很多结果是重复的,需要保存起来(也就是重叠子问题),所以可以用dp来解决

递归式是array[i][j]表示第i位为j时有多少种情况,array[i][j]=∑array[i+1][k](k为2*i到最大值)

笔者的错误:

1因为最大结果10 2000很大,所以要用unsigned long long

2本来节省空间用map+vector来存储,结果时间多了很多,还是老老实实用数组吧

#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<list>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <stack>
#include<queue>
#include<string.h>
using namespace std;

int main()
{
	int geshu;
	cin>>geshu;
	for(int xx=0;xx<geshu;xx++)
	{
		int n,m;
		cin>>n>>m;
		vector<int> min(n);
		vector<int> max(n);
		unsigned long long record[11][2001];//必须是longlong
		memset(record,0,sizeof(record));
		min[0]=1;
		max[n-1]=m;
		for(int i=1;i<n;i++)
			min[i]=min[i-1]*2;
		for(int i=n-2;i>=0;i--)
			max[i]=max[i+1]/2;
		int w=min[0];
		unsigned long long count=0;
		for(int index=n-1;index>=0;index--)
		{
			unsigned long long sum=0;
			while(min[index]<=max[index])
			{
				if(index==min.size()-1)
					record[index][min[index]]=1;
				else
				{
					for(int i=min[index]*2;i<=max[index+1];i++)
						record[index][min[index]]+=record[index+1][i];
				}
				min[index]++;
			}//end while
		}
		for(int i=max[0];i>=w;i--)
			count+=record[0][i];
		cout<<"Case "<<xx+1<<": n = "<<n<<", m = "<<m<<", # lists = "<<count<<endl;
	}//end for
	return 1;
}


你可能感兴趣的:([sicily online]1011. Lenny's Lucky Lotto)