codeforces 872 D. Something with XOR Queries(思维)

题目链接:http://codeforces.com/contest/872/problem/D

题意:给你一个排列p和对应的位置b也就是说p[b[i]]=i,然后给你最多询问2*n次找出所有的p排列,然后任意输出一个。

题解:其实是一道水题要知道一共n个数询问2*n次也就是说能够询问p[0]^b[i](0<=i<n)和p[i]^b[0](0<=i<n)然后只要给p[0]赋一个值所有的值就都出来了然后就简单了。。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int ans[5432][5432] , p[5432] , b[5432] , p_true[5432] , b_true[5432];
int main() {
    int n;
    scanf("%d" , &n);
    int x;
    for(int i = 0 ; i < n ; i++) {
        printf("? %d %d
" , i , 0);
        fflush(stdout);
        scanf("%d" , &x);
        ans[i][0] = x;
    }
    for(int i = 1 ; i < n ; i++) {
        printf("? %d %d
" , 0 , i);
        fflush(stdout);
        scanf("%d" , &x);;
        ans[0][i] = x;
    }
    int res = 0;
    for(int i = 0 ; i < n ; i++) {
        memset(p , -1 , sizeof(p));
        memset(b , -1 , sizeof(b));
        p[0] = i;
        for(int j = 0 ; j < n ; j++) {
            b[j] = (ans[0][j] ^ p[0]);
        }
        for(int j = 1 ; j < n ; j++) {
            p[j] = (ans[j][0] ^ b[0]);
        }
        int flag = 0;
        for(int j = 0 ; j < n ; j++) {
            if(p[b[j]] != j || p[j] >= n || b[j] >= n) {
                flag = 1;
                break;
            }
        }
        if(!flag) {
            res++;
            for(int j = 0 ; j < n ; j++) {
                p_true[j] = p[j];
                b_true[j] = b[j];
            }
        }
    }
    printf("!
");
    printf("%d
" , res);
    for(int i = 0 ; i < n ; i++){
        printf("%d " , p_true[i]);
    }
    printf("
");
    fflush(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/TnT2333333/p/7677569.html