結果

問題 No.1322 Totient Bound
ユーザー PachicobuePachicobue
提出日時 2020-12-19 03:39:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 9,944 bytes
コンパイル時間 3,028 ms
コンパイル使用メモリ 230,540 KB
実行使用メモリ 120,712 KB
最終ジャッジ日時 2023-10-21 08:56:28
合計ジャッジ時間 10,014 ms
ジャッジサーバーID
(参考情報)
judge14 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 108 ms
9,472 KB
testcase_14 AC 99 ms
9,196 KB
testcase_15 AC 21 ms
5,524 KB
testcase_16 AC 40 ms
6,616 KB
testcase_17 AC 18 ms
5,252 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
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 #

#include <bits/stdc++.h>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> using max_heap = std::priority_queue<T>;
template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template<typename T, int n, int i = 0>
auto nd_array(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));
    }
}
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }
private:
    template<typename T> void dump(const T& v) { m_os << v; }
    template<typename T> void dump(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T> void dump(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }
    std::ostream& m_os;
};
printer out;
class range
{
private:
    struct itr
    {
        itr(const int start = 0, const int step = 1) : m_cnt{start}, m_step{step} {}
        bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; }
        int& operator*() { return m_cnt; }
        itr& operator++() { return m_cnt += m_step, *this; }
        int m_cnt, m_step;
    };
    int m_start, m_end, m_step;
