#8479: 第九組測資


inthewing (Hsu)

學校 : 國立中正大學
編號 : 29488
來源 : [39.15.2.129]
最後登入時間 :
2023-04-02 00:15:55

第九組始終過不了,有甚麼例外嗎? 

#8481: Re:第九組測資


silithus (希利蘇斯)

學校 : 澳門培道中學
編號 : 33314
來源 : [60.246.116.246]
最後登入時間 :
2023-09-19 17:00:10

第九組始終過不了,有甚麼例外嗎? 


把你的code貼出來看看?
#8492: Re:第九組測資


inthewing (Hsu)

學校 : 國立中正大學
編號 : 29488
來源 : [39.15.2.129]
最後登入時間 :
2023-04-02 00:15:55

第九組始終過不了,有甚麼例外嗎? 


把你的code貼出來看看?

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
//最大公因數 
long long max(long long x,long long y)
{
    long long tmp;
    if(y>x)
    {
        tmp=x;
        x=y;
        y=tmp;
    }
    while(1)
    {
        if(x%y==0)
        {
            tmp=y;
            break;
        }
        x%=y;
        if(y%x==0)
        {
            tmp=x;
            break;
        }
        y%=x;
    }
    return tmp;
}
int main(int argc, char *argv[])
{
    long long l,r,x,y,c,ans,ans1,ans2,tmp;
    while(cin>>l>>r>>x>>y)
    {
        ans=0;
        if(x<0)
            x=-x;
        if(y<0)
            y=-y;
        if(x==0&&y==0)
        {
            if(l<=0&&r>=0)
                ans++;
        }     
        else
        {
            c=x/max(x,y)*y;
            if(l<0&&r<=0)
            {
                tmp=-l;
                l=-r;
                r=tmp;
            }
            if(l==0)
            {
                ans=r/x+r/y-r/c+1;
            }
            else if(l>0)
            {
                ans1=r/x+r/y-r/c;
                ans2=l/x+l/y-l/c;
                if(l%x==0||l%y==0)
                {
                    ans1++;
                }
                ans=ans1-ans2;
            }
            else
            {
                ans1=r/x+r/y-r/c;
                l=-l;
                ans2=l/x+l/y-l/c;
                ans=ans1+ans2+1;
            }
        }  
        printf("%lld\n",ans);
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}

#8493: Re:第九組測資


inthewing (Hsu)

學校 : 國立中正大學
編號 : 29488
來源 : [39.15.2.129]
最後登入時間 :
2023-04-02 00:15:55

第九組始終過不了,有甚麼例外嗎? 


把你的code貼出來看看?

#include
#include
#include
using namespace std;
//最大公因數 
long long max(long long x,long long y)
{
    long long tmp;
    if(y>x)
    {
        tmp=x;
        x=y;
        y=tmp;
    }
    while(1)
    {
        if(x%y==0)
        {
            tmp=y;
            break;
        }
        x%=y;
        if(y%x==0)
        {
            tmp=x;
            break;
        }
        y%=x;
    }
    return tmp;
}
int main(int argc, char *argv[])
{
    long long l,r,x,y,c,ans,ans1,ans2,tmp;
    while(cin>>l>>r>>x>>y)
    {
        ans=0;
        if(x<0)
            x=-x;
        if(y<0)
            y=-y;
        if(x==0&&y==0)
        {
            if(l<=0&&r>=0)
                ans++;
        }     
        else
        {
            c=x/max(x,y)*y;
            if(l<0&&r<=0)
            {
                tmp=-l;
                l=-r;
                r=tmp;
            }
            if(l==0)
            {
                ans=r/x+r/y-r/c+1;
            }
            else if(l>0)
            {
                ans1=r/x+r/y-r/c;
                ans2=l/x+l/y-l/c;
                if(l%x==0||l%y==0)
                {
                    ans1++;
                }
                ans=ans1-ans2;
            }
            else
            {
                ans1=r/x+r/y-r/c;
                l=-l;
                ans2=l/x+l/y-l/c;
                ans=ans1+ans2+1;
            }
        }  
        printf("%lld\n",ans);
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}


只有最後一個一直過不了,實在找不到原因大哭
#8497: Re:第九組測資


silithus (希利蘇斯)

學校 : 澳門培道中學
編號 : 33314
來源 : [60.246.116.246]
最後登入時間 :
2023-09-19 17:00:10

第九組始終過不了,有甚麼例外嗎? 


把你的code貼出來看看?

#include
#include
#include
using namespace std;
//最大公因數 
long long max(long long x,long long y)
{
    long long tmp;
    if(y>x)
    {
        tmp=x;
        x=y;
        y=tmp;
    }
    while(1)
    {
        if(x%y==0)
        {
            tmp=y;
            break;
        }
        x%=y;
        if(y%x==0)
        {
            tmp=x;
            break;
        }
        y%=x;
    }
    return tmp;
}
int main(int argc, char *argv[])
{
    long long l,r,x,y,c,ans,ans1,ans2,tmp;
    while(cin>>l>>r>>x>>y)
    {
        ans=0;
        if(x<0)
            x=-x;
        if(y<0)
            y=-y;
        if(x==0&&y==0)
        {
            if(l<=0&&r>=0)
                ans++;
        }     
        else
        {
            c=x/max(x,y)*y;
            if(l<0&&r<=0)
            {
                tmp=-l;
                l=-r;
                r=tmp;
            }
            if(l==0)
            {
                ans=r/x+r/y-r/c+1;
            }
            else if(l>0)
            {
                ans1=r/x+r/y-r/c;
                ans2=l/x+l/y-l/c;
                if(l%x==0||l%y==0)
                {
                    ans1++;
                }
                ans=ans1-ans2;
            }
            else
            {
                ans1=r/x+r/y-r/c;
                l=-l;
                ans2=l/x+l/y-l/c;
                ans=ans1+ans2+1;
            }
        }  
        printf("%lld\n",ans);
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}


只有最後一個一直過不了,實在找不到原因大哭


c=x/max(x,y)*y; 這句有問題吧,c可能會超出long long範圍,如x=10000000000001、y=30000000000007時,c的值會變成-7613192870346211321

我的AC代碼:https://sites.google.com/site/silithusxoi/code/ks2013,希望能幫到你。 

#8503: Re:第九組測資


inthewing (Hsu)

學校 : 國立中正大學
編號 : 29488
來源 : [39.15.2.129]
最後登入時間 :
2023-04-02 00:15:55

第九組始終過不了,有甚麼例外嗎? 


把你的code貼出來看看?

#include
#include
#include
using namespace std;
//最大公因數 
long long max(long long x,long long y)
{
    long long tmp;
    if(y>x)
    {
        tmp=x;
        x=y;
        y=tmp;
    }
    while(1)
    {
        if(x%y==0)
        {
            tmp=y;
            break;
        }
        x%=y;
        if(y%x==0)
        {
            tmp=x;
            break;
        }
        y%=x;
    }
    return tmp;
}
int main(int argc, char *argv[])
{
    long long l,r,x,y,c,ans,ans1,ans2,tmp;
    while(cin>>l>>r>>x>>y)
    {
        ans=0;
        if(x<0)
            x=-x;
        if(y<0)
            y=-y;
        if(x==0&&y==0)
        {
            if(l<=0&&r>=0)
                ans++;
        }     
        else
        {
            c=x/max(x,y)*y;
            if(l<0&&r<=0)
            {
                tmp=-l;
                l=-r;
                r=tmp;
            }
            if(l==0)
            {
                ans=r/x+r/y-r/c+1;
            }
            else if(l>0)
            {
                ans1=r/x+r/y-r/c;
                ans2=l/x+l/y-l/c;
                if(l%x==0||l%y==0)
                {
                    ans1++;
                }
                ans=ans1-ans2;
            }
            else
            {
                ans1=r/x+r/y-r/c;
                l=-l;
                ans2=l/x+l/y-l/c;
                ans=ans1+ans2+1;
            }
        }  
        printf("%lld\n",ans);
    }
    //system("PAUSE");
    return EXIT_SUCCESS;
}


只有最後一個一直過不了,實在找不到原因大哭


c=x/max(x,y)*y; 這句有問題吧,c可能會超出long long範圍,如x=10000000000001、y=30000000000007時,c的值會變成-7613192870346211321

我的AC代碼:https://sites.google.com/site/silithusxoi/code/ks2013,希望能幫到你。 

感謝~前幾天已經測試通過了~我一直以為(a/最大公因數)已經會幫我處理掉超過範圍的問題了,壓根都沒想起互質得狀況..