結果

問題 No.1261 数字集め
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-10-16 22:41:29
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 5,249 ms / 8,000 ms
コード長 4,395 bytes
コンパイル時間 2,137 ms
コンパイル使用メモリ 152,560 KB
実行使用メモリ 44,656 KB
最終ジャッジ日時 2023-09-04 10:41:01
合計ジャッジ時間 328,543 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,940 ms
43,764 KB
testcase_01 AC 1,555 ms
12,440 KB
testcase_02 AC 3,655 ms
40,796 KB
testcase_03 AC 3,600 ms
41,104 KB
testcase_04 AC 2,201 ms
21,500 KB
testcase_05 AC 4,986 ms
41,476 KB
testcase_06 AC 2,512 ms
20,560 KB
testcase_07 AC 3,785 ms
42,372 KB
testcase_08 AC 1,929 ms
23,972 KB
testcase_09 AC 2,581 ms
21,688 KB
testcase_10 AC 100 ms
4,384 KB
testcase_11 AC 4,968 ms
42,256 KB
testcase_12 AC 4,345 ms
43,052 KB
testcase_13 AC 3,857 ms
42,524 KB
testcase_14 AC 316 ms
6,216 KB
testcase_15 AC 1,657 ms
12,832 KB
testcase_16 AC 3,775 ms
42,268 KB
testcase_17 AC 2,933 ms
22,728 KB
testcase_18 AC 1,715 ms
13,060 KB
testcase_19 AC 3,537 ms
43,356 KB
testcase_20 AC 409 ms
6,268 KB
testcase_21 AC 5,249 ms
42,704 KB
testcase_22 AC 5,171 ms
44,400 KB
testcase_23 AC 5,209 ms
42,692 KB
testcase_24 AC 5,068 ms
43,420 KB
testcase_25 AC 4,972 ms
42,684 KB
testcase_26 AC 4,966 ms
43,232 KB
testcase_27 AC 4,986 ms
42,700 KB
testcase_28 AC 4,974 ms
44,656 KB
testcase_29 AC 4,954 ms
42,660 KB
testcase_30 AC 5,012 ms
43,736 KB
testcase_31 AC 5,070 ms
42,732 KB
testcase_32 AC 4,948 ms
44,484 KB
testcase_33 AC 5,039 ms
42,652 KB
testcase_34 AC 4,965 ms
43,900 KB
testcase_35 AC 5,017 ms
42,660 KB
testcase_36 AC 4,996 ms
43,996 KB
testcase_37 AC 4,976 ms
42,728 KB
testcase_38 AC 4,959 ms
42,832 KB
testcase_39 AC 4,995 ms
42,768 KB
testcase_40 AC 5,039 ms
43,608 KB
testcase_41 AC 5,035 ms
42,680 KB
testcase_42 AC 4,980 ms
42,688 KB
testcase_43 AC 5,081 ms
42,632 KB
testcase_44 AC 4,343 ms
44,636 KB
testcase_45 AC 779 ms
11,252 KB
testcase_46 AC 2,210 ms
21,932 KB
testcase_47 AC 1,413 ms
12,444 KB
testcase_48 AC 3,570 ms
42,064 KB
testcase_49 AC 4,263 ms
43,772 KB
testcase_50 AC 3,807 ms
37,500 KB
testcase_51 AC 867 ms
12,820 KB
testcase_52 AC 56 ms
4,448 KB
testcase_53 AC 888 ms
12,536 KB
testcase_54 AC 1,023 ms
12,068 KB
testcase_55 AC 452 ms
6,984 KB
testcase_56 AC 599 ms
10,520 KB
testcase_57 AC 3,365 ms
41,228 KB
testcase_58 AC 789 ms
13,256 KB
testcase_59 AC 4,345 ms
40,756 KB
testcase_60 AC 1,000 ms
12,056 KB
testcase_61 AC 1,501 ms
12,524 KB
testcase_62 AC 3,919 ms
38,468 KB
testcase_63 AC 522 ms
6,820 KB
testcase_64 AC 4,810 ms
42,888 KB
testcase_65 AC 3,058 ms
23,124 KB
testcase_66 AC 2,441 ms
20,916 KB
testcase_67 AC 3,985 ms
44,376 KB
testcase_68 AC 2,309 ms
22,384 KB
testcase_69 AC 2,261 ms
22,344 KB
testcase_70 AC 1,729 ms
18,916 KB
testcase_71 AC 3,707 ms
37,124 KB
testcase_72 AC 1,139 ms
14,032 KB
testcase_73 AC 1,410 ms
13,204 KB
testcase_74 AC 600 ms
11,652 KB
testcase_75 AC 2,113 ms
20,400 KB
testcase_76 AC 3,422 ms
40,856 KB
testcase_77 AC 3,971 ms
42,144 KB
testcase_78 AC 17 ms
4,380 KB
testcase_79 AC 4,129 ms
43,008 KB
testcase_80 AC 56 ms
4,388 KB
testcase_81 AC 318 ms
7,168 KB
testcase_82 AC 3,181 ms
40,276 KB
testcase_83 AC 1,219 ms
12,836 KB
testcase_84 AC 5,003 ms
43,712 KB
testcase_85 AC 4,960 ms
43,760 KB
testcase_86 AC 4,940 ms
43,716 KB
testcase_87 AC 4,992 ms
43,740 KB
testcase_88 AC 4,954 ms
44,260 KB
testcase_89 AC 4,948 ms
44,264 KB
testcase_90 AC 4,937 ms
43,692 KB
testcase_91 AC 5,037 ms
43,776 KB
testcase_92 AC 5,050 ms
44,336 KB
testcase_93 AC 4,987 ms
44,208 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;


void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readLong();
      auto A = new Mint[N + 1];
      foreach (u; 2 .. N + 1) {
// if(N==10^^6){A[u]=u;continue;}
        A[u] = readLong() - 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] = A[u]^^((M - 3) + 2);
      }
      
      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);
        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