結果

問題 No.1261 数字集め
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-10-16 22:45:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,895 ms / 8,000 ms
コード長 5,156 bytes
コンパイル時間 3,811 ms
コンパイル使用メモリ 151,748 KB
実行使用メモリ 58,588 KB
最終ジャッジ日時 2023-09-04 10:47:04
合計ジャッジ時間 183,444 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,816 ms
54,744 KB
testcase_01 AC 718 ms
27,112 KB
testcase_02 AC 1,938 ms
48,560 KB
testcase_03 AC 1,762 ms
48,960 KB
testcase_04 AC 1,113 ms
31,420 KB
testcase_05 AC 2,558 ms
51,596 KB
testcase_06 AC 1,189 ms
28,872 KB
testcase_07 AC 1,898 ms
49,772 KB
testcase_08 AC 967 ms
29,816 KB
testcase_09 AC 1,212 ms
29,328 KB
testcase_10 AC 71 ms
16,140 KB
testcase_11 AC 2,815 ms
51,252 KB
testcase_12 AC 2,298 ms
48,916 KB
testcase_13 AC 2,151 ms
49,868 KB
testcase_14 AC 164 ms
18,340 KB
testcase_15 AC 813 ms
28,332 KB
testcase_16 AC 2,093 ms
49,800 KB
testcase_17 AC 1,426 ms
46,568 KB
testcase_18 AC 847 ms
28,368 KB
testcase_19 AC 1,912 ms
48,880 KB
testcase_20 AC 194 ms
19,508 KB
testcase_21 AC 2,772 ms
52,664 KB
testcase_22 AC 2,516 ms
54,916 KB
testcase_23 AC 2,628 ms
52,624 KB
testcase_24 AC 2,722 ms
53,648 KB
testcase_25 AC 2,645 ms
52,576 KB
testcase_26 AC 2,559 ms
55,156 KB
testcase_27 AC 2,895 ms
52,564 KB
testcase_28 AC 2,758 ms
55,452 KB
testcase_29 AC 2,810 ms
52,548 KB
testcase_30 AC 2,693 ms
55,272 KB
testcase_31 AC 2,630 ms
52,572 KB
testcase_32 AC 2,795 ms
56,876 KB
testcase_33 AC 2,759 ms
52,588 KB
testcase_34 AC 2,643 ms
53,880 KB
testcase_35 AC 2,606 ms
52,620 KB
testcase_36 AC 2,701 ms
56,176 KB
testcase_37 AC 2,744 ms
52,620 KB
testcase_38 AC 2,676 ms
55,712 KB
testcase_39 AC 2,779 ms
52,572 KB
testcase_40 AC 2,781 ms
55,740 KB
testcase_41 AC 2,609 ms
52,588 KB
testcase_42 AC 2,697 ms
55,300 KB
testcase_43 AC 2,741 ms
52,616 KB
testcase_44 AC 2,279 ms
54,808 KB
testcase_45 AC 378 ms
26,856 KB
testcase_46 AC 1,182 ms
32,048 KB
testcase_47 AC 699 ms
28,148 KB
testcase_48 AC 1,790 ms
49,888 KB
testcase_49 AC 2,167 ms
51,540 KB
testcase_50 AC 1,962 ms
51,836 KB
testcase_51 AC 452 ms
27,396 KB
testcase_52 AC 64 ms
16,604 KB
testcase_53 AC 442 ms
26,628 KB
testcase_54 AC 508 ms
28,372 KB
testcase_55 AC 242 ms
21,888 KB
testcase_56 AC 314 ms
23,624 KB
testcase_57 AC 1,812 ms
48,864 KB
testcase_58 AC 401 ms
26,688 KB
testcase_59 AC 2,445 ms
51,804 KB
testcase_60 AC 475 ms
27,920 KB
testcase_61 AC 753 ms
28,032 KB
testcase_62 AC 2,135 ms
52,944 KB
testcase_63 AC 268 ms
24,608 KB
testcase_64 AC 2,592 ms
53,324 KB
testcase_65 AC 1,659 ms
49,876 KB
testcase_66 AC 1,184 ms
29,252 KB
testcase_67 AC 2,141 ms
51,388 KB
testcase_68 AC 1,036 ms
29,936 KB
testcase_69 AC 1,159 ms
32,456 KB
testcase_70 AC 875 ms
30,336 KB
testcase_71 AC 1,868 ms
51,264 KB
testcase_72 AC 521 ms
29,272 KB
testcase_73 AC 640 ms
27,480 KB
testcase_74 AC 327 ms
22,816 KB
testcase_75 AC 1,021 ms
32,996 KB
testcase_76 AC 1,885 ms
48,860 KB
testcase_77 AC 2,041 ms
53,524 KB
testcase_78 AC 46 ms
16,396 KB
testcase_79 AC 2,301 ms
49,468 KB
testcase_80 AC 66 ms
19,860 KB
testcase_81 AC 186 ms
19,092 KB
testcase_82 AC 1,668 ms
48,284 KB
testcase_83 AC 671 ms
29,352 KB
testcase_84 AC 2,645 ms
54,664 KB
testcase_85 AC 2,617 ms
57,472 KB
testcase_86 AC 2,792 ms
54,644 KB
testcase_87 AC 2,630 ms
56,816 KB
testcase_88 AC 2,569 ms
55,836 KB
testcase_89 AC 2,755 ms
54,772 KB
testcase_90 AC 2,514 ms
56,504 KB
testcase_91 AC 2,635 ms
54,684 KB
testcase_92 AC 2,640 ms
58,588 KB
testcase_93 AC 2,733 ms
54,756 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