fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define llong long long
  4. #define N (int)1e5 + 6
  5.  
  6. const int BASE_DIGITS = 9;
  7. const int BASE = 1000000000;
  8.  
  9. struct BigInt
  10. {
  11. int sign;
  12. vector<int> a;
  13.  
  14. // -------------------- Constructors --------------------
  15. // Default constructor.
  16. BigInt() : sign(1) {}
  17.  
  18. // Constructor from long long.
  19. BigInt(long long v)
  20. {
  21. *this = v;
  22. }
  23. BigInt &operator=(long long v)
  24. {
  25. sign = 1;
  26. if (v < 0)
  27. {
  28. sign = -1;
  29. v = -v;
  30. }
  31. a.clear();
  32. for (; v > 0; v = v / BASE)
  33. a.push_back(v % BASE);
  34. return *this;
  35. }
  36.  
  37. // Initialize from string.
  38. BigInt(const string &s)
  39. {
  40. read(s);
  41. }
  42.  
  43. // -------------------- Input / Output --------------------
  44. void read(const string &s)
  45. {
  46. sign = 1;
  47. a.clear();
  48. int pos = 0;
  49. while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+'))
  50. {
  51. if (s[pos] == '-')
  52. sign = -sign;
  53. ++pos;
  54. }
  55. for (int i = s.size() - 1; i >= pos; i -= BASE_DIGITS)
  56. {
  57. int x = 0;
  58. for (int j = max(pos, i - BASE_DIGITS + 1); j <= i; j++)
  59. x = x * 10 + s[j] - '0';
  60. a.push_back(x);
  61. }
  62. trim();
  63. }
  64. friend istream &operator>>(istream &stream, BigInt &v)
  65. {
  66. string s;
  67. stream >> s;
  68. v.read(s);
  69. return stream;
  70. }
  71.  
  72. friend ostream &operator<<(ostream &stream, const BigInt &v)
  73. {
  74. if (v.sign == -1 && !v.isZero())
  75. stream << '-';
  76. stream << (v.a.empty() ? 0 : v.a.back());
  77. for (int i = (int)v.a.size() - 2; i >= 0; --i)
  78. stream << setw(BASE_DIGITS) << setfill('0') << v.a[i];
  79. return stream;
  80. }
  81.  
  82. // -------------------- Comparison --------------------
  83. bool operator<(const BigInt &v) const
  84. {
  85. if (sign != v.sign)
  86. return sign < v.sign;
  87. if (a.size() != v.a.size())
  88. return a.size() * sign < v.a.size() * v.sign;
  89. for (int i = ((int)a.size()) - 1; i >= 0; i--)
  90. if (a[i] != v.a[i])
  91. return a[i] * sign < v.a[i] * sign;
  92. return false;
  93. }
  94.  
  95. bool operator>(const BigInt &v) const
  96. {
  97. return v < *this;
  98. }
  99. bool operator<=(const BigInt &v) const
  100. {
  101. return !(v < *this);
  102. }
  103. bool operator>=(const BigInt &v) const
  104. {
  105. return !(*this < v);
  106. }
  107. bool operator==(const BigInt &v) const
  108. {
  109. return !(*this < v) && !(v < *this);
  110. }
  111. bool operator!=(const BigInt &v) const
  112. {
  113. return *this < v || v < *this;
  114. }
  115.  
  116. // Returns:
  117. // 0 if |x| == |y|
  118. // -1 if |x| < |y|
  119. // 1 if |x| > |y|
  120. friend int __compare_abs(const BigInt &x, const BigInt &y)
  121. {
  122. if (x.a.size() != y.a.size())
  123. {
  124. return x.a.size() < y.a.size() ? -1 : 1;
  125. }
  126.  
  127. for (int i = ((int)x.a.size()) - 1; i >= 0; --i)
  128. {
  129. if (x.a[i] != y.a[i])
  130. {
  131. return x.a[i] < y.a[i] ? -1 : 1;
  132. }
  133. }
  134. return 0;
  135. }
  136.  
  137. // -------------------- Unary operator - and operators +- --------------------
  138. BigInt operator-() const
  139. {
  140. BigInt res = *this;
  141. if (isZero())
  142. return res;
  143.  
  144. res.sign = -sign;
  145. return res;
  146. }
  147.  
  148. // Note: sign ignored.
  149. void __internal_add(const BigInt &v)
  150. {
  151. if (a.size() < v.a.size())
  152. {
  153. a.resize(v.a.size(), 0);
  154. }
  155. for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) || carry; ++i)
  156. {
  157. if (i == (int)a.size())
  158. a.push_back(0);
  159.  
  160. a[i] += carry + (i < (int)v.a.size() ? v.a[i] : 0);
  161. carry = a[i] >= BASE;
  162. if (carry)
  163. a[i] -= BASE;
  164. }
  165. }
  166.  
  167. // Note: sign ignored.
  168. void __internal_sub(const BigInt &v)
  169. {
  170. for (int i = 0, carry = 0; i < (int)v.a.size() || carry; ++i)
  171. {
  172. a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0);
  173. carry = a[i] < 0;
  174. if (carry)
  175. a[i] += BASE;
  176. }
  177. this->trim();
  178. }
  179.  
  180. BigInt operator+=(const BigInt &v)
  181. {
  182. if (sign == v.sign)
  183. {
  184. __internal_add(v);
  185. }
  186. else
  187. {
  188. if (__compare_abs(*this, v) >= 0)
  189. {
  190. __internal_sub(v);
  191. }
  192. else
  193. {
  194. BigInt vv = v;
  195. swap(*this, vv);
  196. __internal_sub(vv);
  197. }
  198. }
  199. return *this;
  200. }
  201.  
  202. BigInt operator-=(const BigInt &v)
  203. {
  204. if (sign == v.sign)
  205. {
  206. if (__compare_abs(*this, v) >= 0)
  207. {
  208. __internal_sub(v);
  209. }
  210. else
  211. {
  212. BigInt vv = v;
  213. swap(*this, vv);
  214. __internal_sub(vv);
  215. this->sign = -this->sign;
  216. }
  217. }
  218. else
  219. {
  220. __internal_add(v);
  221. }
  222. return *this;
  223. }
  224.  
  225. // Optimize operators + and - according to
  226. // https://stackoverflow.com/questions/13166079/move-semantics-and-pass-by-rvalue-reference-in-overloaded-arithmetic
  227. template <typename L, typename R>
  228. typename std::enable_if<
  229. std::is_convertible<L, BigInt>::value &&
  230. std::is_convertible<R, BigInt>::value &&
  231. std::is_lvalue_reference<R &&>::value,
  232. BigInt>::type friend
  233. operator+(L &&l, R &&r)
  234. {
  235. BigInt result(std::forward<L>(l));
  236. result += r;
  237. return result;
  238. }
  239. template <typename L, typename R>
  240. typename std::enable_if<
  241. std::is_convertible<L, BigInt>::value &&
  242. std::is_convertible<R, BigInt>::value &&
  243. std::is_rvalue_reference<R &&>::value,
  244. BigInt>::type friend
  245. operator+(L &&l, R &&r)
  246. {
  247. BigInt result(std::move(r));
  248. result += l;
  249. return result;
  250. }
  251.  
  252. template <typename L, typename R>
  253. typename std::enable_if<
  254. std::is_convertible<L, BigInt>::value &&
  255. std::is_convertible<R, BigInt>::value,
  256. BigInt>::type friend
  257. operator-(L &&l, R &&r)
  258. {
  259. BigInt result(std::forward<L>(l));
  260. result -= r;
  261. return result;
  262. }
  263.  
  264. // -------------------- Operators * / % --------------------
  265. friend pair<BigInt, BigInt> divmod(const BigInt &a1, const BigInt &b1)
  266. {
  267. assert(b1 > 0); // divmod not well-defined for b < 0.
  268.  
  269. long long norm = BASE / (b1.a.back() + 1);
  270. BigInt a = a1.abs() * norm;
  271. BigInt b = b1.abs() * norm;
  272. BigInt q = 0, r = 0;
  273. q.a.resize(a.a.size());
  274.  
  275. for (int i = a.a.size() - 1; i >= 0; i--)
  276. {
  277. r *= BASE;
  278. r += a.a[i];
  279. long long s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
  280. long long s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
  281. long long d = ((long long)BASE * s1 + s2) / b.a.back();
  282. r -= b * d;
  283. while (r < 0)
  284. {
  285. r += b, --d;
  286. }
  287. q.a[i] = d;
  288. }
  289.  
  290. q.sign = a1.sign * b1.sign;
  291. r.sign = a1.sign;
  292. q.trim();
  293. r.trim();
  294. auto res = make_pair(q, r / norm);
  295. if (res.second < 0)
  296. res.second += b1;
  297. return res;
  298. }
  299. BigInt operator/(const BigInt &v) const
  300. {
  301. return divmod(*this, v).first;
  302. }
  303.  
  304. BigInt operator%(const BigInt &v) const
  305. {
  306. return divmod(*this, v).second;
  307. }
  308.  
  309. void operator/=(int v)
  310. {
  311. assert(v > 0); // operator / not well-defined for v <= 0.
  312. if (llabs(v) >= BASE)
  313. {
  314. *this /= BigInt(v);
  315. return;
  316. }
  317. if (v < 0)
  318. sign = -sign, v = -v;
  319. for (int i = (int)a.size() - 1, rem = 0; i >= 0; --i)
  320. {
  321. long long cur = a[i] + rem * (long long)BASE;
  322. a[i] = (int)(cur / v);
  323. rem = (int)(cur % v);
  324. }
  325. trim();
  326. }
  327.  
  328. BigInt operator/(int v) const
  329. {
  330. assert(v > 0); // operator / not well-defined for v <= 0.
  331.  
  332. if (llabs(v) >= BASE)
  333. {
  334. return *this / BigInt(v);
  335. }
  336. BigInt res = *this;
  337. res /= v;
  338. return res;
  339. }
  340. void operator/=(const BigInt &v)
  341. {
  342. *this = *this / v;
  343. }
  344.  
  345. long long operator%(long long v) const
  346. {
  347. assert(v > 0); // operator / not well-defined for v <= 0.
  348. assert(v < BASE);
  349. int m = 0;
  350. for (int i = a.size() - 1; i >= 0; --i)
  351. m = (a[i] + m * (long long)BASE) % v;
  352. return m * sign;
  353. }
  354.  
  355. void operator*=(int v)
  356. {
  357. if (llabs(v) >= BASE)
  358. {
  359. *this *= BigInt(v);
  360. return;
  361. }
  362. if (v < 0)
  363. sign = -sign, v = -v;
  364. for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i)
  365. {
  366. if (i == (int)a.size())
  367. a.push_back(0);
  368. long long cur = a[i] * (long long)v + carry;
  369. carry = (int)(cur / BASE);
  370. a[i] = (int)(cur % BASE);
  371. //asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
  372. /*
  373.   int val;
  374.   __asm {
  375.   lea esi, cur
  376.   mov eax, [esi]
  377.   mov edx, [esi+4]
  378.   mov ecx, base
  379.   div ecx
  380.   mov carry, eax
  381.   mov val, edx;
  382.   }
  383.   a[i] = val;
  384.   */
  385. }
  386. trim();
  387. }
  388.  
  389. BigInt operator*(int v) const
  390. {
  391. if (llabs(v) >= BASE)
  392. {
  393. return *this * BigInt(v);
  394. }
  395. BigInt res = *this;
  396. res *= v;
  397. return res;
  398. }
  399.  
  400. // Convert BASE 10^old --> 10^new.
  401. static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits)
  402. {
  403. vector<long long> p(max(old_digits, new_digits) + 1);
  404. p[0] = 1;
  405. for (int i = 1; i < (int)p.size(); i++)
  406. p[i] = p[i - 1] * 10;
  407. vector<int> res;
  408. long long cur = 0;
  409. int cur_digits = 0;
  410. for (int i = 0; i < (int)a.size(); i++)
  411. {
  412. cur += a[i] * p[cur_digits];
  413. cur_digits += old_digits;
  414. while (cur_digits >= new_digits)
  415. {
  416. res.push_back((long long)(cur % p[new_digits]));
  417. cur /= p[new_digits];
  418. cur_digits -= new_digits;
  419. }
  420. }
  421. res.push_back((int)cur);
  422. while (!res.empty() && !res.back())
  423. res.pop_back();
  424. return res;
  425. }
  426.  
  427. void fft(vector<complex<double>> &a, bool invert) const
  428. {
  429. int n = (int)a.size();
  430.  
  431. for (int i = 1, j = 0; i < n; ++i)
  432. {
  433. int bit = n >> 1;
  434. for (; j >= bit; bit >>= 1)
  435. j -= bit;
  436. j += bit;
  437. if (i < j)
  438. swap(a[i], a[j]);
  439. }
  440.  
  441. for (int len = 2; len <= n; len <<= 1)
  442. {
  443. double ang = 2 * 3.14159265358979323846 / len * (invert ? -1 : 1);
  444. complex<double> wlen(cos(ang), sin(ang));
  445. for (int i = 0; i < n; i += len)
  446. {
  447. complex<double> w(1);
  448. for (int j = 0; j < len / 2; ++j)
  449. {
  450. complex<double> u = a[i + j];
  451. complex<double> v = a[i + j + len / 2] * w;
  452. a[i + j] = u + v;
  453. a[i + j + len / 2] = u - v;
  454. w *= wlen;
  455. }
  456. }
  457. }
  458. if (invert)
  459. for (int i = 0; i < n; ++i)
  460. a[i] /= n;
  461. }
  462.  
  463. void multiply_fft(const vector<int> &a, const vector<int> &b, vector<int> &res) const
  464. {
  465. vector<complex<double>> fa(a.begin(), a.end());
  466. vector<complex<double>> fb(b.begin(), b.end());
  467. int n = 1;
  468. while (n < (int)max(a.size(), b.size()))
  469. n <<= 1;
  470. n <<= 1;
  471. fa.resize(n);
  472. fb.resize(n);
  473.  
  474. fft(fa, false);
  475. fft(fb, false);
  476. for (int i = 0; i < n; ++i)
  477. fa[i] *= fb[i];
  478. fft(fa, true);
  479.  
  480. res.resize(n);
  481. long long carry = 0;
  482. for (int i = 0; i < n; ++i)
  483. {
  484. long long t = (long long)(fa[i].real() + 0.5) + carry;
  485. carry = t / 1000;
  486. res[i] = t % 1000;
  487. }
  488. }
  489.  
  490. BigInt mul_simple(const BigInt &v) const
  491. {
  492. BigInt res;
  493. res.sign = sign * v.sign;
  494. res.a.resize(a.size() + v.a.size());
  495. for (int i = 0; i < (int)a.size(); ++i)
  496. if (a[i])
  497. for (int j = 0, carry = 0; j < (int)v.a.size() || carry; ++j)
  498. {
  499. long long cur = res.a[i + j] + (long long)a[i] * (j < (int)v.a.size() ? v.a[j] : 0) + carry;
  500. carry = (int)(cur / BASE);
  501. res.a[i + j] = (int)(cur % BASE);
  502. }
  503. res.trim();
  504. return res;
  505. }
  506.  
  507. typedef vector<long long> vll;
  508.  
  509. static vll karatsubaMultiply(const vll &a, const vll &b)
  510. {
  511. int n = a.size();
  512. vll res(n + n);
  513. if (n <= 32)
  514. {
  515. for (int i = 0; i < n; i++)
  516. for (int j = 0; j < n; j++)
  517. res[i + j] += a[i] * b[j];
  518. return res;
  519. }
  520.  
  521. int k = n >> 1;
  522. vll a1(a.begin(), a.begin() + k);
  523. vll a2(a.begin() + k, a.end());
  524. vll b1(b.begin(), b.begin() + k);
  525. vll b2(b.begin() + k, b.end());
  526.  
  527. vll a1b1 = karatsubaMultiply(a1, b1);
  528. vll a2b2 = karatsubaMultiply(a2, b2);
  529.  
  530. for (int i = 0; i < k; i++)
  531. a2[i] += a1[i];
  532. for (int i = 0; i < k; i++)
  533. b2[i] += b1[i];
  534.  
  535. vll r = karatsubaMultiply(a2, b2);
  536. for (int i = 0; i < (int)a1b1.size(); i++)
  537. r[i] -= a1b1[i];
  538. for (int i = 0; i < (int)a2b2.size(); i++)
  539. r[i] -= a2b2[i];
  540.  
  541. for (int i = 0; i < (int)r.size(); i++)
  542. res[i + k] += r[i];
  543. for (int i = 0; i < (int)a1b1.size(); i++)
  544. res[i] += a1b1[i];
  545. for (int i = 0; i < (int)a2b2.size(); i++)
  546. res[i + n] += a2b2[i];
  547. return res;
  548. }
  549.  
  550. BigInt mul_karatsuba(const BigInt &v) const
  551. {
  552. vector<int> a6 = convert_base(this->a, BASE_DIGITS, 6);
  553. vector<int> b6 = convert_base(v.a, BASE_DIGITS, 6);
  554. vll a(a6.begin(), a6.end());
  555. vll b(b6.begin(), b6.end());
  556. while (a.size() < b.size())
  557. a.push_back(0);
  558. while (b.size() < a.size())
  559. b.push_back(0);
  560. while (a.size() & (a.size() - 1))
  561. a.push_back(0), b.push_back(0);
  562. vll c = karatsubaMultiply(a, b);
  563. BigInt res;
  564. res.sign = sign * v.sign;
  565. long long carry = 0;
  566. for (int i = 0; i < (int)c.size(); i++)
  567. {
  568. long long cur = c[i] + carry;
  569. res.a.push_back((int)(cur % 1000000));
  570. carry = cur / 1000000;
  571. }
  572. res.a = convert_base(res.a, 6, BASE_DIGITS);
  573. res.trim();
  574. return res;
  575. }
  576.  
  577. void operator*=(const BigInt &v)
  578. {
  579. *this = *this * v;
  580. }
  581. BigInt operator*(const BigInt &v) const
  582. {
  583. if (a.size() * v.a.size() <= 1000111)
  584. return mul_simple(v);
  585. if (a.size() > 500111 || v.a.size() > 500111)
  586. return mul_fft(v);
  587. return mul_karatsuba(v);
  588. }
  589.  
  590. BigInt mul_fft(const BigInt &v) const
  591. {
  592. BigInt res;
  593. res.sign = sign * v.sign;
  594. multiply_fft(convert_base(a, BASE_DIGITS, 3), convert_base(v.a, BASE_DIGITS, 3), res.a);
  595. res.a = convert_base(res.a, 3, BASE_DIGITS);
  596. res.trim();
  597. return res;
  598. }
  599.  
  600. // -------------------- Misc --------------------
  601. BigInt abs() const
  602. {
  603. BigInt res = *this;
  604. res.sign *= res.sign;
  605. return res;
  606. }
  607. void trim()
  608. {
  609. while (!a.empty() && !a.back())
  610. a.pop_back();
  611. if (a.empty())
  612. sign = 1;
  613. }
  614.  
  615. bool isZero() const
  616. {
  617. return a.empty() || (a.size() == 1 && !a[0]);
  618. }
  619.  
  620. friend BigInt gcd(const BigInt &a, const BigInt &b)
  621. {
  622. return b.isZero() ? a : gcd(b, a % b);
  623. }
  624. friend BigInt lcm(const BigInt &a, const BigInt &b)
  625. {
  626. return a / gcd(a, b) * b;
  627. }
  628.  
  629. friend BigInt sqrt(const BigInt &a1)
  630. {
  631. BigInt a = a1;
  632. while (a.a.empty() || a.a.size() % 2 == 1)
  633. a.a.push_back(0);
  634.  
  635. int n = a.a.size();
  636.  
  637. int firstDigit = (int)sqrt((double)a.a[n - 1] * BASE + a.a[n - 2]);
  638. int norm = BASE / (firstDigit + 1);
  639. a *= norm;
  640. a *= norm;
  641. while (a.a.empty() || a.a.size() % 2 == 1)
  642. a.a.push_back(0);
  643.  
  644. BigInt r = (long long)a.a[n - 1] * BASE + a.a[n - 2];
  645. firstDigit = (int)sqrt((double)a.a[n - 1] * BASE + a.a[n - 2]);
  646. int q = firstDigit;
  647. BigInt res;
  648.  
  649. for (int j = n / 2 - 1; j >= 0; j--)
  650. {
  651. for (;; --q)
  652. {
  653. BigInt r1 = (r - (res * 2 * BigInt(BASE) + q) * q) * BigInt(BASE) * BigInt(BASE) + (j > 0 ? (long long)a.a[2 * j - 1] * BASE + a.a[2 * j - 2] : 0);
  654. if (r1 >= 0)
  655. {
  656. r = r1;
  657. break;
  658. }
  659. }
  660. res *= BASE;
  661. res += q;
  662.  
  663. if (j > 0)
  664. {
  665. int d1 = res.a.size() + 2 < r.a.size() ? r.a[res.a.size() + 2] : 0;
  666. int d2 = res.a.size() + 1 < r.a.size() ? r.a[res.a.size() + 1] : 0;
  667. int d3 = res.a.size() < r.a.size() ? r.a[res.a.size()] : 0;
  668. q = ((long long)d1 * BASE * BASE + (long long)d2 * BASE + d3) / (firstDigit * 2);
  669. }
  670. }
  671.  
  672. res.trim();
  673. return res / norm;
  674. }
  675. };
  676.  
  677. set<BigInt> S;
  678. BigInt a, b, res1, res2;
  679. long long po(long long a, long long n)
  680. {
  681. long long res = a, ans = 1;
  682. while (1)
  683. {
  684. if (n == 0)
  685. break;
  686. if (n % 2)
  687. ans = ans * res;
  688. res = res * res;
  689. n = n / 2;
  690. }
  691. return ans;
  692. }
  693. long long sqrt3(long long n)
  694. {
  695. long long l = 1, r = 1000000;
  696. while (l <= r)
  697. {
  698. long long mid = (l + r) / 2;
  699. if ((long long)mid * mid * mid <= n)
  700. {
  701. l = mid + 1;
  702. }
  703. else
  704. {
  705. r = mid - 1;
  706. }
  707. }
  708. return l - 1;
  709. }
  710. signed main()
  711. {
  712. ios_base::sync_with_stdio(false);
  713. cin.tie(0);
  714. cout.tie(0);
  715. long long n, k;
  716. cin >> n;
  717. k = sqrt3(n);
  718. BigInt ans = 0;
  719. for (long long i = 1; i < k; i++)
  720. {
  721. long long tmp = i * (po(i + 1, 3) - 1 - po(i, 3) + 1);
  722. ans = ans + tmp;
  723. }
  724. long long tmp1 = k * (n - po(k, 3) + 1);
  725. ans = ans + tmp1;
  726. cout << ans;
  727. return 0;
  728. }
Success #stdin #stdout 0.01s 5560KB
stdin
Standard input is empty
stdout
5445288071202805088