結果

問題 No.435 占い(Extra)
ユーザー te-shte-sh
提出日時 2017-12-19 17:21:42
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,268 ms / 2,000 ms
コード長 3,870 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 88,380 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 17:44:25
合計ジャッジ時間 21,071 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 13 ms
4,376 KB
testcase_12 AC 119 ms
4,380 KB
testcase_13 AC 120 ms
4,380 KB
testcase_14 AC 119 ms
4,376 KB
testcase_15 AC 122 ms
4,376 KB
testcase_16 AC 128 ms
4,380 KB
testcase_17 AC 198 ms
4,376 KB
testcase_18 AC 122 ms
4,380 KB
testcase_19 AC 118 ms
4,376 KB
testcase_20 AC 1,209 ms
4,380 KB
testcase_21 AC 1,188 ms
4,380 KB
testcase_22 AC 1,207 ms
4,376 KB
testcase_23 AC 1,206 ms
4,380 KB
testcase_24 AC 1,223 ms
4,380 KB
testcase_25 AC 1,268 ms
4,380 KB
testcase_26 AC 1,164 ms
4,376 KB
testcase_27 AC 1,202 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1,206 ms
4,376 KB
testcase_30 AC 1,205 ms
4,380 KB
testcase_31 AC 973 ms
4,376 KB
testcase_32 AC 1,038 ms
4,380 KB
testcase_33 AC 1,158 ms
4,380 KB
testcase_34 AC 1,222 ms
4,380 KB
testcase_35 AC 1,183 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.typecons;  // Tuple, Nullable, BigFlags

alias mint = FactorRing!9;

void main()
{
  auto t = readln.chomp.to!int;

  auto f3 = new int[](16);
  f3[0] = 1;
  foreach (i; 1..16) f3[i] = f3[i-1]*3;

  auto f3m = new mint[](16);
  foreach (i; 0..16) f3m[i] = mint(f3[i], true);

  auto split3(int i)
  {
    auto j = 0;
    while (i > 0 && i%3 == 0) {
      ++j;
      i /= 3;
    }
    return tuple(j, mint(i, true));
  }

  foreach (_; 0..t) {
    auto rd = readln.splitter;
    auto n = rd.front.to!int; rd.popFront();
    auto x = rd.front.to!int; rd.popFront();
    auto a = rd.front.to!int; rd.popFront();
    auto b = rd.front.to!int; rd.popFront();
    auto m = rd.front.to!int;

    auto ans = mint(0), s = 0;
    auto r = x;
    auto c3 = 0, co = mint(1);

    foreach (i; 0..n) {
      ans += mint(r%10, true) * f3m[c3] * co;
      s += r%10;

      auto r1 = split3(n-i-1), r2 = split3(i+1);
      c3 += r1[0] - r2[0];
      co *= r1[1] / r2[1];

      r = ((r ^ a) + b) % m;
    }

    if (s == 0)
      writeln(0);
    else
      writeln(ans == 0 ? 9 : ans);
  }
}

struct FactorRing(int m, bool pos = false)
{
  version(BigEndian) {
    union { long vl; struct { int vi2; int vi; } }
  } else {
    union { long vl; int vi; }
  }

  static init() { return FactorRing!(m, pos)(0); }

  @property int toInt() { return vi; }
  alias toInt this;

  this(int v) { vi = v; }
  this(int v, bool runMod) { vi = runMod ? mod(v) : v; }
  this(long v) { vi = mod(v); }

  ref FactorRing!(m, pos) opAssign(int v) { vi = v; return this; }

  pure auto mod(int v) const
  {
    static if (pos) return v % m;
    else return (v % m + m) % m;
  }

  pure auto mod(long v) const
  {
    static if (pos) return cast(int)(v % m);
    else return cast(int)((v % m + m) % m);
  }

  static if (!pos) {
    pure auto opUnary(string op: "-")() const { return FactorRing!(m, pos)(mod(-vi)); }
  }

  static if (m < int.max / 2) {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vi + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vi - rhs)); }
  } else {
    pure auto opBinary(string op: "+")(int rhs) const { return FactorRing!(m, pos)(mod(vl + rhs)); }
    pure auto opBinary(string op: "-")(int rhs) const { return FactorRing!(m, pos)(mod(vl - rhs)); }
  }
  pure auto opBinary(string op: "*")(int rhs) const { return FactorRing!(m, pos)(mod(vl * rhs)); }

  pure auto opBinary(string op)(FactorRing!(m, pos) rhs) const
    if (op == "+" || op == "-" || op == "*") { return opBinary!op(rhs.vi); }

  static if (m < int.max / 2) {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vi + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vi - rhs); }
  } else {
    auto opOpAssign(string op: "+")(int rhs) { vi = mod(vl + rhs); }
    auto opOpAssign(string op: "-")(int rhs) { vi = mod(vl - rhs); }
  }
  auto opOpAssign(string op: "*")(int rhs) { vi = mod(vl * rhs); }

  auto opOpAssign(string op)(FactorRing!(m, pos) rhs)
    if (op == "+" || op == "-" || op == "*") { return opOpAssign!op(rhs.vi); }

  pure auto opBinary(string op: "/")(FactorRing!(m, pos) rhs) { return FactorRing!(m, pos)(mod(vl * rhs.inv.vi)); }
  pure auto opBinary(string op: "/")(int rhs) { return opBinary!op(FactorRing!(m, pos)(rhs)); }
  auto opOpAssign(string op: "/")(FactorRing!(m, pos) rhs) { vi = mod(vl * rhs.inv.vi); }
  auto opOpAssign(string op: "/")(int rhs) { return opOpAssign!op(FactorRing!(m, pos)(rhs)); }

  pure auto inv() const
  {
    int x = vi, a, b;
    exEuclid(x, m, a, b);
    return FactorRing!(m, pos)(mod(a));
  }
}

pure T exEuclid(T)(T a, T b, ref T x, ref T y)
{
  auto g = a;
  x = 1;
  y = 0;
  if (b != 0) {
    g = exEuclid(b, a % b, y, x);
    y -= a / b * x;
  }
  return g;
}
0