[sicily online]1011. Lenny’s Lucky Lotto

來源:互聯網
上載者:User
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];//必須是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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.