結果

問題 No.1261 数字集め
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-10-16 22:45:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,017 ms / 8,000 ms
コード長 5,156 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 154,880 KB
実行使用メモリ 56,464 KB
最終ジャッジ日時 2024-06-22 09:42:57
合計ジャッジ時間 122,446 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,725 ms
56,464 KB
testcase_01 AC 534 ms
27,156 KB
testcase_02 AC 1,191 ms
49,980 KB
testcase_03 AC 1,185 ms
49,520 KB
testcase_04 AC 738 ms
31,900 KB
testcase_05 AC 1,651 ms
52,728 KB
testcase_06 AC 864 ms
28,460 KB
testcase_07 AC 1,290 ms
49,364 KB
testcase_08 AC 676 ms
29,452 KB
testcase_09 AC 912 ms
29,052 KB
testcase_10 AC 60 ms
15,616 KB
testcase_11 AC 1,735 ms
51,020 KB
testcase_12 AC 1,447 ms
48,700 KB
testcase_13 AC 1,320 ms
49,596 KB
testcase_14 AC 132 ms
17,648 KB
testcase_15 AC 596 ms
27,928 KB
testcase_16 AC 1,328 ms
49,388 KB
testcase_17 AC 993 ms
45,280 KB
testcase_18 AC 618 ms
28,132 KB
testcase_19 AC 1,258 ms
48,736 KB
testcase_20 AC 166 ms
18,376 KB
testcase_21 AC 1,871 ms
52,368 KB
testcase_22 AC 1,790 ms
52,372 KB
testcase_23 AC 1,935 ms
52,368 KB
testcase_24 AC 1,782 ms
52,368 KB
testcase_25 AC 1,874 ms
52,364 KB
testcase_26 AC 1,873 ms
52,360 KB
testcase_27 AC 1,885 ms
52,368 KB
testcase_28 AC 1,752 ms
52,236 KB
testcase_29 AC 1,890 ms
52,428 KB
testcase_30 AC 2,017 ms
52,360 KB
testcase_31 AC 1,832 ms
52,364 KB
testcase_32 AC 1,794 ms
52,364 KB
testcase_33 AC 1,906 ms
52,236 KB
testcase_34 AC 1,745 ms
52,244 KB
testcase_35 AC 1,744 ms
52,364 KB
testcase_36 AC 1,798 ms
52,368 KB
testcase_37 AC 1,699 ms
52,360 KB
testcase_38 AC 1,709 ms
52,360 KB
testcase_39 AC 1,724 ms
52,360 KB
testcase_40 AC 1,765 ms
52,364 KB
testcase_41 AC 1,707 ms
52,240 KB
testcase_42 AC 1,708 ms
52,364 KB
testcase_43 AC 1,814 ms
52,360 KB
testcase_44 AC 1,554 ms
50,616 KB
testcase_45 AC 316 ms
26,324 KB
testcase_46 AC 768 ms
31,648 KB
testcase_47 AC 533 ms
27,776 KB
testcase_48 AC 1,230 ms
49,484 KB
testcase_49 AC 1,489 ms
49,748 KB
testcase_50 AC 1,436 ms
51,572 KB
testcase_51 AC 349 ms
25,700 KB
testcase_52 AC 53 ms
16,000 KB
testcase_53 AC 356 ms
26,360 KB
testcase_54 AC 410 ms
28,076 KB
testcase_55 AC 198 ms
21,660 KB
testcase_56 AC 261 ms
23,424 KB
testcase_57 AC 1,144 ms
48,692 KB
testcase_58 AC 320 ms
25,988 KB
testcase_59 AC 1,488 ms
51,536 KB
testcase_60 AC 399 ms
27,112 KB
testcase_61 AC 547 ms
27,796 KB
testcase_62 AC 1,438 ms
52,592 KB
testcase_63 AC 228 ms
21,600 KB
testcase_64 AC 1,801 ms
53,148 KB
testcase_65 AC 1,061 ms
46,620 KB
testcase_66 AC 965 ms
28,752 KB
testcase_67 AC 1,461 ms
51,136 KB
testcase_68 AC 867 ms
28,196 KB
testcase_69 AC 796 ms
32,296 KB
testcase_70 AC 638 ms
30,048 KB
testcase_71 AC 1,316 ms
51,064 KB
testcase_72 AC 463 ms
27,856 KB
testcase_73 AC 517 ms
27,184 KB
testcase_74 AC 247 ms
22,432 KB
testcase_75 AC 752 ms
32,476 KB
testcase_76 AC 1,252 ms
48,544 KB
testcase_77 AC 1,416 ms
51,852 KB
testcase_78 AC 40 ms
16,256 KB
testcase_79 AC 1,484 ms
49,260 KB
testcase_80 AC 55 ms
17,152 KB
testcase_81 AC 142 ms
18,744 KB
testcase_82 AC 1,111 ms
47,908 KB
testcase_83 AC 476 ms
28,904 KB
testcase_84 AC 1,885 ms
54,408 KB
testcase_85 AC 1,762 ms
54,412 KB
testcase_86 AC 1,920 ms
54,416 KB
testcase_87 AC 1,865 ms
54,412 KB
testcase_88 AC 1,801 ms
54,412 KB
testcase_89 AC 1,988 ms
54,412 KB
testcase_90 AC 1,874 ms
54,412 KB
testcase_91 AC 1,812 ms
54,416 KB
testcase_92 AC 1,921 ms
54,408 KB
testcase_93 AC 1,972 ms
54,412 KB
権限があれば一括ダウンロードができます

