結果
問題 | No.1322 Totient Bound |
ユーザー |
👑 |
提出日時 | 2020-12-19 13:03:29 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,364 ms / 5,000 ms |
コード長 | 4,285 bytes |
コンパイル時間 | 1,028 ms |
コンパイル使用メモリ | 102,644 KB |
実行使用メモリ | 7,864 KB |
最終ジャッジ日時 | 2024-09-21 10:12:57 |
合計ジャッジ時間 | 22,723 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#include <cassert>#include <cmath>#include <cstdint>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <bitset>#include <complex>#include <deque>#include <functional>#include <iostream>#include <map>#include <numeric>#include <queue>#include <set>#include <sstream>#include <string>#include <unordered_map>#include <unordered_set>#include <utility>#include <vector>using namespace std;using Int = long long;template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }// floor(sqrt(a))long long floorSqrt(long long a) {long long b = a, x = 0, y = 0;for (int e = (63 - __builtin_clzll(a)) & ~1; e >= 0; e -= 2) {x <<= 1;y <<= 1;if (b >= (y | 1) << e) {b -= (y | 1) << e;x |= 1;y += 2;}}return x;}constexpr int LIM = 100'010;Int N;Int sqrtN;bool isPrime[LIM];Int primesLen;Int primes[LIM];Int small[LIM], large[LIM], large1[LIM];Int SMALL[LIM], LARGE[LIM];Int ans;Int get(Int n) {if (n <= sqrtN + 1) return small[n];if (n <= N) {const Int l = N / n;if (n == N / l) return large[l];}return large1[N / (n - 1)];}int main() {for (; ~scanf("%lld", &N); ) {sqrtN = floorSqrt(N + 1);fill(isPrime + 2, isPrime + sqrtN + 1 + 1, true);fill(small, small + sqrtN + 1 + 1, 0);fill(large, large + sqrtN + 1, 0);fill(large1, large1 + sqrtN + 1, 0);fill(SMALL, SMALL + sqrtN + 1, 0);fill(LARGE, LARGE + sqrtN + 1, 0);primesLen = 0;for (Int n = 1; n <= sqrtN + 1; ++n) small[n] = n;for (Int l = 1; l <= sqrtN; ++l) large[l] = N / l;for (Int l = 1; l <= sqrtN; ++l) large1[l] = N / l + 1;for (Int p = 2; p <= sqrtN + 1; ++p) {if (isPrime[p]) {primes[primesLen++] = p;const Int p2 = p * p;for (Int n = p2; n <= sqrtN + 1; n += p) isPrime[n] = false;const Int g1 = small[p - 1];for (Int l = 1; l <= sqrtN; ++l) {Int n = N / l + 1;if (n < p2) break;large1[l] -= (get(n / p) - g1);--n;if (n < p2) break;large[l] -= (get(n / p) - g1);}for (Int n = sqrtN + 1; n >= 1; --n) {if (n < p2) break;small[n] -= (small[n / p] - g1);}}}for (Int n = 1; n <= sqrtN + 1; ++n) small[n] -= 1;for (Int l = 1; l <= sqrtN; ++l) large[l] -= 1;for (Int l = 1; l <= sqrtN; ++l) large1[l] -= 1;// cerr<<"sqrtN = "<<sqrtN<<endl;// cerr<<"primes = ";pv(primes,primes+primesLen);// cerr<<"small = ";pv(small,small+sqrtN+1+1);// cerr<<"large = ";pv(large,large+sqrtN+1);// cerr<<"large1 = ";pv(large1,large1+sqrtN+1);// cerr<<"SMALL = ";pv(SMALL,SMALL+sqrtN+1);// cerr<<"LARGE = ";pv(LARGE,LARGE+sqrtN+1);Int limBef = N + 1;for (Int i = primesLen; i--; ) {const Int p = primes[i];auto GET = [&](Int n) {if (n <= sqrtN) {return (n >= limBef) ? SMALL[n] : (max(small[n + 1] - (i + 1), 0LL) + 1);} else {const int l = N / n;return (n >= limBef) ? LARGE[l] : (max(large1[l] - (i + 1), 0LL) + 1);}};const Int lim = p * (p - 1);for (Int l = 1; l <= sqrtN; ++l) {const Int n = N / l;if (n < lim) break;if (n < limBef) LARGE[l] = GET(n);for (Int m = n / (p - 1); m > 0; m /= p) {LARGE[l] += GET(m);}}for (Int n = sqrtN; n >= 1; --n) {if (n < lim) break;if (n < limBef) SMALL[n] = GET(n);for (Int m = n / (p - 1); m > 0; m /= p) {SMALL[n] += GET(m);}}limBef = lim;// cerr<<"p = "<<p<<endl;// cerr<<"SMALL = ";pv(SMALL,SMALL+sqrtN+1);// cerr<<"LARGE = ";pv(LARGE,LARGE+sqrtN+1);}const Int ans = (N >= limBef) ? LARGE[1] : (large1[1] + 1);printf("%lld\n", ans);}return 0;}