Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries



D. Something with XOR Queries
time limit per test
2 seconds
memory limit per test
256 megabytes
standard input
standard output

This is an interactive problem.

Jury has hidden a permutation p of integers from 0 to n - 1. You know only the length n. Remind that in permutation all integers are distinct.

Let b be the inverse permutation for p, i.e. pbi = i for all i. The only thing you can do is to ask xor of elements pi and bj, printing two indices i and j (not necessarily distinct). As a result of the query with indices i and j you'll get the value , where  denotes the xoroperation. You can find the description of xor operation in notes.

Note that some permutations can remain indistinguishable from the hidden one, even if you make all possible n2 queries. You have to compute the number of permutations indistinguishable from the hidden one, and print one of such permutations, making no more than 2nqueries.

The hidden permutation does not depend on your queries.


The first line contains single integer n (1 ≤ n ≤ 5000) — the length of the hidden permutation. You should read this integer first.


When your program is ready to print the answer, print three lines.

In the first line print "!".

In the second line print single integer answers_cnt — the number of permutations indistinguishable from the hidden one, including the hidden one.

In the third line print n integers p0, p1, ..., pn - 1 (0 ≤ pi < n, all pi should be distinct) — one of the permutations indistinguishable from the hidden one.

Your program should terminate after printing the answer.


To ask about xor of two elements, print a string "? i j", where i and j — are integers from 0 to n - 1 — the index of the permutation element and the index of the inverse permutation element you want to know the xor-sum for. After that print a line break and make flushoperation.

After printing the query your program should read single integer — the value of .

For a permutation of length n your program should make no more than 2n queries about xor-sum. Note that printing answer doesn't count as a query. Note that you can't ask more than 2n questions. If you ask more than 2n questions or at least one incorrect question, your solution will get "Wrong answer".

If at some moment your program reads -1 as an answer, it should immediately exit (for example, by calling exit(0)). You will get "Wrong answer" in this case, it means that you asked more than 2n questions, or asked an invalid question. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.

Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including for the final answer .

To flush you can use (just after printing line break):

  • fflush(stdout) in C++;
  • System.out.flush() in Java;
  • stdout.flush() in Python;
  • flush(output) in Pascal;
  • For other languages see the documentation.


For hacking use the following format:


pp1 ... pn - 1

Contestant programs will not be able to see this input.







 1 #include <bits/stdc++.h>
 3 using namespace std;
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
14 int n,a[K],b[K],cnt,ans[K],sa[K],sb[K];
16 int main(void)
17 {
18     ans[0]=-1;
19     scanf("%d",&n);
20     for(int i=0;i<n;i++)
21     {
22         printf("? %d %d\n",i,0);
23         fflush(stdout);
24         scanf("%d",a+i);
25     }
26     for(int i=0;i<n;i++)
27     {
28         printf("? %d %d\n",0,i);
29         fflush(stdout);
30         scanf("%d",b+i);
31     }
32     for(int i=0;i<n;i++)
33     {
34         int ff=1;
35         for(int j=0;j<n;j++)
36             sa[j]=i^a[j];
37         for(int j=0;j<n;j++)
38             sb[j]=sa[0]^b[j];
39         for(int j=0;j<n&&ff;j++)
40         if(sa[sb[j]]!=j)
41             ff=0;
42         cnt+=ff;
43         if(ans[0]==-1&&ff)
44         {
45             for(int j=0;j<n;j++)
46                 ans[j]=sa[j];
47         }
48     }
49     printf("!\n%d\n",cnt);
50     for(int i=0;i<n;i++)
51         printf("%d ",ans[i]);
52     fflush(stdout);
53     return 0;
54 }


posted @ 2017-10-15 19:47  weeping  阅读(324)  评论(0编辑  收藏  举报