mr_dot_convict

12-Highly_divisible_triangular_num_ProjectEuler-mr.convict

Jun 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC optimize("Ofast")
  11. #pragma GCC optimize("unroll-loops")
  12. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS                                                                    \
  15.   ios_base::sync_with_stdio(false);                                            \
  16.   cin.tie(nullptr)
  17. #define PREC                                                                   \
  18.   cout.precision(10);                                                          \
  19.   cout << fixed
  20. #define bg(x) " [ " << #x << " : " << (x) << " ]"
  21. #define x first
  22. #define y second
  23.  
  24. #define debug(args...)                                                         \
  25.   {                                                                            \
  26.     string _s = #args;                                                         \
  27.     replace(_s.begin(), _s.end(), ',', ' ');                                   \
  28.     stringstream _ss(_s);                                                      \
  29.     istream_iterator<string> _it(_ss);                                         \
  30.     err(_it, args);                                                            \
  31.   }
  32. void err(istream_iterator<string> it) {
  33.   it->empty();
  34.   cerr << " (Line : " << __LINE__ << ")" << '\n';
  35. }
  36. template <typename T, typename... Args>
  37. void err(istream_iterator<string> it, T a, Args... args) {
  38.   cerr << " [ " << *it << " : " << a << " ] " << ' ';
  39.   err(++it, args...);
  40. }
  41. using ll = long long;
  42.  
  43. const int MAX_P = 300;
  44. vector<int> primes = {2};
  45. void sieve() {
  46.   vector<bool> isPrime(MAX_P + 1, true);
  47.   for (int i = 3; i * 2 <= MAX_P; i += 2) {
  48.     int p = 0;
  49.     while (1ll * i * (i + p) <= MAX_P)
  50.       isPrime[i * (i + p)] = false, ++p;
  51.   }
  52.   for (int i = 3; i <= MAX_P; i += 2) {
  53.     if (isPrime[i])
  54.       primes.push_back(i);
  55.   }
  56. }
  57.  
  58. int total_div(int n) {
  59.   int td = 1;
  60.   int n_ref = n;
  61.   for (int p : primes) {
  62.     if (1ll * p * p > n_ref)
  63.       break;
  64.     int alpha = 0;
  65.     while (n % p == 0)
  66.       n /= p, ++alpha;
  67.     td *= (1 + alpha);
  68.   }
  69.   td *= (1 + (n != 1));
  70.   return td;
  71. }
  72.  
  73. ll solve(int n) {
  74.   for (int i = 1; i <= 41040; ++i) {
  75.     ll tri = (1ll * i * (i + 1)) / 2;
  76.     ll ansl = total_div((i & 1 ? i : i / 2));
  77.     ll ansr = total_div(((i + 1) & 1 ? i + 1 : (i + 1) / 2));
  78.     ll ans = ansl * ansr;
  79.     if (ans > n)
  80.       return tri;
  81.   }
  82.   assert(false);
  83.   return -1;
  84. }
  85.  
  86. signed main() {
  87.   IOS;
  88.   PREC;
  89.   sieve();
  90.   int tc;
  91.   cin >> tc;
  92.   while (tc--) {
  93.     int n;
  94.     cin >> n;
  95.     cout << solve(n) << '\n';
  96.   }
  97.   return EXIT_SUCCESS;
  98. }
Add Comment
Please, Sign In to add comment