Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320751
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-18 10:39:45

Nancy's Birthday

Time Limit:1sMemory limit:32M
Accepted Submit:12Total Submit:65

Description

Nancy has bought a lottery. The lottery numbers are positive integers. The prized lottery numbers are determined by randomizing two integers m and k (m>=0, k>=1). Any lottery number that contains m 0's can be prized, and the kth number is the first prize if all the prized numbers are sorted in an ascending order. Nancy won the first prize with a lucky number, for the number of digits and the leftmost digit in the first prized number happen to make up the date of Nancy's birthday. Can you work out Nancy's birthday?

Input

The first line in the input is an integer N representing the number of test cases. The following N lines are N test cases. Each line contains two integers separated by one space , where the first integer is m and the second is k (0<=m<=10,1<=k<=10^12).

Output

For each test case, output a line containing two integers separated by one space, where the first integer is the number of digits in the first prized number and the second integer is the leftmost digit in the first prized number.

Sample Input

2
1 5
2 15

Output for the Sample Input

2 5
4 1

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

#define size 30

long long power9[size + 1];
long long C[size + 1][size + 1];

int main()
{
    long long i, j, k, tmp, test;
    long long t1,t2;

    power9[0] = 1;
    for (i = 1; i <= size; ++i)
        power9[i] = power9[i - 1] * 9;

    for (i = 0; i <= size; ++i)
    {
        for (j = 0; j <= i; ++j)
        {
            tmp = 1;
            if ((i != 0) && (j != 0))
            {
                for (k = j + 1; k <= i; ++k)
                    tmp *= k;
                for (k = 1; k <= i - j; ++k)
                    tmp /= k;
            }
            C[i][j] = tmp;
        }
    }

    cin >> test;
    for(i = 0;i < test;i ++)
    {
        cin >> t1 >> t2;
        for (j = t1;; ++j)
        {
            if(9 * power9[j - t1] * C[j][t1] >= t2)
                break;
            else
                t2 -= (9 * power9[j - t1] * C[j][t1]);
        }
        cout << j + 1 << ' ' << (t2 - 1) / (power9[j - t1] * C[j][t1]) + 1 << endl;
    }
    return 1;
}

阅读(1579) | 评论(0) | 转发(0) |
0

上一篇:Square(搜索)

下一篇:Coloring of Graph

给主人留下些什么吧!~~