传送门
文章目录
- 题意:
- 思路:
题意:
定义a⊕kba\oplus_k ba⊕kb为a,ba,ba,b在kkk进制下的不进位加法。系统会随机生成一个数xxx,你猜这个数,假设当前猜的数为yyy,如果猜对了就返回111,否则要猜的数会变成zzz,其中x⊕kz=bx\oplus_k z=bx⊕kz=b。其随机生成的数0≤x<n0\le x< n0≤x<n,你可以询问nnn次。
n≤2e5,k≤100n\le2e5,k\le100n≤2e5,k≤100
思路:
由easyeasyeasy版本的异或自反性得到启发,能否在kkk进制的情况下也能利用某种方式来消除上一次询问对原始答案造成的影响呢?
考虑将xxx移动到右边,即z=b⊖kxz=b\ominus_k xz=b⊖kx,这样就可以得到zzz了。
设原始答案为preprepre,当前猜的数为guessguessguess,当前变成的答案为nownownow,那么有pre⊕know=guesspre\oplus_k now=guesspre⊕know=guess。
一开始还是从0开始,先将guess=0guess=0guess=0,之后得到now=0⊖kprenow=0\ominus_k prenow=0⊖kpre,之后就可以按照对应的形式,比如下一次需要判断111是否合法,那么就让preprepre的位置变成111,即让guess=(1−1)⊖k1guess=(1-1)\ominus_k 1guess=(1−1)⊖k1,这个时候now=pre⊖k1now=pre\ominus_k 1now=pre⊖k1,让后再让preprepre的位置为222,即guess=2⊖k(2−1)guess=2\ominus_k(2-1)guess=2⊖k(2−1),现在now=2⊖kprenow=2\ominus_kprenow=2⊖kpre,这样一直循环下去即可,可以证明这样是一定可以找到答案的。
综上,答案就是ifi=0guess=0if\ \ i=0 \ \ guess=0if i=0 guess=0ifimod2=1guess=(i−1)⊖kiif\ \ i\bmod 2=1 \ \ guess=(i-1)\ominus_kiif imod2=1 guess=(i−1)⊖ki ifimod2=0guess=i⊖k(i−1)if\ \ i\bmod2=0 \ \ guess=i\ominus_k(i-1)if imod2=0 guess=i⊖k(i−1)
复杂度nlogknnlog_knnlogkn
// 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;
}
/**/