CF870D Something with XOR Queries(异或+暴力枚举)
来源:互联网 发布:淘宝券 编辑:程序博客网 时间:2024/06/07 18:54
首先考虑如果n^2的询问我们都问了,如何算出答案。我们已知了两两数之间的异或和,则我们可以枚举p0的取值,根据p0的取值算出所有数的取值,然后去检查是否合法即可。那我们现在最多只能问2n个问题,如何实现n^2个问题的效果呢?我们先得到p0与bi的所有异或值,用掉n个问题,然后再得到b0与pi的所有异或值,用掉n-1个问题。
对于p0^pi,可以直接通过p0^b0^b0^pi得到。然后对于pi^bj可以通过pi^p0^p0^bj得到。这样我们就得到了所有的n^2个问题。所以我们还是枚举p0的取值,算出pi的值,然后推出bi的值,最后判断是否合法。复杂度O(n^2)
#include <bits/stdc++.h>using namespace std;#define ll long long#define inf 0x3f3f3f3f#define N 5010inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,b[N],p[N],a[N],bb[N],ans=0,res[N];bool check(int x){ a[0]=x; for(int i=1;i<n;++i) if((a[i]=p[i]^a[0])>=n) return 0; for(int i=0;i<n;++i) bb[a[i]]=i; for(int i=0;i<n;++i) if((bb[i]^a[0])!=b[i]) return 0; return 1;}int main(){// freopen("a1.in","r",stdin); n=read(); for(int i=0;i<n;++i){ printf("? 0 %d\n",i);fflush(stdout);b[i]=read();//p0^bi }for(int i=1;i<n;++i){ printf("? %d 0\n",i);fflush(stdout);p[i]=read()^b[0];//p0^pi }for(int i=0;i<n;++i) if(check(i)) if(++ans==1) for(int j=0;j<n;++j) res[j]=a[j]; printf("!\n%d\n",ans); for(int i=0;i<n;++i) printf("%d ",res[i]); return 0;}
阅读全文
0 0
- CF870D Something with XOR Queries(异或+暴力枚举)
- CF 870D. Something with XOR Queries 交互,思维
- Codeforces Round #440 (Div. 1) B. Something with XOR Query
- 加密101-异或(xor)
- XOR (异或)加密简单实现
- 神经网络实现异或(XOR)
- BZOJ2115: [Wc2011] Xor(异或方程组)
- xor 逻辑异或
- 口算异或xor
- XOR异或
- 异或运算xor
- Codeforces 617E XOR and Favorite Number (区间异或和 莫队算法 分块暴力)
- HDU 5175 Misaki's Kiss again(暴力枚举+异或运算)
- HDU 4810 Wall Painting【二进制+异或+暴力枚举】
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
- SPOJ-Matrices with XOR property,暴力打表!
- SPOJ-VECTAR1 Matrices with XOR property(二维BIT/暴力)
- CodeChef XRQRS - Xor Queries
- 预处理器,编译器,汇编器,连接器
- vmware磁盘动态扩容
- 1.安装jdk
- 选择数据源(SQL Server 导入和导出向导)
- C++字符串输入
- CF870D Something with XOR Queries(异或+暴力枚举)
- 安心360CTO:谈自建手机定位数据库的必要性
- 【Java学习4.2.1】if条件语句
- C 常量
- jquery.cityselect.js
- hdu 1028 回溯法
- BP,RNN 和 LSTM暨《Supervised Sequence Labelling with Recurrent Neural Networks-2012》阅读笔记
- MFC 修改Button的颜色、Button不可用
- 基于C++和MFC遍历指定文件夹下指定格式的文件