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 = 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
// Problem#: 1011
// Submission#: 2216249
// 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 <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

long long buf[11][2001];

long long num(int a, int b)
{
    //printf("request:%d %d\n", a, b);
    int mi = 1;
    for(int i = 1; i < a; i++)
    {
        mi *= 2;
    }
    if(b < mi)
    {
        buf[a][b] = 0;
        return 0;   
    }
    if(a == 1)
    {
        return 1;   
    }
    if(a == 2 && b == 2)
    {
        return 1;   
    }
    if(a == 2 && b == 3)
    {
        return 1;   
    }
    if(buf[a][b] != 0)
    {
        return buf[a][b];   
    }
    long long sum = 0;
    for(int i = b / 2; i >= mi / 2; i--)
    {
        sum += num(a - 1, i);   
    }
    buf[a][b] = sum;
    return sum;
}

long long sum(int a, int b)
{
    int mi = 1;
    int i;
    for(i = 1; i < a; i++)
    {
        mi *= 2;
    }
    long long s = 0;
    for(i = b; i >= mi; i--)
    {
        //printf("request:%d %d\n", a, i);
        s += num(a, i); 
    }
    return s;
} 

int main()
{
    int k;
    int a;
    int b;
    int n;
    
    scanf("%d", &n);
    memset(buf, 0, sizeof(buf));
    for(k = 0; k < n; k++)
    {
        scanf("%d %d", &a, &b);
        printf("Case %d: n = %d, m = %d, # lists = %lld\n", k + 1, a, b, sum(a, b));
    }
    
}                                 


你可能感兴趣的:(Sicily 1011. Lenny's Lucky Lotto)