public:
    range(const int start, const int end, const int step = 1) : m_start{start}, m_end{end}, m_step{step}
    {
        assert(m_step != 0);
        if (m_step > 0) { m_end = m_start + std::max(m_step - 1, m_end - m_start + m_step - 1) / m_step * m_step; }
        if (m_step < 0) { m_end = m_start - std::max(-m_step - 1, m_start - m_end - m_step - 1) / (-m_step) * (-m_step); }
    }
    itr begin() const { return itr{m_start, m_step}; }
    itr end() const { return itr{m_end, m_step}; }
};
range rep(const int end, const int step = 1) { return range(0, end, step); }
range per(const int rend, const int step = -1) { return range(rend - 1, -1, step); }
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }
    template<typename T> T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return make_v<T>(n, [this]() { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return make_v<T>(n, [this, offset]() { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }
private:
    template<typename T, typename F>
    std::vector<T> make_v(const int n, F f)
    {
        std::vector<T> ans;
        for (int i = 0; i < n; i++) { ans.push_back(f()); }
        return ans;
    }
    std::istream& m_is;
};
scanner in;
template<typename T, typename ST = T>
class prime_accum
{
public:
    prime_accum(const T N) : N{N}, SQRT{sqrt(N)}, isp(SQRT + 1, true)
    {
        assert(N >= 2);
        init();
        init_smalls();
        init_larges();
        for (int i = 0; i < pn; i++) {
            const int p = ps[i];
            for (int j = nn; (j--) > 0;) {
                const T n = ns[j];
                if (n < (T)p * (T)p) { break; }
                const T pn = n / (T)p;
                const int pj = rev(pn);
                assert(C_smalls[p - 1] == i);
                Cs[j] -= Cs[pj] - C_smalls[p - 1];
                Ss[j] -= (ST)p * (Ss[pj] - S_smalls[p - 1]);
            }
        }
    }
    T count(const T n) const
    {
        const int i = rev(n);
        assert(ns[i] == n);
        return Cs[i];
    }
    ST sum(const T n) const
    {
        const int i = rev(n);
        assert(ns[i] == n);
        return Ss[i];
    }
private:
    static int sqrt(const T n)
    {
        int ans = std::sqrt(n);
        for (; (T)(ans + 1) * (T)(ans + 1) <= n; ans++) {}
        return ans;
    }
    void init()
    {
        isp[0] = false, isp[1] = false;
        for (int p = 2; p <= SQRT; p++) {
            if (not isp[p]) { continue; }
            ps.push_back(p);
            for (int q = p * 2; q <= SQRT; q += p) { isp[q] = false; }
        }
        for (T p = 1; p * p <= N; p++) { ns.push_back(p), ns.push_back(N / p); }
        std::sort(ns.begin(), ns.end());
        ns.erase(std::unique(ns.begin(), ns.end()), ns.end());
        pn = ps.size(), nn = ns.size();
    }
    void init_smalls()
    {
        C_smalls.resize(SQRT + 1, 0), S_smalls.resize(SQRT + 1, 0);
        for (int n = 1; n <= SQRT; n++) {
            if (not isp[n]) { continue; }
            C_smalls[n] = isp[n] ? (T)1 : (T)0;
            S_smalls[n] = isp[n] ? (ST)n : (ST)0;
        }
        for (int n = 1; n <= SQRT; n++) { C_smalls[n] += C_smalls[n - 1], S_smalls[n] += S_smalls[n - 1]; }
    }
    void init_larges()
    {
        Cs.resize(nn, 0), Ss.resize(nn, 0);
        for (int i = 0; i < nn; i++) {
            const T n = ns[i];
            Cs[i] = (T)n - (T)1;
            Ss[i] = (ST)n * (ST)(n + 1) / (ST)2 - (ST)2;
        }
    }
    int rev(const T n) const { return n <= SQRT ? (int)(n - 1) : nn - (int)(N / n); }
    T N = 0;
    const int SQRT;
    int pn, nn;
    std::vector<bool> isp;
    std::vector<int> ps;
    std::vector<T> ns;
    std::vector<T> Cs;
    std::vector<ST> Ss;
    std::vector<T> C_smalls;
    std::vector<ST> S_smalls;
};
template<typename T, typename V>
inline bool miller_rabin(const T& n, const std::vector<T>& as)
{
    auto pow = [&](auto&& self, const V& a, const T k) -> V {
        if (k == 0) { return 1; }
        if (k % 2 == 0) {
            return self(self, (a * a) % V(n), k / 2);
        } else {
            return (self(self, a, k - 1) * a) % V(n);
        }
    };
    T d = n - 1;
    for (; (d & 1) == 0; d >>= 1) {}
    for (const T& a : as) {
        if (n <= a) { break; }
        T s = d;
        V x = pow(pow, a, s);
        while (x != 1 and x != n - 1 and s != n - 1) {
            (x *= x) %= V(n);
            s *= 2;
        }
        if (x != n - 1 and s % 2 == 0) { return false; }
    }
    return true;
}
inline bool is_prime(const ull n)
{
    if (n % 2 == 0) { return n == 2; }
    if (n < (1ULL << 32)) {
        return miller_rabin<uint, ull>((uint)n, std::vector<uint>{2, 7, 61});
    } else {
        return miller_rabin<ull, __uint128_t>(n, std::vector<ull>{2, 325, 9375, 28178, 450775, 9780504});
    }
}
constexpr int B = 100001;
bool isp[B];
std::vector<ll> ps;
int main()
{
    std::fill(isp + 2, isp + B, true);
    for (const int p : range(2, B)) {
        if (not isp[p]) { continue; }
        ps.push_back(p);
        for (const int q : range(p + p, B, p)) { isp[q] = false; }
    }
    const auto N = in.val<ll>();
    auto solve = [&](const ll N) {
        std::map<ll, ll> cands;
        cands[1] = 1;
        std::map<ll, ll> adds;
        int i = 0;
        for (const ll p : ps) {
            adds.clear();
            for (const auto& [prod, num] : cands) {
                if (prod * (p - 1) > N) { break; }
                for (ll pr = prod * (p - 1); pr <= N; pr *= p) { adds[pr] += num; }
            }
            for (const auto& [p, n] : adds) { cands[p] += n; }
            if (adds.empty()) { break; }
        }
        prime_accum<ll, ll> pi(N);
        ll ans = 0;
        for (const auto& [p, n] : cands) {
            ans += n;
            const ll pmax = N / p + 1;
            if (pmax < B) { continue; }
            const ll P = pi.count(pmax - 1) + is_prime(pmax) - (ll)ps.size();
            ans += n * P;
        }
        return ans;
    };
    out.ln(solve(N));
    return 0;
}
0