RPD and Rap Sheet (Easy Version) Codeforces Solution

RPD and Rap Sheet (Easy Version) Solution

This is the easy version of the problem. The only difference is that here k=2k=2. You can make hacks only if both the versions of the problem are solved.

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)modk(a+b)modk.

See Also: Codeforces Round 730 (Div. 2)

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⊕kb 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. When k=2k=2 (it is always true in this version), the kk-itwise XOR is the same as the bitwise XOR.

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⊕kz=y. Guess the password and break into the system.Input

The first line of input contains a single integer tt (1≤t≤100001≤t≤10000) 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≤n≤2⋅105) and kk (k=2k=2).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.Interaction

For each test case, first read two integers nn and kk. Then you may ask up to nn queries.

For each query, print a single integer yy (0≤y≤2⋅1070≤y≤2⋅107). Let the current password be xx. After that, read an integer rr.

If x=yx=y, you will read r=1r=1 and the test case is solved. You must then continue solving the remaining test cases.

Else, you will read r=0r=0. At this moment the password is changed to a number zz such that x⊕kz=yx⊕kz=y.

After printing a query, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict.

To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

If you ask an invalid query or exceed nn queries, you will read r=−1r=−1 and you will receive the Wrong Answer verdict. Make sure to exit immediately to avoid unexpected verdicts.

Note that the interactor is adaptive. That is, the original password is not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial password such that all the answers to the queries are consistent.


To use hacks, use the following format of tests:

The first line should contain a single integer tt (1≤t≤100001≤t≤10000) — the number of test cases.

The first and only line of each test case should contain two integers nn (1≤n≤2⋅1051≤n≤2⋅105) and kk (k=2k=2) denoting the number of queries and the base respectively. The optimal original password is automatically decided by the adaptive interactor.

You must ensure that the sum of nn over all test cases does not exceed 2⋅1052⋅105.



5 2









In the example test 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⊕21=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⊕25=4.

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

Note that this initial password is taken just for the sake of explanation. When you submit, the interactor might behave differently because it is adaptive.


#include <iostream>
#include <vector>
#include <utility>

using namespace std; 

int main() {
	int t; cin >> t;
	while(t--) {
		long n, k; cin >> n >> k; // ignore k (k==2)
		long p = 0, r = 0; // current bitmask
		for(long i=0; !r && (i<n); i++) {
			long y = p ^ i; 
			cout << y << "\n"; 
			cin >> r; 
			p = p ^ y; 
	cout << flush; 

Codeforces Round #730 (Div. 2)

July Long Challenge 2021 Solutions

Weekly Contest 247

Biweekly Contest 55

Codechef Long Challenge Solutions

June Long Challenge 2021 Solutions

March Long Challenge 2021 Solutions

April Long Challenge 2021 Solutions

February Long Challenge 2021

January Long Challenge 2021

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

Leave a Comment

Please Click on 1 or 2 Ads to help us run this site.