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
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
利用动态规划的思路,先确定n个数中第一个数可能的情况,再利用递归计算每种情况下的list个数,再全部相加即可得总数量。由于递归效率偏低,所以引入memory数组来保存递归结果,可以省去重复计算的时间。// Problem#: 1011 // Submission#: 4932061 // 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<algorithm> #include<cmath> using namespace std; long long memory[11][2001];//memory[i][j]用来记录数字个数为i,起始值为j时的list数量 void ini()//将memory数组全部置为0 { for(int i=0;i<11;++i) for(int j=0;j<2001;++j) memory[i][j]=0; } long long getlist(int n,int start,int end) { if( memory[n][start]==0)//若不为0则说明此值已被计算出,可直接返回 { if(start>end&&n>1) memory[n][start]=0;//起始值大于终值,则可行的list数量为0 if(start>end&&n==1) memory[n][start]=0;//起始值大于终值,则可行的list数量为0 if(start==end&&n>1) memory[n][start]=0;//起始值等于终值,但要确定的数超过一个则不可行 if(start==end&&n==1) memory[n][start]=1;//虽然起始值等于终值,但只要确定一个数,故只有唯一的一种可能,list数量为1 if(start<end&&n==1) memory[n][start]=end-start+1;//起始值小于终值,但只需确定一个数,所以[start,end]间的任意一个值均可,所以list数量为end-start+1 if(start<end&&n>1) { int firstmax=end*pow(0.5,n-1);//由于后一个数必须为前一个的两倍以上,所以可以确定n个数中第一个数的最大值 if(firstmax<start) memory[n][start]=0;//若第一个数的最大值小于start则无可行解 else { int count=firstmax-start+1;//n个数时第一个数可能的情况,第一个数介于[start,firstmax]之间,共有count种选择 long long result=0; for(int i=0;i<count;++i)//分别计算这count种情况下的list的数量,全部相加存于result变量中 { if(memory[n-1][(start+i)*2]) result+=memory[n-1][(start+i)*2]; else { memory[n-1][(start+i)*2]=getlist(n-1,(start+i)*2,end); result+=memory[n-1][(start+i)*2]; } } memory[n][start]=result;//result即为最终结果,存入数组 } } } return memory[n][start]; } int main() { int c,n,m; cin>>c; for(int i=0;i<c;++i) { ini(); cin>>n>>m; cout<<"Case "<<i+1<<": n = "<<n<<", m = "<<m<<", # lists = "<<getlist(n,1,m)<<endl; } return 0; }