結果

問題 No.546 オンリー・ワン
コンテスト
ユーザー te-sh
提出日時 2017-07-15 00:08:07
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,921 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,762 ms
コンパイル使用メモリ 145,216 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2026-03-05 16:30:24
合計ジャッジ時間 2,457 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap
import std.typecons;  // Tuple, Nullable, BigFlags
import std.math;      // math functions
import std.numeric;   // gcd, fft
import std.bigint;    // BigInt
import std.random;    // random
import std.bitmanip;  // BitArray
import core.bitop;    // bit operation
import std.regex;     // RegEx
import std.uni;       // unicode

void main()
{
  auto rd = readln.split.to!(BigInt[]), n = rd[0].to!int, l = rd[1], h = rd[2];
  auto c = readln.split.to!(BigInt[]);
  writeln(f(n, c, h) - f(n, c, l-1));
}

auto f(int n, BigInt[] c, BigInt m)
{
  auto r = BigInt(0);
  foreach (i; 1..1 << n) {
    auto d = c.indexed(i.bitsSet).array;
    auto t = (-1) ^^ (i.popcnt - 1) * i.popcnt * calc(m, d);
    r += t;
  }
  return r;
}

auto calc(BigInt n, BigInt[] c)
{
  auto l = c.fold!((a, b) => lcd(a, b));
  auto r = n / l;
  return r;
}

BigInt lcd(BigInt a, BigInt b)
{
  return a / gcd(a, b) * b;
}

BigInt gcd(BigInt a, BigInt b)
{
  if (a < b) return gcd(b, a);
  if (a % b == 0) return b;
  return gcd(b, a % b);
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  pure T bitSet(T)(T n, size_t s, size_t e) { return n | ((T(1) << e) - 1) & ~((T(1) << s) - 1); }
  pure T bitReset(T)(T n, size_t s, size_t e) { return n & (~((T(1) << e) - 1) | ((T(1) << s) - 1)); }
  pure T bitComp(T)(T n, size_t s, size_t e) { return n ^ ((T(1) << e) - 1) & ~((T(1) << s) - 1); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
0