Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cost[1000009];
ll n,k;
ll pr[1000009];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    for(ll i=2;i<1000;i++)
    {
        if(pr[i])continue;

        for(int j=i;j<=1000000;j+=i)pr[j]=i;
    }
    for(int i=2;i<=1000000;i++)
        if(pr[i]==0)pr[i]=i;
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        ll x,y;
        cin>>x>>y;
        if(x>1000000)continue;
        cost[x]=y;

    }
    ll ans=0;
    for(ll i=2;i<=1000000;i++)
    {
        ll x=i;
        bool bl=true;
        ll u=0,r=0;
        while(x>1)
        {
            ll d=pr[x];
            if((x%(d*d))==0){bl=false;break;}
            if(cost[d]==0){bl=false;break;}
          u++;
          r=max(r,cost[d]);
          x/=d;
        }
    if(!bl)continue;
    ll z=k/(i*i);
      ll p=r*z;
      if(u%2)ans+=p;
        else ans-=p;

    }
    cout<<ans<<endl;
}
Copy
Number of the Beast Kaitokid
GNU G++17
116 ms
16.5 MB
Accepted