Codeforces Round #730 (Div. 2) D2. RPD and Rap Sheet (Hard Version) 交互 + k进制的转换

司徒捷
2023-12-01

传送门

题意:

定义 a ⊕ k b a\oplus_k b akb a , b a,b a,b k k k进制下的不进位加法。系统会随机生成一个数 x x x,你猜这个数,假设当前猜的数为 y y y,如果猜对了就返回 1 1 1,否则要猜的数会变成 z z z,其中 x ⊕ k z = b x\oplus_k z=b xkz=b。其随机生成的数 0 ≤ x < n 0\le x< n 0x<n,你可以询问 n n n次。
n ≤ 2 e 5 , k ≤ 100 n\le2e5,k\le100 n2e5,k100

思路:

e a s y easy easy版本的异或自反性得到启发,能否在 k k k进制的情况下也能利用某种方式来消除上一次询问对原始答案造成的影响呢?
考虑将 x x x移动到右边,即 z = b ⊖ k x z=b\ominus_k x z=bkx,这样就可以得到 z z z了。
设原始答案为 p r e pre pre,当前猜的数为 g u e s s guess guess,当前变成的答案为 n o w now now,那么有 p r e ⊕ k n o w = g u e s s pre\oplus_k now=guess preknow=guess
一开始还是从0开始,先将 g u e s s = 0 guess=0 guess=0,之后得到 n o w = 0 ⊖ k p r e now=0\ominus_k pre now=0kpre,之后就可以按照对应的形式,比如下一次需要判断 1 1 1是否合法,那么就让 p r e pre pre的位置变成 1 1 1,即让 g u e s s = ( 1 − 1 ) ⊖ k 1 guess=(1-1)\ominus_k 1 guess=(11)k1,这个时候 n o w = p r e ⊖ k 1 now=pre\ominus_k 1 now=prek1,让后再让 p r e pre pre的位置为 2 2 2,即 g u e s s = 2 ⊖ k ( 2 − 1 ) guess=2\ominus_k(2-1) guess=2k(21),现在 n o w = 2 ⊖ k p r e now=2\ominus_kpre now=2kpre,这样一直循环下去即可,可以证明这样是一定可以找到答案的。
综上,答案就是 i f    i = 0    g u e s s = 0 if\ \ i=0 \ \ guess=0 if  i=0  guess=0 i f    i   m o d   2 = 1    g u e s s = ( i − 1 ) ⊖ k i if\ \ i\bmod 2=1 \ \ guess=(i-1)\ominus_ki if  imod2=1  guess=(i1)ki i f    i   m o d   2 = 0    g u e s s = i ⊖ k ( i − 1 ) if\ \ i\bmod2=0 \ \ guess=i\ominus_k(i-1) if  imod2=0  guess=ik(i1)

复杂度 n l o g k n nlog_kn nlogkn

// Problem: D2. RPD and Rap Sheet (Hard Version)
// Contest: Codeforces - Codeforces Round #730 (Div. 2)
// URL: https://codeforces.com/contest/1543/problem/D2
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,k;

int query(int x) {
	printf("%d\n",x);
	fflush(stdout);
	int now; scanf("%d",&now);
	return now;
}

int get1(int l,int r) {
	int ans=0;
	int fun=1;
	while(l||r) {
		ans+=(l%k+r%k)%k*fun;
		l/=k; r/=k;
		fun*=k;
	}
	return ans;
} 
 
int get2(int l,int r) {
	int ans=0;
	int fun=1;
	while(l||r) {
		ans+=(r%k-l%k+k)%k*fun;
		l/=k; r/=k;
		fun*=k;
	}
	return ans;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	int _; scanf("%d",&_);
	while(_--) {
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++) {
			if(i==0) {
				int x=query(0);
				if(x) break;
			} else {
				int x;
				if(i%2==1) x=query(get2(i,i-1));
				else x=query(get2(i-1,i));
				if(x) break;
			}
		}
	}



	return 0;
}
/*

*/









 类似资料: