CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)

12 篇文章 0 订阅
5 篇文章 1 订阅

题目链接:点击查看

题目大意:交互题猜密码,设原密码为 x x x,猜的密码为 y y y,如果没猜到,密码会自适应变成 z z z,满足 x ⊕ z = y x \oplus z=y xz=y ,最多猜 n n n

题目分析:首先不难看出, x ⊕ z = y x \oplus z=y xz=y,新密码 z z z 其实就是 x ⊕ y x \oplus y xy

一开始想按位拆分的,可惜没什么思路,偏偏忘记了异或运算最重要的一个性质,就是它的自反性

那么我们就假设每次密码都不变,然后挨个猜不就行了吗?

假设我们已经尝试过的询问的异或和为 s u m sum sum,即 s u m = q 1 ⊕ q 2 ⊕ . . . ⊕ q k sum=q_1 \oplus q_2 \oplus ... \oplus q_k sum=q1q2...qk q i q_i qi 代表我们第 i i i 次猜的数字。再假设密码初始时为 p a s s w o r d password password,现在密码已经变成了 p a s s w o r d ⊕ s u m password \oplus sum passwordsum。新的一轮我打算猜 p a s s w o r d password password 是否等于 x x x,那么我们只需要去询问此时的密码是否等于 x ⊕ s u m x \oplus sum xsum 即可,因为判断 p a s s w o r d ⊕ s u m password \oplus sum passwordsum x ⊕ s u m x \oplus sum xsum 是否相等,只需要根据异或运算的自反性就能得到了。

所以依次枚举就可以了

D2 明天再看吧,感觉对比本题而言,应该只是多了个模拟罢了

代码:

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

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
    T f=1;x=0;
    char ch=getchar();
    while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    x*=f;
}
template<typename T>
inline void write(T x)
{
    if(x<0){x=~(x-1);putchar('-');}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
int main()
{
#ifndef ONLINE_JUDGE
//	freopen("data.in.txt","r",stdin);
//	freopen("data.out.txt","w",stdout);
#endif
//	ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--) {
		int n,k,sum=0;
		read(n),read(k);
		for(int i=0;i<n;i++) {
			printf("%d\n",sum^i);
			fflush(stdout);
			int ans;
			scanf("%d",&ans);
			if(ans==1) {
				break;
			} else {
				sum^=sum^i;
			}
		}
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frozen_Guardian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值