結果
| 問題 |
No.2496 LCM between Permutations
|
| ユーザー |
shobonvip
|
| 提出日時 | 2023-10-06 23:00:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,631 bytes |
| コンパイル時間 | 2,597 ms |
| コンパイル使用メモリ | 214,880 KB |
| 最終ジャッジ日時 | 2025-02-17 05:30:01 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 24 RE * 4 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
struct Queries{
int n;
vector<vector<bool>> checked;
vector<vector<int>> val;
int cnt = 0;
Queries(int _n) : n(_n) {
checked.resize(n);
val.resize(n);
for (int i=0; i<n; i++){
checked[i].resize(n);
val[i].resize(n);
}
}
int question(int i, int j){
if (checked[i][j]){
return val[i][j];
}
//assert(cnt < 3*n);
cout << "? " << i+1 << ' ' << j+1 << endl;
int x; cin >> x;
val[i][j] = x;
checked[i][j] = true;
cnt++;
return val[i][j];
}
void answer(vector<int> a, vector<int> b){
cout << "! ";
for (int i=0; i<n; i++){
cout << a[i] << ' ';
}
for (int i=0; i<n; i++){
cout << b[i];
if (i == n-1) cout << endl;
else cout << ' ';
}
}
};
int main(){
int n; cin >> n;
if (n == 1){
cout << "! 1 1" << endl;
return 0;
}
vector<bool> p(n+1, true);
for (int i=2; i<=n; i++){
if (!p[i]) continue;
for (int j=i*2; j<=n; j+=i){
p[j] = false;
}
}
int P = -1;
for (int i=n; i>=2; i--){
if (p[i]){
P = i;
break;
}
}
assert(P >= 2);
// STEP 1 ... find max sosuu P less than or equal N either on A or B
// if gcd(A[i], B) = P, then A[i] = P
// if gcd(A[i], B[j]) = P * ??, then B[i] = P
//
// STEP 2 ...
// WLOG assume A[i] = P
// check all lcm(A[i], B[j])
// if (A[i], B[j]) = A[i] = P, then B[j] = 1 or P.
// check lcm(A[k], B[j])
//
// STEP 3 ...
// you have B[j] = P, so check all.
Queries que(n);
int now = 0;
vector<int> a(n,-1), b(n,-1);
// N
for (int i=0; i<n; i++){
int x = que.question(0, i);
now = __gcd(now, x);
}
vector<int> kouho;
char wherep;
if (now == P){
wherep = 'A';
for (int i=0; i<n; i++){
if (que.question(0, i) == P){
kouho.push_back(i);
}else{
b[i] = que.question(0, i) / P;
}
}
}else{
wherep = 'B';
int targ = -1;
for (int i=0; i<n; i++){
if (que.question(0, i) % P == 0){
b[i] = P;
assert(targ == -1);
targ = i;
}
}
assert(targ >= 0);
for (int i=0; i<n; i++){
que.question(i, targ);
}
// N-1
for (int i=0; i<n; i++){
if (que.question(i, targ) == P){
kouho.push_back(i);
}else{
a[i] = que.question(i, targ) / P;
}
}
}
assert(kouho.size() == 2);
for (int i=0; i<(int)kouho.size(); i++){
if (wherep == 'A'){
// 1
if (que.question(1, kouho[i]) % P == 0){
b[kouho[i]] = P;
b[kouho[i^1]] = 1;
}else{
b[kouho[i]] = 1;
b[kouho[i^1]] = P;
}
break;
}else{
int targ = -1;
for (int j=0; j<n; j++){
if (b[j] != P){
targ = j;
break;
}
}
assert(targ >= 0);
// 1
if (que.question(kouho[i], targ) % P == 0){
a[kouho[i]] = P;
a[kouho[i^1]] = 1;
}else{
a[kouho[i]] = 1;
a[kouho[i^1]] = P;
}
break;
}
}
{
int targ = -1;
for (int i=0; i<n; i++){
if (a[i] == P){
targ = i;
break;
}
}
assert(targ >= 0);
for (int i=0; i<n; i++){
int x = que.question(targ, i);
if (x == P && b[i] == -1){
b[i] = 1;
}
if (x != P && b[i] == -1){
b[i] = x / P;
}
}
}
//que.answer(a, b);
{
int targ = -1;
for (int i=0; i<n; i++){
if (b[i] == P){
targ = i;
break;
}
}
assert(targ >= 0);
for (int i=0; i<n; i++){
int x = que.question(i, targ);
if (x == P && a[i] == -1){
a[i] = 1;
}
if (x != P && a[i] == -1){
a[i] = x / P;
}
}
}
//que.answer(a, b);
set<int> as, bs;
for (int i=0; i<n; i++){
assert(a[i] >= 1);
as.insert(a[i]);
}
for (int i=0; i<n; i++){
assert(b[i] >= 1);
bs.insert(b[i]);
}
assert((int)as.size() == n);
assert((int)bs.size() == n);
que.answer(a, b);
}
shobonvip