結果

問題 No.125 悪の花弁
ユーザー te-shte-sh
提出日時 2017-08-08 11:10:43
言語 D
(dmd 2.106.1)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,661 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 142,024 KB
最終ジャッジ日時 2024-11-14 20:11:20
合計ジャッジ時間 1,011 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/internal/write.d(143): Error: cannot implicitly convert expression `obj` of type `const(FactorRing!(1000000007, false))` to `int`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/write.d(1239): Error: template instance `std.format.internal.write.formatValueImpl!(LockingTextWriter, FactorRing!(1000000007, false), char)` error instantiating
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/format/write.d(632):        instantiated from here: `formatValue!(LockingTextWriter, FactorRing!(1000000007, false), char)`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/stdio.d(1759):        instantiated from here: `formattedWrite!(LockingTextWriter, char, FactorRing!(1000000007, false))`
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/stdio.d(4277):        instantiated from here: `write!(FactorRing!(1000000007, false), char)`
Main.d(34):        instantiated from here: `writeln!(FactorRing!(1000000007, false))`

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.numeric;   // gcd, fft

const mod = 10 ^^ 9 + 7;
alias FactorRing!mod mint;

void main()
{
  auto k = readln.chomp.to!int;
  auto c = readln.split.to!(int[]), mc = c.maxElement, sc = c.sum;

  auto fact = new mint[](sc+1), invFact = new mint[](sc+1);
  fact[0] = mint(1);
  foreach (i; 1..sc+1) fact[i] = fact[i-1] * i;
  invFact[sc] = fact[sc].inv;
  foreach_reverse (i; 1..sc) invFact[i] = invFact[i+1] * (i+1);

  auto g = c.fold!((a, b) => gcd(a, b));
  auto d = g.divisors;

  mint[int] r;
  foreach_reverse (di; d) {
    r[sc/di] = fact[sc/di];
    foreach (ci; c) r[sc/di] *= invFact[ci/di];
    foreach_reverse (ei; d) {
      if (ei <= di) break;
      if (ei % di == 0) r[sc/di] -= r[sc/ei];
    }
  }

  auto s = mint(0);
  foreach (gi; r.byKey) s += r[gi] * mint(gi).inv;

  writeln(s);
}

auto divisors(int n)
{
  int[] r;
  foreach (i; 1..n.nsqrt+1) {
    if (n%i == 0) {
      r ~= i;
      if (n/i != i) r ~= n/i;
    }
  }
  r.sort();
  return r;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr / 2 + 1);
  return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}

struct FactorRing(int m, bool pos = false)
{
  long v;

  @property int toInt() { return v.to!int; }
  alias toInt this;

  this(T)(T _v) { v = mod(_v); }

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

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

  pure auto opBinary(string op: "+")(long rhs) const { return FactorRing!(m, pos)(v + rhs); }
  pure auto opBinary(string op: "-")(long rhs) const { return FactorRing!(m, pos)(v - rhs); }
  pure auto opBinary(string op: "*")(long rhs) const { return FactorRing!(m, pos)(v * rhs); }

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

  auto opOpAssign(string op: "+")(long rhs) { v = mod(v + rhs); }
  auto opOpAssign(string op: "-")(long rhs) { v = mod(v - rhs); }
  auto opOpAssign(string op: "*")(long rhs) { v = mod(v * rhs); }

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

  pure auto inv() const
  {
    int x = v.to!int, a, b;
    exEuclid(x, m, a, b);
    return FactorRing!(m, pos)(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