結果

問題 No.2496 LCM between Permutations
ユーザー Dispersion-atcDispersion-atc
提出日時 2024-04-03 00:05:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,006 bytes
コンパイル時間 2,731 ms
コンパイル使用メモリ 212,024 KB
実行使用メモリ 25,476 KB
平均クエリ数 953.76
最終ジャッジ日時 2024-09-30 23:26:15
合計ジャッジ時間 6,000 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
25,196 KB
testcase_01 AC 26 ms
24,836 KB
testcase_02 AC 21 ms
25,476 KB
testcase_03 AC 20 ms
24,580 KB
testcase_04 AC 20 ms
24,836 KB
testcase_05 AC 21 ms
24,580 KB
testcase_06 AC 21 ms
24,964 KB
testcase_07 AC 21 ms
24,580 KB
testcase_08 AC 21 ms
24,824 KB
testcase_09 RE -
testcase_10 AC 22 ms
24,836 KB
testcase_11 AC 22 ms
24,964 KB
testcase_12 AC 21 ms
24,836 KB
testcase_13 AC 21 ms
25,124 KB
testcase_14 AC 27 ms
24,580 KB
testcase_15 AC 29 ms
24,812 KB
testcase_16 AC 29 ms
24,940 KB
testcase_17 AC 29 ms
24,940 KB
testcase_18 AC 104 ms
24,580 KB
testcase_19 AC 73 ms
25,220 KB
testcase_20 AC 140 ms
25,220 KB
testcase_21 AC 101 ms
24,836 KB
testcase_22 AC 95 ms
25,220 KB
testcase_23 AC 177 ms
25,148 KB
testcase_24 AC 107 ms
24,856 KB
testcase_25 AC 186 ms
24,580 KB
testcase_26 AC 188 ms
24,940 KB
testcase_27 AC 186 ms
24,556 KB
testcase_28 AC 186 ms
24,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef long double ld;

template<class T>
bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>
bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }

ll gcd(ll a, ll b)
{
    if (a % b == 0) {return b;}
    else {return gcd(b, a % b);}
}

ll lcm(ll a, ll b)
{
    return a / gcd(a, b) * b;
}


ll query(ll i, ll j)
{
    cout << "?" << " " << i+1 << " " << j+1 << endl;
    ll X; cin >> X;
    return X;
}

void output(vector<ll> &A, vector<ll> &B)
{
    assert(A.size() == B.size());
    cout << "!" << " ";
    for (auto a : A) cout << a << " ";
    for (auto b : B) cout << b << " ";
    cout << endl;
    return;
}

mt19937 engine(42);
const ll nmax = 1010;
bool isprime[nmax];

const ll INF = 1e18;

void init()
{
    for (int i = 2; i < nmax; ++i)
    {
        bool flg = true;
        for (int j = 2; j * j <= i; ++j)
        {
            if (i % j == 0) flg = false;
        }
        isprime[i] = flg;
    }
    
    return;
}

int main()
{
    
    init();
    
    ll N; cin >> N;
    
    if (N <= 2)
    {
        if (N == 1)
        {
            cout << "!" << " " << 1 << " " << 1 << endl;
            return 0;
        }
        else
        {
            ll q00 = query(0, 0);
            ll q01 = query(0, 1);
            ll q10 = query(1, 0);
            ll q11 = query(1, 1);
            
            vector<ll> A(N), B(N);
            {
                if (q00 == q01) A[0] = 2;
                else A[0] = 1;
                A[1] = 3 - A[0];
            }
            {
                if (q00 == q10) B[0] = 2;
                else B[0] = 1;
                B[1] = 3 - B[0];
            }
            output(A, B);
            return 0;
        }
        
        return 0;
    }
    
    
    ll a0 = INF, p = -1;
    ll bp = -1;
    {
        ll f = engine() % N;
        vector<ll> b(N);
        for (int i = 0; i < N; ++i) b[i] = query(f, i);
        for (auto bb : b) chmin(a0, bb);
        
        for (int n = 1; n <= N; ++n)
        {
            if (isprime[n] && gcd(a0, n) == 1) p = n;
        }
        
        assert(p != -1);
        ll lc = lcm(a0, p);
        for (int i = 0; i < N; ++i)
        {
            if (b[i] == lc) bp = i;
        }
    }
    // cerr << p << endl;
    
    vector<ll> A(N), B(N);
    {
        vector<ll> veca(N);
        vector<ll> pid;
        for (int i = 0; i < N; ++i)
        {
            ll a = query(i, bp);
            veca[i] = a;
            
            for (int j = 1; j <= N; ++j)
            {
                if (lcm(j, p) == a) A[i] = j;
            }
            
            if (a == p) pid.emplace_back(i);
        }
        
        vector<ll> vecb(N);
        assert((ll)pid.size() == 2);
        ll ap0 = pid[0], ap1 = pid[1];
        bool isone = false;
        for (int i = 0; i < N; ++i)
        {
            vecb[i] = query(ap0, i);
            if (vecb[i] == 1) isone = true;
        }
        
        if (isone)
        {
            A[ap0] = 1, A[ap1] = p;
            for (int i = 0; i < N; ++i)
            {
                B[i] = vecb[i];
            }
        }
        else
        {
            A[ap0] = p, A[ap1] = 1;
            for (int i = 0; i < N; ++i)
            {
                ll b = vecb[i];
                for (int j = 1; j <= N; ++j)
                {
                    if (lcm(p, j) == b) B[i] = j;
                }
                
                if (b == p)
                {
                    if (i == bp) B[i] = p;
                    else B[i] = 1;
                }
            }
        }
    }
    
    vector<ll> copya = A, copyb = B;
    sort(copya.begin(), copya.end());
    sort(copyb.begin(), copyb.end());
    for (int i = 0; i < N; ++i)
    {
        // cout << i << " " << copya[i] << " " << copyb[i] << endl;
        assert(copya[i] == i+1);
        assert(copyb[i] == i+1);
    }
    
    output(A, B);
    
    return 0;
}
0