結果

問題 No.1322 Totient Bound
ユーザー hitonanodehitonanode
提出日時 2020-12-19 02:18:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 8,255 bytes
コンパイル時間 3,132 ms
コンパイル使用メモリ 229,632 KB
実行使用メモリ 398,824 KB
最終ジャッジ日時 2024-09-21 09:53:49
合計ジャッジ時間 55,472 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,128 ms
398,824 KB
testcase_01 AC 2,124 ms
198,552 KB
testcase_02 AC 1,966 ms
198,572 KB
testcase_03 AC 1,968 ms
198,512 KB
testcase_04 AC 1,953 ms
198,512 KB
testcase_05 AC 2,042 ms
198,624 KB
testcase_06 AC 1,933 ms
198,536 KB
testcase_07 AC 1,937 ms
198,624 KB
testcase_08 AC 1,958 ms
198,576 KB
testcase_09 AC 1,954 ms
198,360 KB
testcase_10 AC 1,926 ms
198,516 KB
testcase_11 AC 1,932 ms
198,576 KB
testcase_12 AC 1,968 ms
198,600 KB
testcase_13 AC 1,962 ms
198,636 KB
testcase_14 AC 1,947 ms
198,536 KB
testcase_15 AC 1,966 ms
198,556 KB
testcase_16 AC 1,970 ms
198,508 KB
testcase_17 AC 1,969 ms
198,520 KB
testcase_18 AC 3,444 ms
199,288 KB
testcase_19 AC 4,704 ms
199,636 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);

    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) {
        vector<int> div(B + 2);
        for (int i = p; i < div.size(); i += p) div[i] = 1;
        FOR(i, 1, div.size()) div[i] += div[i - 1];

        FOR(i, 1, cnt2.size()) {
            for (int now = i / (p - 1); now; now = div[now]) cnt2[now] += cnt2[i];
        }
        IFOR(i, 1, cnt.size()) {
            lint nx;
            for (nx = i * lint(p - 1); nx < lint(cnt.size()); nx *= p) {
                cnt[nx] += cnt[i];
            }
            for (int now = N / nx; now; now = div[now]) cnt2[now] += cnt[i];
        }
    }
    cerr << cnt2.back() << '\n';
    // return 0;

    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