#line 1 "main.cpp" #define PROBLEM "https://yukicoder.me/problems/no/1593" #line 1 "/home/maspy/compro/library/my_template.hpp" #include using namespace std; using ll = long long; using pi = pair; using vi = vector; using u32 = unsigned int; using u64 = unsigned long long; using i128 = __int128; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template using vvvvc = vector>; template using vvvvvc = vector>; template using pq = priority_queue; template using pqg = priority_queue, greater>; #define vec(type, name, ...) vector name(__VA_ARGS__) #define vv(type, name, h, ...) \ vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) \ vector>> name( \ h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name( \ a, vector>>( \ b, vector>(c, vector(__VA_ARGS__)))) #define FOR_(n) for (ll _ = 0; (_) < (ll)(n); ++(_)) #define FOR(i, n) for (ll i = 0; (i) < (ll)(n); ++(i)) #define FOR3(i, m, n) for (ll i = (m); (i) < (ll)(n); ++(i)) #define FOR_R(i, n) for (ll i = (ll)(n)-1; (i) >= 0; --(i)) #define FOR3_R(i, m, n) for (ll i = (ll)(n)-1; (i) >= (ll)(m); --(i)) #define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s)) #define all(x) x.begin(), x.end() #define len(x) ll(x.size()) #define elif else if #define eb emplace_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define stoi stoll template T SUM(vector &A) { T sum = T(0); for (auto &&a: A) sum += a; return sum; } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) int popcnt(int x) { return __builtin_popcount(x); } int popcnt(u32 x) { return __builtin_popcount(x); } int popcnt(ll x) { return __builtin_popcountll(x); } int popcnt(u64 x) { return __builtin_popcountll(x); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2) int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); } int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } // (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2) int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); } int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template T ceil(T x, U y) { return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, U y) { return (x > 0 ? x / y : (x - y + 1) / y); } template pair divmod(T x, U y) { T q = floor(x, y); return {q, x - q * y}; } ll binary_search(function check, ll ok, ll ng) { assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; if (check(x)) ok = x; else ng = x; } return ok; } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } vi s_to_vi(const string& S, char first_char) { vi A(S.size()); FOR(i, S.size()) { A[i] = S[i] - first_char; } return A; } template vector cumsum(vector &A, int off = 1) { int N = A.size(); vector B(N + 1); FOR(i, N) { B[i + 1] = B[i] + A[i]; } if (off == 0) B.erase(B.begin()); return B; } template vc bincount(const vc &A, int size) { vc C(size); for (auto &&x: A) { ++C[x]; } return C; } template vector argsort(const vector &A) { // stable vector ids(A.size()); iota(all(ids), 0); sort(all(ids), [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); }); return ids; } // A[I[0]], A[I[1]], ... template vc rearrange(const vc &A, const vc &I) { int n = len(A); assert(len(I) == n); vc B(n); FOR(i, n) B[i] = A[I[i]]; return B; } #line 1 "/home/maspy/compro/library/other/io.hpp" // based on yosupo's fastio #include namespace detail { template std::true_type check_value(int); template std::false_type check_value(long); } // namespace detail template struct is_modint : decltype(detail::check_value(0)) {}; template using is_modint_t = enable_if_t::value>; template using is_not_modint_t = enable_if_t::value>; struct Scanner { FILE *fp; char line[(1 << 15) + 1]; size_t st = 0, ed = 0; void reread() { memmove(line, line + st, ed - st); ed -= st; st = 0; ed += fread(line + ed, 1, (1 << 15) - ed, fp); line[ed] = '\0'; } bool succ() { while (true) { if (st == ed) { reread(); if (st == ed) return false; } while (st != ed && isspace(line[st])) st++; if (st != ed) break; } if (ed - st <= 50) { bool sep = false; for (size_t i = st; i < ed; i++) { if (isspace(line[i])) { sep = true; break; } } if (!sep) reread(); } return true; } template ::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; while (true) { size_t sz = 0; while (st + sz < ed && !isspace(line[st + sz])) sz++; ref.append(line + st, sz); st += sz; if (!sz || st != ed) break; reread(); } return true; } template ::value, int> = 0> bool read_single(T &ref) { if (!succ()) return false; bool neg = false; if (line[st] == '-') { neg = true; st++; } ref = T(0); while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); } if (neg) ref = -ref; return true; } template * = nullptr> bool read_single(T &ref) { long long val = 0; bool f = read_single(val); ref = T(val); return f; } bool read_single(double &ref) { string s; if (!read_single(s)) return false; ref = std::stod(s); return true; } bool read_single(char &ref) { string s; if (!read_single(s) || s.size() != 1) return false; ref = s[0]; return true; } template bool read_single(vector &ref) { for (auto &d: ref) { if (!read_single(d)) return false; } return true; } template bool read_single(pair &p) { return (read_single(p.first) && read_single(p.second)); } template bool read_single(tuple &p) { return (read_single(get<0>(p)) && read_single(get<1>(p)) && read_single(get<2>(p))); } template bool read_single(tuple &p) { return (read_single(get<0>(p)) && read_single(get<1>(p)) && read_single(get<2>(p)) && read_single(get<3>(p))); } void read() {} template void read(H &h, T &... t) { bool f = read_single(h); assert(f); read(t...); } Scanner(FILE *fp) : fp(fp) {} }; struct Printer { Printer(FILE *_fp) : fp(_fp) {} ~Printer() { flush(); } static constexpr size_t SIZE = 1 << 15; FILE *fp; char line[SIZE], small[50]; size_t pos = 0; void flush() { fwrite(line, 1, pos, fp); pos = 0; } void write(const char &val) { if (pos == SIZE) flush(); line[pos++] = val; } template ::value, int> = 0> void write(T val) { if (pos > (1 << 15) - 50) flush(); if (val == 0) { write('0'); return; } if (val < 0) { write('-'); val = -val; // todo min } size_t len = 0; while (val) { small[len++] = char(0x30 | (val % 10)); val /= 10; } for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; } pos += len; } void write(const string &s) { for (char c: s) write(c); } void write(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) write(s[i]); } void write(const double &x) { ostringstream oss; oss << setprecision(15) << x; string s = oss.str(); write(s); } void write(const long double &x) { ostringstream oss; oss << setprecision(15) << x; string s = oss.str(); write(s); } template * = nullptr> void write(T &ref) { write(ref.val); } template void write(const vector &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } template void write(const pair &val) { write(val.first); write(' '); write(val.second); } template void write(const tuple &val) { auto &[a, b, c] = val; write(a), write(' '), write(b), write(' '), write(c); } template void write(const tuple &val) { auto &[a, b, c, d] = val; write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d); } template void write(const tuple &val) { auto &[a, b, c, d, e] = val; write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e); } template void write(const tuple &val) { auto &[a, b, c, d, e, f] = val; write(a), write(' '), write(b), write(' '), write(c), write(' '), write(d), write(' '), write(e), write(' '), write(f); } template void write(const array &val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) write(' '); write(val[i]); } } void write(i128 val) { string s; bool negative = 0; if(val < 0){ negative = 1; val = -val; } while (val) { s += '0' + int(val % 10); val /= 10; } if(negative) s += "-"; reverse(all(s)); if (len(s) == 0) s = "0"; write(s); } }; Scanner scanner = Scanner(stdin); Printer printer = Printer(stdout); void flush() { printer.flush(); } void print() { printer.write('\n'); } template void print(Head &&head, Tail &&... tail) { printer.write(head); if (sizeof...(Tail)) printer.write(' '); print(forward(tail)...); } void read() {} template void read(Head &head, Tail &... tail) { scanner.read(head); read(tail...); } #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ read(name) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ read(name) void YES(bool t = 1) { print(t ? "YES" : "NO"); } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { print(t ? "Yes" : "No"); } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { print(t ? "yes" : "no"); } void no(bool t = 1) { yes(!t); } #line 2 "/home/maspy/compro/library/nt/primetest.hpp" struct m64 { using i64 = int64_t; using u64 = uint64_t; using u128 = __uint128_t; inline static u64 m, r, n2; // r * m = -1 (mod 1<<64), n2 = 1<<128 (mod m) static void set_mod(u64 m) { assert(m < (1ull << 62)); assert((m & 1) == 1); m64::m = m; n2 = -u128(m) % m; r = m; FOR(_, 5) r *= 2 - m * r; r = -r; assert(r * m == -1ull); } static u64 reduce(u128 b) { return (b + u128(u64(b) * r) * m) >> 64; } u64 x; m64() : x(0) {} m64(u64 x) : x(reduce(u128(x) * n2)){}; u64 val() const { u64 y = reduce(x); return y >= m ? y - m : y; } m64 &operator+=(m64 y) { x += y.x - (m << 1); x = (i64(x) < 0 ? x + (m << 1) : x); return *this; } m64 &operator-=(m64 y) { x -= y.x; x = (i64(x) < 0 ? x + (m << 1) : x); return *this; } m64 &operator*=(m64 y) { x = reduce(u128(x) * y.x); return *this; } m64 operator+(m64 y) const { return m64(*this) += y; } m64 operator-(m64 y) const { return m64(*this) -= y; } m64 operator*(m64 y) const { return m64(*this) *= y; } bool operator==(m64 y) const { return (x >= m ? x - m : x) == (y.x >= m ? y.x - m : y.x); } bool operator!=(m64 y) const { return not operator==(y); } m64 pow(u64 n) const { m64 y = 1, z = *this; for (; n; n >>= 1, z *= z) if (n & 1) y *= z; return y; } }; bool primetest(const uint64_t x) { using u64 = uint64_t; if (x == 2 or x == 3 or x == 5 or x == 7) return true; if (x % 2 == 0 or x % 3 == 0 or x % 5 == 0 or x % 7 == 0) return false; if (x < 121) return x > 1; const u64 d = (x - 1) >> __builtin_ctzll(x - 1); m64::set_mod(x); const m64 one(1), minus_one(x - 1); auto ok = [&](u64 a) { auto y = m64(a).pow(d); u64 t = d; while (y != one and y != minus_one and t != x - 1) y *= y, t <<= 1; if (y != minus_one and t % 2 == 0) return false; return true; }; if (x < (1ull << 32)) { for (u64 a: {2, 7, 61}) if (not ok(a)) return false; } else { for (u64 a: {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (x <= a) return true; if (not ok(a)) return false; } } return true; } #line 3 "/home/maspy/compro/library/nt/factor.hpp" mt19937_64 rng_mt{random_device{}()}; ll rnd(ll n) { return uniform_int_distribution(0, n - 1)(rng_mt); } ll rho(ll n, ll c) { m64::set_mod(n); assert(n > 1); const m64 cc(c); auto f = [&](m64 x) { return x * x + cc; }; m64 x = 1, y = 2, z = 1, q = 1; ll g = 1; const ll m = 1LL << (__lg(n) / 5); // ? for (ll r = 1; g == 1; r <<= 1) { x = y; FOR(_, r) y = f(y); for (ll k = 0; k < r and g == 1; k += m) { z = y; FOR(_, min(m, r - k)) y = f(y), q *= x - y; g = gcd(q.val(), n); } } if (g == n) do { z = f(z); g = gcd((x - z).val(), n); } while (g == 1); return g; } ll find_prime_factor(ll n) { assert(n > 1); if (primetest(n)) return n; FOR(_, 100) { ll m = rho(n, rnd(n)); if (primetest(m)) return m; n = m; } cerr << "failed" << endl; assert(false); return -1; } vc factor(ll n) { assert(n >= 1); vc pf; FOR3(p, 2, 100) { if (p * p > n) break; if (n % p == 0) { ll e = 0; do { n /= p, e += 1; } while (n % p == 0); pf.eb(p, e); } } while (n > 1) { ll p = find_prime_factor(n); ll e = 0; do { n /= p, e += 1; } while (n % p == 0); pf.eb(p, e); } sort(all(pf)); return pf; } #line 1 "/home/maspy/compro/library/mod/fast_div.hpp" struct fast_div { // Min25 https://judge.yosupo.jp/submission/46090 // 同じ定数で何度も除算するときの高速化に使える using i64 = long long; using u64 = unsigned long long; using u128 = __uint128_t; constexpr fast_div() : m(), s(), x() {} constexpr fast_div(int n) : m(n), s(std::__lg(n - 1)), x(((u128(1) << (s + 64)) + n - 1) / n) {} constexpr friend u64 operator/(u64 n, const fast_div& d) { return (u128(n) * d.x >> d.s) >> 64; } constexpr friend int operator%(u64 n, const fast_div& d) { return n - n / d * d.m; } constexpr std::pair divmod(u64 n) const { u64 q = n / *this; return {q, n - q * m}; } int m; int s; u64 x; }; #line 2 "/home/maspy/compro/library/mod/mod_pow.hpp" // int ll mod_pow(ll a, ll n, int mod){ fast_div fd(mod); a = a % fd; ll p = a; ll v = 1; while(n){ if(n & 1) v = v * p % fd; p = p * p % fd; n >>= 1; } return v; } ll mod_pow_long(ll a, ll n, ll mod){ a %= mod; ll p = a; ll v = 1; while(n){ if(n & 1) v = i128(v) * p % mod; p = i128(p) * p % mod; n >>= 1; } return v; } #line 3 "/home/maspy/compro/library/nt/gaussian_integers.hpp" template struct Gaussian_Integer { INT x, y; using G = Gaussian_Integer; Gaussian_Integer(INT x = 0, INT y = 0) : x(x), y(y) {} Gaussian_Integer(pair p) : x(p.fi), y(p.se) {} INT norm() const { return x * x + y * y; } G conjugate() const { return G(x, -y); } G &operator+=(const G &g) { x += g.x, y += g.y; return *this; } G &operator-=(const G &g) { x -= g.x, y -= g.y; return *this; } G &operator*=(const G &g) { tie(x, y) = mp(x * g.x - y * g.y, x * g.y + y * g.x); return *this; } G &operator/=(const G &g) { *this *= g.conjugate(); INT n = g.norm(); x = floor(x + n / 2, n); y = floor(y + n / 2, n); return *this; } G &operator%=(const G &g) { auto q = G(*this) / g; q *= g; (*this) -= q; return *this; } G operator-() { return G(-x, -y); } G operator+(const G &g) { return G(*this) += g; } G operator-(const G &g) { return G(*this) -= g; } G operator*(const G &g) { return G(*this) *= g; } G operator/(const G &g) { return G(*this) /= g; } G operator%(const G &g) { return G(*this) %= g; } bool operator==(const G &g) { return (x == g.x && y == g.y); } static G gcd(G a, G b) { while (b.x != 0 || b.y != 0) { a %= b; swap(a, b); } return a; } }; // ノルムがとれるように、2 乗してもオーバーフローしない型を使おう // 0 <= arg < 90 となるもののみ返す。 // 単数倍は作らないので、使うときに気を付ける。 template vc> solve_norm_equation(INT N) { using G = Gaussian_Integer; vc res; if (N < 0) return {}; if (N == 0) { res.eb(G(0, 0)); return res; } auto pfs = factor(N); for (auto &&[p, e]: pfs) { if (p % 4 == 3 && e % 2 == 1) return {}; } auto find = [&](INT p) -> G { // p は素数. ノルム p のガウス整数をひとつ見つける if (p == 2) return G(1, 1); // x^2 = -1 mod p をひとつ見つける INT x = [&]() -> INT { INT x = 1; while (1) { ++x; INT pow_x = 1; if (p < (1 << 30)) { pow_x = mod_pow(x, (p - 1) / 4, p); if (pow_x * pow_x % p == p - 1) return pow_x; } else { pow_x = mod_pow_long(x, (p - 1) / 4, p); if (i128(pow_x) * pow_x % p == p - 1) return pow_x; } } return -1; }(); assert(x != -1); // x は非剰余 G a(p, 0), b(x, 1); a = G::gcd(a, b); assert(a.norm() == p); return a; }; res.eb(G(1, 0)); for (auto &&[p, e]: pfs) { if (p % 4 == 3) { INT pp = 1; FOR_(e / 2) pp *= p; for (auto &&g: res) { g.x *= pp; g.y *= pp; } continue; } auto pi = find(p); vc pows(e + 1); pows[0] = G(1, 0); FOR(i, e) pows[i + 1] = pows[i] * pi; if (p == 2) { for (auto &&g: res) g *= pows[e]; continue; } vc pis(e + 1); FOR(j, e + 1) { pis[j] = pows[j] * (pows[e - j].conjugate()); } vc new_res; new_res.reserve(len(res) * (e + 1)); for (auto &&g: res) { for (auto &&a: pis) { new_res.eb(g * a); } } swap(res, new_res); } for (auto &&g: res) { while (g.x <= 0 || g.y < 0) { g = G(-g.y, g.x); } } return res; } #line 5 "main.cpp" void solve() { LL(N); ll ANS = 0; auto res = solve_norm_equation(N * N); for (auto &&g: res) { if (g.x > 0 && g.y > 0) ++ANS; } print(ANS); } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << setprecision(15); ll T = 1; // LL(T); FOR(_, T) solve(); return 0; }