結果

問題 No.3256 Permutation Equation
ユーザー 👑 hos.lyric
提出日時 2025-09-05 22:20:11
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 2,593 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 108,868 KB
実行使用メモリ 22,160 KB
最終ジャッジ日時 2025-09-05 22:20:49
合計ジャッジ時間 10,583 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// (+1) o P = ((+1) o q)^2

int[] solve(const(int[]) P) {
  const N = cast(int)(P.length);
  auto A = new int[N];
  foreach (u; 0 .. N) A[u] = (P[u] + 1) % N;
  auto C = new int[][][N + 1];
  auto vis = new bool[N];
  foreach (u; 0 .. N) if (!vis[u]) {
    int[] cyc;
    for (int v = u; !vis[v]; v = A[v]) {
      vis[v] = true;
      cyc ~= v;
    }
    C[cyc.length] ~= cyc;    
  }
  debug writeln("C = ", C);
  auto ans = new int[N];
  ans[] = -1;
  foreach (l; 1 .. N + 1) {
    if (l & 1) {
      foreach (cyc; C[l]) {
        foreach (j; 0 .. l) {
          ans[cyc[j]] = cyc[(j + (l+1)/2) % l];
        }
      }
    } else {
      if (C[l].length & 1) return null;
      for (int k = 0; k < C[l].length; k += 2) {
        const cyc0 = C[l][k + 0];
        const cyc1 = C[l][k + 1];
        foreach (j; 0 .. l) {
          ans[cyc0[j]] = cyc1[j];
          ans[cyc1[j]] = cyc0[(j + 1) % l];
        }
      }
    }
  }
  foreach (u; 0 .. N) (ans[u] += (N - 1)) %= N;
  return ans;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt;
      auto P = new int[N];
      foreach (i; 0 .. N) P[i] = readInt - 1;
      
      const ans = solve(P);
      if (ans) {
        writeln("Yes");
        foreach (i; 0 .. N) {
          if (i) write(" ");
          write(ans[i] + 1);
        }
        writeln;
      } else {
        writeln("No");
      }
    }
  } catch (EOFException e) {
  }
}
0