CF1870F.Lazy Numbers

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given positive integers nn and kk . For each number from 11 to nn , we write its representation in the number system with base kk (without leading zeros) and then sort the resulting array in lexicographic order as strings. In the sorted array, we number the elements from 11 to nn (i.e., indexing starts from 11 ). Find the number of values ii such that the representation of number ii is at the ii -th position in the sorted array of representations.

Examples of representations: 11 in any number system is equal to 11 , 77 with k=3k = 3 is written as 2121 , and 8181 with k=9k = 9 is written as 100100 .

输入格式

The first line contains a single integer tt ( 1t1031 \leq t \leq 10^3 ) — the number of test cases. This is followed by a description of the test cases.

The only line of each test case contains integers nn and kk ( 1n10181 \leq n \leq 10^{18} , 2k10182 \leq k \leq 10^{18} ).

输出格式

For each test case, output a single integer — the number of values 1in1 \leq i \leq n such that the representation of number ii in the number system with base kk is at the ii -th position after sorting.

输入输出样例

  • 输入#1

    8
    2 2
    4 2
    6 4
    33 2
    532 13
    780011804570805480 3788
    366364720306464627 4702032149561577
    293940402103595405 2

    输出#1

    2
    2
    1
    3
    1
    3789
    1
    7

说明/提示

In the first test case, for numbers 11 and 22 , their representations are at positions 11 and 22 respectively.

In the second test case, the sorted array is [12=1,102=2,1002=4,112=3][1_2 = 1, 10_2 = 2, 100_2 = 4, 11_2 = 3] , and only the representations of numbers 11 and 22 are at the required positions.

In the third test case, the sorted array is [14=1,104=4,114=5,124=6,24=2,34=3][1_4 = 1, 10_4 = 4, 11_4 = 5, 12_4 = 6, 2_4 = 2, 3_4 = 3] , and only the number 11 is at its position.

首页