题意:
交互题,有一个初始密码 x x x,只有 n n n次猜测机会,假如猜了 y y y, x ! = y x!=y x!=y,那么密码就会变成 x ⨁ y x\bigoplus y x⨁y。
题解:
设第 i i i次猜测的数为 p i p_i pi,那么令 p i = ( i − 1 ) ⨁ ( i − 2 ) p_i=(i-1) \bigoplus (i-2) pi=(i−1)⨁(i−2) ,特别的, p 1 = 0 p_1=0 p1=0 。那么每次猜测完之后,密码的变换就有如下规律:
0 ⨁ x 0\bigoplus x 0⨁x
1 ⨁ x 1\bigoplus x 1⨁x
2 ⨁ x 2\bigoplus x 2⨁x
3 ⨁ x 3\bigoplus x 3⨁x
. . . . . . ...... ......
所以第 x x x次猜测后,密码会变为 ( x − 1 ) ⨁ x (x-1)\bigoplus x (x−1)⨁x ,而第 x + 1 x+1 x+1次 p x + 1 = ( x − 1 ) ⨁ x p_{x+1}=(x-1)\bigoplus x px+1=(x−1)⨁x 因此可以猜到。
代码:
#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=2e5+5;
const int inf=0x3f3f3f3f;
int main()
{
int t;
cin>>t;
while(t--){
int n,k,x;
cin>>n>>k;
for(int i=0;i<=n-1;i++){
if(i==0)
{
cout<<i<<endl;
}
else
{
cout<<(i^(i-1))<<endl;
}
cin>>x;
if(x==1)
{
break;
}
}
}
}