CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)
題目鏈接:點(diǎn)擊查看
題目大意:交互題猜密碼,設(shè)原密碼為 xxx,猜的密碼為 yyy,如果沒(méi)猜到,密碼會(huì)自適應(yīng)變成 zzz,滿(mǎn)足 x⊕z=yx \oplus z=yx⊕z=y ,最多猜 nnn 次
題目分析:首先不難看出,x⊕z=yx \oplus z=yx⊕z=y,新密碼 zzz 其實(shí)就是 x⊕yx \oplus yx⊕y
一開(kāi)始想按位拆分的,可惜沒(méi)什么思路,偏偏忘記了異或運(yùn)算最重要的一個(gè)性質(zhì),就是它的自反性
那么我們就假設(shè)每次密碼都不變,然后挨個(gè)猜不就行了嗎?
假設(shè)我們已經(jīng)嘗試過(guò)的詢(xún)問(wèn)的異或和為 sumsumsum,即 sum=q1⊕q2⊕...⊕qksum=q_1 \oplus q_2 \oplus ... \oplus q_ksum=q1?⊕q2?⊕...⊕qk?,qiq_iqi? 代表我們第 iii 次猜的數(shù)字。再假設(shè)密碼初始時(shí)為 passwordpasswordpassword,現(xiàn)在密碼已經(jīng)變成了 password⊕sumpassword \oplus sumpassword⊕sum。新的一輪我打算猜 passwordpasswordpassword 是否等于 xxx,那么我們只需要去詢(xún)問(wèn)此時(shí)的密碼是否等于 x⊕sumx \oplus sumx⊕sum 即可,因?yàn)榕袛?password⊕sumpassword \oplus sumpassword⊕sum 和 x⊕sumx \oplus sumx⊕sum 是否相等,只需要根據(jù)異或運(yùn)算的自反性就能得到了。
所以依次枚舉就可以了
D2 明天再看吧,感覺(jué)對(duì)比本題而言,應(yīng)該只是多了個(gè)模擬罷了
代碼:
// 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; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1543D1 RPD and Rap Sheet (Easy Version)(异或+交互)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AcWing - 246. 区间最大公约
- 下一篇: CodeForces - 1543D2