Beingamanforever

Sum of Divisors, nlogn Precomputation

Dec 27th, 2024
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /**
  2.  *    author:  heWhoCooks
  3.  *    created: 2024-12-27 22:45:28
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define pb push_back
  21. #define pf push_front
  22. #define yes cout << "YES" << endl
  23. #define no cout << "NO" << endl
  24. #define endl "\n"
  25. #define M_PI 3.14159265358979323846L
  26. const int mod = 1e9 + 7;
  27. const int INF = 2e18;
  28. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  29. const int N = 1e7 + 5;
  30. vi sumd(N, 0);
  31. vi mpp(N, 0);
  32. void sumof_div()
  33. {
  34.     // just do O(nlogn)
  35.     for (int i = 1; i < N; i++)
  36.     {
  37.         for (int j = i; j < N; j += i)
  38.         {
  39.             sumd[j] += i;
  40.         }
  41.         // limit on c = 1e7
  42.         if (sumd[i] <= (1e7))
  43.         {
  44.             if (mpp[sumd[i]] == 0)
  45.             {
  46.                 mpp[sumd[i]] = i;
  47.             }
  48.         }
  49.     }
  50. }
  51. void solve()
  52. {
  53.     int n;
  54.     cin >> n;
  55.     if (mpp[n] == 0)
  56.     {
  57.         cout << -1 << endl;
  58.     }
  59.     else
  60.     {
  61.         cout << mpp[n] << endl;
  62.     }
  63.     return;
  64. }
  65.  
  66. signed main()
  67. {
  68.     NeedForSpeed;
  69.     int t = 1;
  70.     cin >> t;
  71.     sumof_div();
  72.     while (t--)
  73.     {
  74.         solve();
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment