結果

問題 No.1322 Totient Bound
ユーザー 👑 hitonanodehitonanode
提出日時 2020-12-19 01:28:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,587 bytes
コンパイル時間 3,032 ms
コンパイル使用メモリ 215,288 KB
実行使用メモリ 200,064 KB
最終ジャッジ日時 2023-10-21 08:41:04
合計ジャッジ時間 106,905 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,428 ms
199,180 KB
testcase_01 AC 2,451 ms
199,188 KB
testcase_02 AC 2,396 ms
199,188 KB
testcase_03 AC 2,416 ms
199,292 KB
testcase_04 AC 2,401 ms
199,120 KB
testcase_05 AC 2,386 ms
199,112 KB
testcase_06 AC 2,411 ms
199,112 KB
testcase_07 AC 2,421 ms
199,112 KB
testcase_08 AC 2,404 ms
199,112 KB
testcase_09 AC 2,415 ms
199,112 KB
testcase_10 AC 2,418 ms
199,112 KB
testcase_11 AC 2,400 ms
199,112 KB
testcase_12 AC 2,435 ms
199,268 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2,422 ms
199,192 KB
testcase_31 AC 2,410 ms
199,192 KB
testcase_32 AC 2,390 ms
199,192 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#else
#define dbg(x) {}
#endif

// Sieve of Eratosthenes
// (*this)[i] = (divisor of i, greater than 1)
// Example: [0, 1, 2, 3, 2, 5, 3, 7, 2, 3, 2, 11, ...]
// Complexity: Space O(MAXN), Time (construction) O(MAXNloglogMAXN)
struct SieveOfEratosthenes : std::vector<int> {
    SieveOfEratosthenes(int MAXN) : std::vector<int>(MAXN + 1) {
        std::iota(begin(), end(), 0);
        for (int i = 2; i <= MAXN; i++) {
            if ((*this)[i] == i) {
                for (int j = i; j <= MAXN; j += i) (*this)[j] = i;
            }
        }
    }
};
constexpr int B = 50000000;
SieveOfEratosthenes sieve(B);

// https://old.yosupo.jp/submission/7976
using i64 = long long;
int isqrt(i64 n) { return sqrtl(n); }
__attribute__((target("avx"), optimize("O3", "unroll-loops"))) i64 prime_pi(const i64 N) {
    if (N <= 1) return 0;
    if (N == 2) return 1;
    const int v = isqrt(N);
    int s = (v + 1) / 2;
    vector<int> smalls(s);
    for (int i = 1; i < s; ++i) smalls[i] = i;
    vector<int> roughs(s);
    for (int i = 0; i < s; ++i) roughs[i] = 2 * i + 1;
    vector<i64> larges(s);
    for (int i = 0; i < s; ++i) larges[i] = (N / (2 * i + 1) - 1) / 2;
    vector<bool> skip(v + 1);
    const auto divide = [](i64 n, i64 d) -> int { return double(n) / d; };
    const auto half = [](int n) -> int { return (n - 1) >> 1; };
    int pc = 0;
    for (int p = 3; p <= v; p += 2)
        if (!skip[p]) {
            int q = p * p;
            if (i64(q) * q > N) break;
            skip[p] = true;
            for (int i = q; i <= v; i += 2 * p) skip[i] = true;
            int ns = 0;
            for (int k = 0; k < s; ++k) {
                int i = roughs[k];
                if (skip[i]) continue;
                i64 d = i64(i) * p;
                larges[ns] = larges[k] - (d <= v ? larges[smalls[d >> 1] - pc] : smalls[half(divide(N, d))]) + pc;
                roughs[ns++] = i;
            }
            s = ns;
            for (int i = half(v), j = ((v / p) - 1) | 1; j >= p; j -= 2) {
                int c = smalls[j >> 1] - pc;
                for (int e = (j * p) >> 1; i >= e; --i) smalls[i] -= c;
            }
            ++pc;
        }
    larges[0] += i64(s + 2 * (pc - 1)) * (s - 1) / 2;
    for (int k = 1; k < s; ++k) larges[0] -= larges[k];
    for (int l = 1; l < s; ++l) {
        int q = roughs[l];
        i64 M = N / q;
        int e = smalls[half(M / q)] - pc;
        if (e < l + 1) break;
        i64 t = 0;
        for (int k = l + 1; k <= e; ++k) t += smalls[half(divide(M, roughs[k]))];
        larges[0] += t - i64(e - l) * (pc + l - 1);
    }
    return larges[0] + 1;
}



int main()
{

    array<lint, 101001> cnt;
    cnt.fill(0);
    cnt[1] = 1;
    for (int t = 1; t < cnt.size(); t *= 2) cnt[t]++;
    FOR(p, 3, cnt.size()) if (sieve[p] == p) {
        IFOR(i, 1, cnt.size()) {
            for (lint nx = i * lint(p - 1); nx < lint(cnt.size()); nx *= p) cnt[nx] += cnt[i];
        }
    }

    lint N;
    cin >> N;

    REP(i, cnt.size() - 1) cnt[i + 1] += cnt[i];

    if (N < cnt.size()) {
        cout << cnt[N] << '\n';
        return 0;
    }

    lint ret = cnt.back();

    FOR(p, cnt.size(), B) if (sieve[p] == p) {
        lint y = N / (p - 1);
        ret += cnt[y];
    }

    lint l = B;
    while (l <= N + 1) {
        lint y = N / (l - 1);
        lint r = N / y + 1;
        ret += cnt[y] * (prime_pi(r) - prime_pi(l - 1));
        l = r + 1;
    }
    cout << ret << '\n';
}
0