ソースコード

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; }
real readReal() { return readToken.to!real; }

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)); }


struct ModInt(int M_) {
  import std.conv : to;
  alias M = M_;
  int x;
  this(ModInt a) { x = a.x; }
  this(long a) { x = cast(int)(a % M); if (x < 0) x += M; }
  ref ModInt opAssign(long a) { return (this = ModInt(a)); }
  ref ModInt opOpAssign(string op)(ModInt a) {
    static if (op == "+") { x += a.x; if (x >= M) x -= M; }
    else static if (op == "-") { x -= a.x; if (x < 0) x += M; }
    else static if (op == "*") { x = cast(int)((cast(long)(x) * a.x) % M); }
    else static if (op == "/") { this *= a.inv(); }
    else static assert(false);
    return this;
  }
  ref ModInt opOpAssign(string op)(long a) {
    static if (op == "^^") {
      if (a < 0) return (this = inv()^^(-a));
      ModInt t2 = this, te = ModInt(1);
      for (long e = a; e > 0; e >>= 1) {
        if (e & 1) te *= t2;
        t2 *= t2;
      }
      x = cast(int)(te.x);
      return this;
    } else return mixin("this " ~ op ~ "= ModInt(a)");
  }
  ModInt inv() const {
    int a = x, b = M, y = 1, z = 0, t;
    for (; ; ) {
      t = a / b; a -= t * b;
      if (a == 0) {
        assert(b == 1 || b == -1);
        return ModInt(b * z);
      }
      y -= t * z;
      t = b / a; b -= t * a;
      if (b == 0) {
        assert(a == 1 || a == -1);
        return ModInt(a * y);
      }
      z -= t * y;
    }
  }
  ModInt opUnary(string op: "-")() const { return ModInt(-x); }
  ModInt opBinary(string op, T)(T a) const {
    return mixin("ModInt(this) " ~ op ~ "= a");
  }
  ModInt opBinaryRight(string op)(long a) const {
    return mixin("ModInt(a) " ~ op ~ "= this");
  }
  bool opCast(T: bool)() const { return (x != 0); }
  string toString() const { return x.to!string; }
}

enum MO = 1000000007;
alias Mint = ModInt!MO;

enum LIM = 10^^6 + 10;
Mint[] inv, fac, invFac;
void prepare() {
  inv = new Mint[LIM];
  fac = new Mint[LIM];
  invFac = new Mint[LIM];
  inv[1] = 1;
  foreach (i; 2 .. LIM) {
    inv[i] = -(Mint.M / i) * inv[cast(size_t)(Mint.M % i)];
  }
  fac[0] = invFac[0] = 1;
  foreach (i; 1 .. LIM) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(long n, long k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[cast(size_t)(n)] * invFac[cast(size_t)(k)] * invFac[cast(size_t)(n - k)];
  } else {
    return Mint(0);
  }
}


void main() {
  prepare;
  
  try {
    for (; ; ) {
      const N = readInt();
      const M = readLong();
      auto A = new int[N + 1];
      foreach (u; 2 .. N + 1) {
// if(N==10^^6){A[u]=u;continue;}
        A[u] = readInt() - 1;
      }
      const Q = readInt();
      auto X = new int[Q];
      auto Y = new int[Q];
      foreach (q; 0 .. Q) {
        X[q] = readInt();
        Y[q] = readInt();
      }
      
      auto AA = new Mint[N + 1];
      foreach (u; 2 .. N + 1) {
        AA[u] = Mint(A[u])^^((M - 3) + 2);
      }
      
      Mint getInv(int d) {
        return (d < 0) ? -inv[-d] : inv[d];
      }
      Mint calc(int u, int v) {
        const a = A[u], aa = AA[u];
        const b = A[v], bb = AA[v];
        const c = A[N], cc = AA[N];
        Mint ret = -(aa * (b - c) + bb * (c - a) + cc * (a - b));
        // ret /= (a - b) * (b - c) * (c - a);
        ret *= getInv(a - b);
        ret *= getInv(b - c);
        ret *= getInv(c - a);
        debug {
          writeln(u, " ", v, ": ", ret);
        }
        return ret;
      }
      
      Mint ans;
      auto goal = new bool[N + 1];
      auto sums = new Mint[N + 1];
      
      void add(int u, int v) {
        if (v == N) {
          ans += sums[u];
          goal[u] = true;
        } else {
          const here = calc(u, v);
          if (goal[v]) {
            ans += here;
          }
          sums[v] += here;
        }
      }
      
      foreach (u; 2 .. N + 1) {
        for (int v = 2 * u; v <= N; v += u) {
          add(u, v);
        }
      }
      writeln(ans);
      foreach (q; 0 .. Q) {
        add(X[q], Y[q]);
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0