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
題目分析:
按照題意,可以用遞迴,但是中間過程很多結果是重複的,需要儲存起來(也就是重疊子問題),所以可以用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];//必須是longlongmemset(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 forreturn 1;}