• Countable Rational Numbers


     

     

    #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
    ");
                continue;
            }
            if (x<=0||x*y==0||gcd(x,abs(y))!=1)
            {
                printf("0
    ");
                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
    ",ans+1);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Mysql练习#1-建表
    Mysql学习笔记#7-范式
    Git学习笔记#9-标签
    Git学习笔记#8-操作冲突
    Git学习笔记#7-分支操作
    Git学习笔记#6-远程仓库(GitHub)
    Git学习笔记#5-文件删除
    Git学习笔记#4-版本回退
    Git学习笔记#3-修改文件与撤销
    免费分享老男孩全栈9期视频,共126天
  • 原文地址:https://www.cnblogs.com/Accpted/p/11572453.html
Copyright © 2020-2023  润新知