Source Code
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define S second
#define F first
#define ld long double
#define pb push_back
#define LC in+1
#define RC in+2*(mid-l+1)
#define UB upper_bound
#define LB lower_bound
#define pi pair<int,int>
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
/// if a & m are relatively prime, then (a^(n))%m=(a^(n%phi(m)))%m
/// if not relatively prime, then (a^(n))%m=(a^(phi(m)+n%phi(m)))%m
ld Pi=3.14159265358979323846;
ll oo=998244353;
mt19937_64 rnd(98275314);
ll mul(ll x,ll y)
{
    return ((x%oo)*(y%oo))%oo;
}
ll add(ll x,ll y)
{
    return ((x%oo)+(y%oo))%oo;
}
ll sub(ll x,ll y)
{
    return ((x%oo)-(y%oo)+oo+oo)%oo;
}
ll fast_pow(ll x,ll y)
{
    ll ret=1;
    while(y)
    {
        if(y&1)ret=mul(ret,x);
        x=mul(x,x);
        y>>=1;
    }
    return ret;
}
ll fact[200];
ll inv[200];
ll calculate_inverse(ll x)
{
    return fast_pow(x,oo-2);
}
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=200000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
    inv[200000]=calculate_inverse(fact[200000]);
    for(int i=200000; i>0; i--)inv[i-1]=mul(inv[i],i);
}
ll nCr(ll n,ll r)
{
    if(r<0)return 0;
    if(r>n)return 0;
    ll ret=1;
    ret=fact[n];
    ret=mul(ret,inv[r]);
    ret=mul(ret,inv[n-r]);
    //ret=mul(ret,calculate_inverse(fact[r]));
    //ret=mul(ret,calculate_inverse(fact[n-r]));
    return ret;
}
ll gcd(ll x,ll y)
{
    if(x<y)swap(x,y);
    if(y==0)return x;
    return gcd(y,x%y);
}
ll phi(ll x)
{
    ll ans=x;
    for(ll i=2; i*i<=x; i++)
    {
        if(x%i!=0)continue;
        while(x%i==0)x/=i;
        ans-=ans/i;
    }
    if(x!=1)
    {
        ans-=ans/x;
    }
    return ans;
}
ll lcm(ll a,ll b)
{
    ll ret=a/gcd(a,b);
    return ret*b;
}
ll ex_gcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        y=0;
        x=1;
        return a;
    }
    ll x1,y1;
    ll g=ex_gcd(b,a%b,x1,y1);
    x=y1;
    y=x1-y1*(a/b);
    return g;
}
int get_set_bits(ll x){return __builtin_popcount(x);}





ll n,m,k;
const int N=1e6+10;
pair<ll,ll>a[N];
ll exist[N];
int isPrime[N];
void sieve()
{
    for(ll i=2;i<=1e6;i++)
    {
        if(isPrime[i]!=0)continue;
        isPrime[i]=i;
        for(ll j=i*i;j<=1e6;j+=i)
            isPrime[j]=i;
    }
}
void test_case()
{
    sieve();
    cin>>n>>k;
    map<ll,ll>mp;
    set<ll>st;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].F>>a[i].S;
        if(a[i].F>1e6)continue;
        exist[a[i].F]=a[i].S;
    }
    ll ans=0;
    for(ll i=2;i*i<=k;i++)
    {
        ll j=i;
        set<ll>temp;
        ll mx=0;
        bool flag=false;
        while(j>1)
        {
            if(exist[isPrime[j]]==0)
            {
                flag=true;
                break;
            }
            if(temp.count(isPrime[j]))
            {
                flag=true;
                break;
            }
            temp.insert(isPrime[j]);
            mx=max(mx,exist[isPrime[j]]);
            j/=isPrime[j];
        }
        if(flag)continue;
        if(temp.size()&1)ans+=mx*(k/(i*i));
        else ans-=mx*(k/(i*i));
    }
    cout<<ans;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*freopen("connect.in","r",stdin);
    freopen("connect.out","w",stdout);*/
    int t=1;
    //cin>>t;
    while(t--)
    {
        test_case();
    }
    return 0;
}
Copy
Number of the Beast skahl15
GNU G++17
155 ms
14.4 MB
Accepted