An Interesting Mathematical Interactive Problem
Difference between en15 and en16, changed 0 character(s)
Problem Name: Chocolate Bunny↵
------------------↵
Problem Rating: 1600↵

Problem Link: https://mirror.codeforces.com/problemset/problem/1407/C↵

[If I make any mistake, please pardon me & correct me!]↵

In this problem, we can ask $2$ indices $(x,y)$ in 1 query & get $P_{x} \bmod P_{y}$. Our limitation is we can ask no more than $2\cdot n$ queries. Here is the main trick! If we were allowed to ask $n^{2}$ queries, we could easily do it asking `? x y` for each $x (1->n)$ and each $y (1->n $ except $x)$↵

e.g. if $n=5$, to get $a[3]$ we can ask↵

`?1 3`↵

`?2 3`↵

`?4 3`↵

`?5 3`↵

All these results must be all numbers $1<=r<a[3]$($r$ can also be $0$ for some numbers), so $a[3]=max($ all remainders $)+1$↵

But as we can ask no more than $2\cdot n$ queries, we have to focus on the mathematical property of $MOD$.↵

Let's think about $a \bmod b$ & $b \bmod a$: If $a>b$, then $a \bmod b<b$; and $b \bmod a = b> a \bmod b$↵

So if we ask `? x y` and `? y x`, we get $a[x]\bmod a[y]$ and $a[y]\bmod a[x]$, Between these, the max one is the real one that exists in the Permutation & the min one is the changed remainder of the max one. So if $a[x]\bmod a[y]>a[y]\bmod a[x]$, then $P[x]=a[x]$, then $x++$ and compare $x$ with the previous $y$.And if $a[x]\bmod a[y]<a[y]\bmod a[x]$, $P[y]=a[y], y=x, x++$.[Here we are iterating over x]↵

 Thus we can get $(n-1)$ numbers except the max one which we all know to be $n$.↵

Here Queries used $2\cdot (n-1)=2\cdot n-2$↵

Complexity: $O(2\cdot n)$↵

My Submission: https://mirror.codeforces.com/contest/1407/submission/320641205 ↵

Solution:↵

```c++↵
#include<bits/stdc++.h>↵
using namespace std;↵


#define TT1 int TC=1;for(int ti=1;ti<=TC;ti++)↵
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);↵

int main()↵
{↵
    fastio↵
    TT1↵
    {↵
        int n;↵
        cin>>n;↵
        int a[n+1];↵
        for(int i=1;i<=n;i++)a[i]=n;↵
        int j=1;↵
        for(int i=2;i<=n;i++){↵
            int r1,r2;↵
            cout<<"? "<<j<<" "<<i<<endl;↵
            cin>>r1;↵
            cout<<"? "<<i<<" "<<j<<endl;↵
            cin>>r2;↵
            if(r1>r2){↵
                a[j]=r1;↵
                j=i;↵
            }↵
            else{↵
                a[i]=r2;↵
            }↵

        }↵
        cout<<"! ";↵
        for(int i=1;i<=n;i++)cout<<a[i]<<" ";↵
            cout<<endl;↵

        ↵
    }↵
    return 0;↵
}↵
```↵

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en16 English salehin_076923 2025-05-22 09:41:32 0 (published)
en15 English salehin_076923 2025-05-22 09:41:05 7 Tiny change: 'ot (n-1)=2*n-2$\n\nCo' -> 'ot (n-1)=2\cdot n-2$\n\nCo'
en14 English salehin_076923 2025-05-22 09:40:06 20
en13 English salehin_076923 2025-05-22 09:38:12 11
en12 English salehin_076923 2025-05-22 09:36:23 1288
en11 English salehin_076923 2025-05-22 09:27:21 15
en10 English salehin_076923 2025-05-21 22:25:23 12
en9 English salehin_076923 2025-05-21 22:21:16 6 Tiny change: 'n\n`?1 3\n\n?2 3\n\n?4 3\n\n?5 3`\n\' -> 'n\n`?1 3\n?2 3\n?4 3\n?5 3`\n\'
en8 English salehin_076923 2025-05-21 22:20:49 12
en7 English salehin_076923 2025-05-21 22:19:56 14 (saved to drafts)
en6 English salehin_076923 2025-05-21 17:10:25 2 Tiny change: ' over x]\n Thus we' -> ' over x]\n\n Thus we'
en5 English salehin_076923 2025-05-21 17:09:11 9 Tiny change: ' mod a = b<b\n\nSo if' -> ' mod a = b> a mod b\n\nSo if'
en4 English salehin_076923 2025-05-21 17:07:35 22
en3 English salehin_076923 2025-05-21 17:05:52 798
en2 English salehin_076923 2025-05-21 17:03:30 22
en1 English salehin_076923 2025-05-21 17:00:33 2257 Initial revision (published)