結果
問題 | No.2620 Sieve of Coins |
ユーザー |
|
提出日時 | 2024-01-27 00:17:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 43 ms / 2,000 ms |
コード長 | 1,839 bytes |
コンパイル時間 | 799 ms |
コンパイル使用メモリ | 85,112 KB |
最終ジャッジ日時 | 2025-02-18 23:53:36 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 53 |
ソースコード
#include <array>#include <cmath>template <int M>struct Mobius {std::array<int, M + 1> mobius;Mobius() : mobius{} {mobius[1] = 1;std::array<int, M + 1> not_prime{};for (int p = 2; p <= M; ++p) {if (not_prime[p]) continue;for (int v = M / p, vp = v * p; v; --v, vp -= p) {mobius[vp] = -mobius[v];not_prime[vp] = true;}}}int operator[](int i) const { return mobius[i]; }};constexpr int M = 1000000;Mobius<M> mobius;long long count_square_2_3_free(long long n) {const int q = std::sqrt(n);long long res = 0;for (int i = 1; i <= q; ++i) if (mobius[i] and i % 2 and i % 3) {long long v1 = double(n + 1LL * i * i) / (6LL * i * i);long long v5 = double(n + 5LL * i * i) / (6LL * i * i);res += mobius[i] * (v1 + v5);}return res;}#include <iostream>#include <vector>int main() {long long l;int n;std::cin >> l >> n;std::array<std::array<bool, 30>, 40> cnt{};for (int i = 0; i < n; ++i) {long long e;std::cin >> e;int p2 = 0, p3 = 0;while (e % 2 == 0) e /= 2, ++p2;while (e % 3 == 0) e /= 3, ++p3;cnt[p2][p3] = true;}long long ans = 0;for (int p2 = 0; p2 < 40; ++p2) for (int p3 = 0; p3 < 30; ++p3) {int x00 = cnt[p2][p3];int x10 = p2 and cnt[p2 - 1][p3];int x01 = p3 and cnt[p2][p3 - 1];int x11 = p2 and p3 and cnt[p2 - 1][p3 - 1];if (x00 xor x01 xor x10 xor x11) {long long v = 1;for (int e = p2; e--;) v *= 2;for (int e = p3; e--;) v *= 3;long long lim = l / v;ans += count_square_2_3_free(lim);}}std::cout << ans << '\n';}