結果

問題 No.1322 Totient Bound
ユーザー 👑 hitonanodehitonanode
提出日時 2020-12-19 02:07:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 8,037 bytes
コンパイル時間 2,686 ms
コンパイル使用メモリ 230,260 KB
実行使用メモリ 199,748 KB
最終ジャッジ日時 2023-10-21 08:48:24
合計ジャッジ時間 50,033 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,636 ms
198,596 KB
testcase_01 AC 1,629 ms
198,596 KB
testcase_02 AC 1,647 ms
198,596 KB
testcase_03 AC 1,635 ms
198,596 KB
testcase_04 AC 1,642 ms
198,596 KB
testcase_05 AC 1,653 ms
198,596 KB
testcase_06 AC 1,665 ms
198,596 KB
testcase_07 AC 1,656 ms
198,596 KB
testcase_08 AC 1,686 ms
198,596 KB
testcase_09 AC 1,655 ms
198,596 KB
testcase_10 AC 1,631 ms
198,596 KB
testcase_11 AC 1,666 ms
198,596 KB
testcase_12 AC 1,619 ms
198,596 KB
testcase_13 AC 1,652 ms
198,596 KB
testcase_14 AC 1,668 ms
198,596 KB
testcase_15 AC 1,838 ms
198,508 KB
testcase_16 AC 1,657 ms
198,596 KB
testcase_17 AC 1,642 ms
198,596 KB
testcase_18 AC 3,237 ms
199,548 KB
testcase_19 AC 4,594 ms
199,748 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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; }
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

// 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 BS = 50000000;
SieveOfEratosthenes sieve(BS);

// 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()
{
    lint N;
    cin >> N;

    lint B = 1;
    while ((B + 1) * (B + 1) <= N) B++;
    vector<lint> cnt(B + 2);
    // B = sqrt(N)
    lint C = N / B;
    // if (B * C == N) C--;
    vector<lint> cnt2(C + 1);
    dbg(B);
    dbg(C);

    cnt[1] = 1;
    for (lint t = 1; t <= N; t *= 2) {
        if (t < lint(cnt.size())) cnt[t]++;
        else cnt2[N / t]++;
    }

    FOR(p, 3, cnt.size()) if (sieve[p] == p) {
        FOR(i, 1, cnt2.size()) {
            for (int now = i / (p - 1); now; now /= p) cnt2[now] += cnt2[i];
        }
        IFOR(i, 1, cnt.size()) {
            for (lint nx = i * lint(p - 1); nx <= N; nx *= p) {
                if (nx < cnt.size()) cnt[nx] += cnt[i];
                else cnt2[N / nx] += cnt[i];
            }
        }
    }

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

    lint ret = cnt2.front();

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

    lint l = BS;
    while (l - 1 <= N) {
        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