【题解】CF1556D Take a Guess
Sol
首先考虑 \(n=3\) 的情况。
记 \(a_1\) 与 \(a_2\) 的和为 \(x_1\),\(a_1\) 与 \(a_3\) 的和为 \(x_2\),\(a_2\) 与 \(a_3\) 的和为 \(x_3\)。
考虑 \(x+y=x \ and \ y +x \ or \ y\)。
那么我们可以通过操作来得出 \(x_1\),\(x_2\),\(x_3\) 的值,从而得出 \(a_1\) 的值。
\(a_2\),\(a_3\) 也可以通过这样的方式来得出。
显然,当 \(n\) 更大时,这个办法同样通用。
我们每求出一个数时,两种操作分别使用了一次。
所以最终的使用次数即为 \(2n\)。
Code
//LYC_music yyds!
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0)
#define int long long
using namespace std;
int read()
{
int pos=1,num=0;
char ch=getchar();
while (!isdigit(ch))
{
if (ch=='-') pos=-1;
ch=getchar();
}
while (isdigit(ch))
{
num=num*10+(int)(ch-'0');
ch=getchar();
}
return pos*num;
}
void write(int x)
{
if (x<0)
{
putchar('-');
write(-x);
return;
}
if (x>=10) write(x/10);
putchar(x%10+'0');
}
void writesp(int x)
{
write(x);
putchar(' ');
}
void writeln(int x)
{
write(x);
putchar('\n');
}
const int N=1e5+1;
int n,k,a[N],b[N];
int work(int u,int v)
{
int now1,now2;
cout<<"or "<<u<<" "<<v<<endl;
fflush(stdout); now1=read();
cout<<"and "<<u<<" "<<v<<endl;
fflush(stdout); now2=read();
return now1+now2;
}
signed main()
{
n=read(); k=read();
b[0]=work(1,3); b[1]=work(1,2); b[2]=work(2,3);
a[1]=(b[1]-b[2]+b[0])/2; a[2]=b[1]-a[1]; a[3]=b[2]-a[2];
for (int i=3;i<n;i++)
{
b[i]=work(i,i+1);
a[i+1]=b[i]-a[i];
}
sort(a+1,a+n+1);
cout<<"finish "<<a[k]<<endl;
fflush(stdout);
}

浙公网安备 33010602011771号