CF1543D2.RPD and Rap Sheet (Hard Version)

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The only difference is that here 2≤k≤1002\leq k\leq 100. You can make hacks only if both the versions of the problem are solved.

This is an interactive problem!

Every decimal number has a base kk equivalent. The individual digits of a base kk number are called kk-its. Let's define the kk-itwise XOR of two kk-its aa and bb as (a+b) mod k(a + b)\bmod k.

The kk-itwise XOR of two base kk numbers is equal to the new number formed by taking the kk-itwise XOR of their corresponding kk-its. The kk-itwise XOR of two decimal numbers aa and bb is denoted by a⊕kba\oplus_{k} b and is equal to the decimal representation of the kk-itwise XOR of the base kk representations of aa and bb. All further numbers used in the statement below are in decimal unless specified.

You have hacked the criminal database of Rockport Police Department (RPD), also known as the Rap Sheet. But in order to access it, you require a password. You don't know it, but you are quite sure that it lies between 00 and n−1n-1 inclusive. So, you have decided to guess it. Luckily, you can try at most nn times without being blocked by the system. But the system is adaptive. Each time you make an incorrect guess, it changes the password. Specifically, if the password before the guess was xx, and you guess a different number yy, then the system changes the password to a number zz such that x⊕kz=yx\oplus_{k} z=y. Guess the password and break into the system.

这是该问题的困难版本。唯一的区别在于此处 2≤k≤1002\leq k\leq 100。仅当两个版本的问题均被解决时,你才可进行 hack。

这是一个交互式问题!

每个十进制数都有一个对应的 kk 进制表示。kk 进制数的各位数字被称为 kk-its(kk 进制位)。我们定义两个 kk-its aa 和 bb 的 kk-itwise XOR(按 kk 进制位异或)为 (a+b) mod k(a + b)\bmod k。

两个 kk 进制数的 kk-itwise XOR 定义为:对它们对应位置上的 kk-its 分别进行 kk-itwise XOR 运算后所形成的新 kk 进制数。两个十进制数 aa 和 bb 的 kk-itwise XOR 记作 a⊕kba\oplus_{k} b,其值等于 aa 与 bb 的 kk 进制表示逐位进行 kk-itwise XOR 后所得结果的十进制表示。以下陈述中所有未特别说明的数字均为十进制。

你已成功入侵了 Rockport 警察局(RPD,又称 Rap Sheet)的罪犯数据库。但要访问该数据库,你需要输入密码。你并不知道密码,但你确信它一定在 00 到 n−1n-1(含端点)之间。因此,你决定尝试猜测密码。幸运的是,你在被系统封锁前最多可尝试 nn 次。但该系统是自适应的:每次你猜错时,密码都会发生变化。具体而言,若猜之前密码为 xx,而你猜了一个不同的数 yy,则系统会将密码更改为某个数 zz,使得 x⊕kz=yx\oplus_{k} z=y。请猜出密码并成功入侵系统。

输入格式

The first line of input contains a single integer tt (1≤t≤10 0001\leq t\leq 10\,000) denoting the number of test cases. tt test cases follow.

The first line of each test case contains two integers nn (1≤n≤2⋅1051\leq n\leq 2\cdot 10^5) and kk (2≤k≤100)(2\leq k\leq 100).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤10 0001\leq t\leq 10\,000),表示测试用例的数量。接下来是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn(1≤n≤2⋅1051\leq n\leq 2\cdot 10^5)和 kk(2≤k≤1002\leq k\leq 100)。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输入输出样例

  • 输入#1

    2
    5 2
    
    0
    
    0
    
    1
    5 3
    
    0
    
    0
    
    1

    输出#1

    3
    
    4
    
    5
    
    
    1
    
    4
    
    6

说明/提示

Test Case 1:

In this case, the hidden password is 22.

The first query is 33. It is not equal to the current password. So, 00 is returned, and the password is changed to 11 since 2⊕21=32\oplus_2 1=3.

The second query is 44. It is not equal to the current password. So, 00 is returned, and the password is changed to 55 since 1⊕25=41\oplus_2 5=4.

The third query is 55. It is equal to the current password. So, 11 is returned, and the job is done.

Test Case 2:

In this case, the hidden password is 33.

The first query is 11. It is not equal to the current password. So, 00 is returned, and the password is changed to 77 since 3⊕37=13\oplus_3 7=1. [3=(10)3[3=(10)_3, 7=(21)37=(21)_3, 1=(01)31=(01)_3 and (10)3⊕3(21)3=(01)3](10)_3\oplus_3 (21)_3 = (01)_3].

The second query is 44. It is not equal to the current password. So, 00 is returned, and the password is changed to 66 since 7⊕36=47\oplus_3 6=4. [7=(21)3[7=(21)_3, 6=(20)36=(20)_3, 4=(11)34=(11)_3 and (21)3⊕3(20)3=(11)3](21)_3\oplus_3 (20)_3 = (11)_3].

The third query is 66. It is equal to the current password. So, 11 is returned, and the job is done.

Note that these initial passwords are taken just for the sake of explanation. In reality, the grader might behave differently because it is adaptive.

测试用例 1:

本例中,隐藏的密码为 22。

第一次查询为 33。它不等于当前密码,因此返回 00;同时密码更新为 11,因为 2⊕21=32\oplus_2 1=3。

第二次查询为 44。它不等于当前密码,因此返回 00;同时密码更新为 55,因为 1⊕25=41\oplus_2 5=4。

第三次查询为 55。它等于当前密码,因此返回 11,任务完成。

测试用例 2:

本例中,隐藏的密码为 33。

第一次查询为 11。它不等于当前密码,因此返回 00;同时密码更新为 77,因为 3⊕37=13\oplus_3 7=1。[3=(10)3[3=(10)_3, 7=(21)37=(21)_3, 1=(01)31=(01)_3,且 (10)3⊕3(21)3=(01)3](10)_3\oplus_3 (21)_3 = (01)_3]。

第二次查询为 44。它不等于当前密码,因此返回 00;同时密码更新为 66,因为 7⊕36=47\oplus_3 6=4。[7=(21)3[7=(21)_3, 6=(20)36=(20)_3, 4=(11)34=(11)_3,且 (21)3⊕3(20)3=(11)3](21)_3\oplus_3 (20)_3 = (11)_3]。

第三次查询为 66。它等于当前密码,因此返回 11,任务完成。

注意:这些初始密码仅为解释目的而设定。实际上,评测程序可能表现不同,因为它具有自适应性。

输入解题思路,AI测评打分。不知道怎么写?

首页