#38682: 測資#8無法通過，不知道是漏了哪個項目

#### alanwang0521@gmail.com (王廷祐)

School : No School
ID : 258559
2023-12-15 22:51:28
a810. 1. 倍數關係 -- | From: [140.113.90.227] | Post Date : 2023-12-16 13:43

#include<bits/stdc++.h>//萬用標頭檔
using namespace std;

int main(){
long long int a,b,c,d;
cin>>a>>b>>c>>d;
c=abs(c);
d=abs(d);
if(c>d){
long long int temp=c;
c=d;
d=temp;
}
if(c==0&&d==0){
if((a>0&&b>0)||(a<0&&b<0))
cout<<0;
else cout<<1;
}
if(c==0&&d!=0){
if(a>0&&b>0){
long long int y;
if(a%d==0) y= b/d - a/d +1;
else y= b/d - a/d;
cout<<y;
}
if(a<=0&&b>=0){
long long int y;
y= b/d - a/d +1;
cout<<y;
}
if(a<0&&b<0){
long long int y;
if(b%d==0) y= b/d - a/d +1;
else y= b/d - a/d;
cout<<y;
}
}
if(c!=0&&d!=0){
if(a>=0&&b>0){
long long int x,y,z,LCM;
if(a%c==0) x= b/c - a/c +1;
else x= b/c - a/c;
if(a%d==0) y= b/d - a/d +1;
else y= b/d - a/d;
LCM=c*d/__gcd(c,d);
if(a%LCM==0) z= b/LCM - a/LCM +1;
else z= b/LCM - a/LCM;
cout<<x+y-z;
}
if(a<0&&b>=0){
long long int x,y,z,LCM;
x= b/c - a/c +1;
y= b/d - a/d +1;
LCM=c*d/__gcd(c,d);
z= b/LCM - a/LCM +1;
cout<<x+y-z;
}
if(a<0&&b<0){
long long int x,y,z,LCM;
if(b%c==0) x= b/c - a/c +1;
else x= b/c - a/c;
if(b%d==0) y= b/d - a/d +1;
else y= b/d - a/d;
LCM=c*d/__gcd(c,d);
if(b%LCM==0) z= b/LCM - a/LCM +1;
else z= b/LCM - a/LCM;
cout<<x+y-z;
}
}
}

ZeroJudge Forum