当前位置 : 主页 > 编程语言 > c++ >

Countable Rational Numbers

来源:互联网 收集:自由互联 发布时间:2021-06-23
#pragma GCC optimize(3,"Ofast","inline")#include bits/stdc++.h using namespace std;typedef long long ll;const ll maxn=1000005;int prime[maxn/10],phi[maxn];bool vis[maxn];int tot; inline int gcd(int a,int b){ return b==0?a:gcd(b,a%b);} void

 

 

 

 

 

 

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
const ll maxn=1000005;
int prime[maxn/10],phi[maxn];
bool vis[maxn];
int tot;
 
inline int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
 
void init()
{
    phi[1]=1;
    for (ll i=2; i<maxn; ++i)
    {
        if (!vis[i])
        {
            prime[tot++]=i;
            phi[i]=i-1;
        }
        for (ll j=0; j<tot&&1ll*prime[j]*i<maxn; j++)
        {
            vis[prime[j]*i]=1;
            if(i%prime[j]==0)
            {
                phi[prime[j]*i]=phi[i]*prime[j];
                break;
            }
            phi[prime[j]*i]=phi[i]*phi[prime[j]];
        }
    }
}
 
inline bool check(ll xx,ll yy,ll x,ll y)
{
    if(max(abs(xx),abs(yy))!=max(abs(x),abs(y)))
    {
        return max(abs(xx),abs(yy))<max(abs(x),abs(y));
    }
    if(yy!=y)
    {
        return yy<y;
    }
    return xx*yy>x*y;
}
 
int main()
{
    init();
    ll _;
    scanf("%lld",&_);
    ll x,y;
    ll k;
    ll ans=0;
    while (_--)
    {
        ll xx,yy;
        ans=0;
        scanf("%lld%lld",&y,&x);
        if(y==0&&x==1){
            printf("2\n");
            continue;
        }
        if (x<=0||x*y==0||gcd(x,abs(y))!=1)
        {
            printf("0\n");
            continue;
        }
        if(max(x,abs(y))<=2)
        {
            if(x==2&&y==1)
            {
                puts("6");
            }
            else if(x==1&& y==2)
            {
                puts("7");
            }
            else if(x==1 &&y==1)
            {
                puts("3");
            }
            else if(x==1&&y==0)
            {
                puts("2");
            }
            else if(x==1&&y==-1)
            {
                puts("1");
            }
            else if(x==2&& y==-1)
            {
                puts("5");
            }
            else if(x==1&&y==-2)
            {
                puts("4");
            }
            continue;
        }
 
        k=max(x,abs(y));
        if (k>=3)
        {
            ans=3;
            for(ll i=2; i<=k-1; ++i)
                ans+=4*phi[i];
        }
        for (ll i=1; i<=k; ++i)
        {
            if (i>1&&k%i==0) continue;
            xx=k;
            yy=i;
            if (gcd(xx,yy)==1&&check(xx,yy,x,y))
                ans++;
            xx=k;
            yy=-i;
            if (gcd(xx,abs(yy))==1&&check(xx,yy,x,y))
                ans++;
 
            yy=k;
            xx=i;
            if (gcd(xx,yy)==1&&check(xx,yy,x,y))
                ans++;
 
            yy=-k;
            xx=i;
            if (gcd(xx,abs(yy))==1&&check(xx,yy,x,y))
                ans++;
        }
        printf("%lld\n",ans+1);
    }
    return 0;
}
网友评论