結果

問題 No.1627 三角形の成立
ユーザー noshi91noshi91
提出日時 2021-07-23 22:45:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 1,000 ms
コード長 3,300 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 85,820 KB
実行使用メモリ 5,708 KB
最終ジャッジ日時 2023-09-25 23:15:00
合計ジャッジ時間 2,214 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 14 ms
5,708 KB
testcase_06 AC 9 ms
4,692 KB
testcase_07 AC 9 ms
4,872 KB
testcase_08 AC 12 ms
5,252 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 13 ms
5,624 KB
testcase_11 AC 10 ms
4,784 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 12 ms
5,196 KB
testcase_15 AC 10 ms
4,696 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 12 ms
5,212 KB
testcase_18 AC 10 ms
5,080 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cstddef>
#include <numeric>
#include <utility>
#include <vector>

namespace n91 {

class prime_sieve {
public:
  using size_type = std::size_t;

private:
  std::vector<size_type> prime, factor;

public:
  prime_sieve() {}
  explicit prime_sieve(const size_type size) : prime(), factor(size) {
    if (size == 0) {
      return;
    }
    std::iota(factor.begin(), factor.end(), static_cast<size_type>(0));
    factor[0] = 1;
    if (size == 1) {
      return;
    }
    factor[1] = 0;
    prime.reserve(size);
    for (size_type i{2}; i != size; ++i) {
      const size_type fi{factor[i]};
      if (fi == i) {
        prime.push_back(i);
      }
      for (const size_type p : prime) {
        if (p > fi) {
          break;
        }
        const size_type ip{i * p};
        if (ip >= size) {
          break;
        }
        factor[ip] = p;
      }
    }
    prime.shrink_to_fit();
  }
  size_type len() const noexcept { return factor.size(); }
  bool is_prime(const size_type i) const noexcept {
    assert(i < len());
    return factor[i] == i;
  }
  std::vector<size_type> factorize(size_type i) const noexcept {
    assert(i != 0);
    std::vector<size_type> ret;
    while (i != 1) {
      ret.push_back(factor[i]);
      i /= factor[i];
    }
    return std::move(ret);
  }
  template <class C> void divisor_zeta(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{1}; j * prime[i] < n; ++j) {
        c[j * prime[i]] += c[j];
      }
    }
  }
  template <class C> void divisor_mobius(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{(n - 1) / prime[i]}; j != 0; --j) {
        c[j * prime[i]] -= c[j];
      }
    }
  }
  template <class C> void multiple_zeta(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{(n - 1) / prime[i]}; j != 0; --j) {
        c[j] += c[j * prime[i]];
      }
    }
  }
  template <class C> void multiple_mobius(C &c) const noexcept {
    const size_type n{c.size()};
    assert(n <= len());
    for (size_type i{0}; i != prime.size() && prime[i] < n; ++i) {
      for (size_type j{1}; j * prime[i] < n; ++j) {
        c[j] -= c[j * prime[i]];
      }
    }
  }
};

} // namespace n91

#include <algorithm>
#include <iostream>
#include <vector>

#include <atcoder/modint>

int main() {
  using mint = atcoder::modint1000000007;

  int n, m;
  std::cin >> n >> m;

  int A = std::max(n, m);
  std::vector<mint> a(A, 0);
  const mint inv2 = mint(1) / 2;
  for (int d = 1; d != A; d += 1) {
    mint h = 0, v = 0;
    {
      mint x = (n - 1) / d;
      h = x * n - d * x * (x + 1) * inv2;
    }
    {
      mint x = (m - 1) / d;
      v = x * m - d * x * (x + 1) * inv2;
    }
    a[d] = 2 * h * v + n * v + m * h;
  }
  n91::prime_sieve(A).multiple_mobius(a);

  mint ans = [&]() {
    mint x = mint(n) * m;
    return x * (x - 1) * (x - 2) / 6;
  }();

  for (int d = 1; d != A; d += 1) {
    ans -= a[d] * (d - 1);
  }

  std::cout << ans.val() << "\n";

  return 0;
}
0