Source Code
#include <bits/stdc++.h>
#include <cwchar>

#define speed                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long
#define YES "Yes"
#define NO "No"
#define endline '\n'

using namespace std;

const ll N = 1e6 + 10, MM = 1e9 + 7, MAX = 1e18;

ll mxCost[N], p[N];
ll k;

ll res;

int solve() {
  int n;
  cin >> n;
  cin >> k;
  set<ll> s;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll y;
    cin >> y;
    if (x > 1e6)
      continue;
    for (ll j = x; j * j <= k; j += x) {
      if (mxCost[j] == -1)
        continue;
      if (j / x % x == 0)
        mxCost[j] = -1;
      else
        mxCost[j] = max(mxCost[j], y);
    }
    for (ll j = x; j * j <= k; j += x) {
      p[j] = x;
    }
  }
  for (ll i = 2; i * i <= k; i++) {
    ll x = i;
    ll f = -1;
    while (p[x]) {
      x /= p[x];
      f *= -1;
    }
    if (x > 1)
      continue;
    if (mxCost[i] == -1)
      continue;
    res += k / i / i * f * mxCost[i];
  }
  cout << res << endline;
  return 0;
}

int main() {
  speed;
  int t = 1;
  // cin >> t;
  int i = 1;
  while (t--) {
    // cout << "Case #" << i++ << ": ";
    solve();
  }
  return 0;
}
/*


*/
Copy
Number of the Beast srdamaa7
GNU G++17
174 ms
16.8 MB
Accepted