結果
問題 | No.1322 Totient Bound |
ユーザー |
![]() |
提出日時 | 2020-12-19 02:04:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 8,037 bytes |
コンパイル時間 | 3,364 ms |
コンパイル使用メモリ | 228,764 KB |
最終ジャッジ日時 | 2025-01-17 03:26:43 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 WA * 1 TLE * 21 |
ソースコード
#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 >= 201703Ltemplate <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); returnis; }template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);},tpl); return os; }#endiftemplate <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/7976using 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 + 1);// 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';}