結果
問題 | No.3004 ヤング図形 |
ユーザー |
![]() |
提出日時 | 2025-01-05 13:53:16 |
言語 | D (dmd 2.109.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,778 bytes |
コンパイル時間 | 745 ms |
コンパイル使用メモリ | 93,792 KB |
実行使用メモリ | 13,772 KB |
最終ジャッジ日時 | 2025-01-05 21:14:53 |
合計ジャッジ時間 | 25,088 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | RE * 3 |
other | WA * 2 RE * 16 TLE * 7 |
ソースコード
import std.algorithm;uint[] to_cycles(uint[] s) {uint[] result;auto N = s.length;auto set = new bool[N];foreach (i; 0 .. N) {if (set[i]) continue;auto n = i;uint size;do {++size;set[n] = true;n = s[n];} while (n != i);result ~= size;}return result;}alias F = FiniteField!998244353;F factorial(ulong N) {auto f = F(1);foreach (i; 0 .. N) f *= (i + 1);return f;}ulong solve(uint[] s) {auto N = s.length;auto type = to_cycles(s).sort.group; // 頻度表auto result = factorial(N);foreach (t; type) {result /= ((factorial(t[0]) ^^ t[1]) * factorial(t[1]));}return result.n;}void main() {import std.array, std.conv, std.stdio;readln;readln.split.to!(uint[]).solve().writeln;}// the struct of finite fields with p elements// p must be a prime numberstruct FiniteField(long p)if (p > 1){ulong n;this(long n) {if (n < 0) this.n = n%p + p;else this.n = n%p;}FiniteField!p opUnary(string op: "+")() {return this;}FiniteField!p opUnary(string op: "-")() {return FiniteField!p(-n);}FiniteField!p opBinary(string op)(long rhs) {static if (op == "^^") {if (rhs < 0) { return this.inv() ^^ rhs; }auto result = FiniteField!p(1);auto i = 0, pow_2_i = this; // pow_2_i = n^{2^i}rhs %= (p-1);while (rhs > 0) {if (rhs % 2 == 1) {result = result * pow_2_i;}rhs >>= 1;i++;pow_2_i = pow_2_i * pow_2_i;}return result;}else {return this.opBinary!op(FiniteField!p(rhs));}}FiniteField!p opBinary(string op)(FiniteField!p rhs) {auto result = this;static if (op == "+") {result.n = (result.n + rhs.n) % p;}else if (op == "-") {result.n = (result.n - rhs.n + p) % p;}else if (op == "*") {result.n = (result.n * rhs.n) % p;}else if (op == "/") {assert (rhs.n != 0);result.n = (result.n * rhs.inv().n) % p;}else assert(0);return result;}FiniteField!p opOpAssign(string op)(long rhs) {return this = this.opBinary!op(rhs);}FiniteField!p opOpAssign(string op)(FiniteField!p rhs) {return this = this.opBinary!op(rhs);}FiniteField!p inv() {assert (this.n != 0);return this ^^ (p-2);}string toString() {import std.conv: to;return n.to!string;}}