tien_noob

SQUARE(DHBB)

Feb 10th, 2021 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. //New start, best wishes
  6. const int N = 5e6;
  7. int n, a[N+1], c[N+1], d[N+1];
  8. bool b[N+1];
  9. using namespace std;
  10. vector<int> prime;
  11. void sieve()
  12. {
  13.     fill (b + 1, b + n + 1, true);
  14.     fill (a + 1, a + n + 1, 1);
  15.     fill(d +1, d + n + 1, 0);
  16.     iota(c + 1, c + n + 1, 1);
  17.     for (int i = 2; i <= n; ++ i)
  18.     {
  19.         if (b[i])
  20.         {
  21.             for (int j = i; j <= n; j += i)
  22.             {
  23.                 b[j] = false;
  24.                 int cnt = 0;
  25.                 while (c[j] % i == 0)
  26.                 {
  27.                     c[j] /= i;
  28.                     ++cnt;
  29.                 }
  30.                 cnt = cnt % 2;
  31.                 if (cnt == 1)
  32.                 {
  33.                     a[j] *= i;
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
  39. void read()
  40. {
  41.     cin >> n;
  42.     sieve();
  43. }
  44. void solve()
  45. {
  46.     a[1] = 1;
  47.     long long ans = 0;
  48.     for (int i = 1; i <= n; ++ i)
  49.     {
  50.         ans += (long long)d[a[i]] * (d[a[i]] - 1)/2;
  51.         ++d[a[i]];
  52.     }
  53.     cout << ans;
  54. }
  55. int main()
  56. {
  57.     ios_base::sync_with_stdio(false);
  58.     cin.tie(nullptr);
  59.     //freopen("SQUARE.INP", "r", stdin);
  60.     //freopen("SQUARE.OUT", "w", stdout);
  61.     read();
  62.     solve();
  63. }
  64.  
Add Comment
Please, Sign In to add comment