#11075: 只有通過第 7 測資點(11%)...

#### vagrantlike (丫維)

School : No School
ID : 27614
2020-01-23 17:45:52
a810. 1. 倍數關係 -- | From: [163.29.253.105] | Post Date : 2016-06-20 16:56

Q.使用long long int 應該夠用了吧...錯在哪裡了呢？能否給個方向？謝謝～

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>

long long int gcd(long long int, long long int);

int main(int argc,char* argv[]) {

long long int a = 0,b = 0,x = 0,y = 0,gcd_of_x_and_y = 0,lcm_of_x_and_y = 0,ans = 0;
while(scanf("%I64d %I64d %I64d %I64d",&a,&b,&x,&y)==4) {

//須考慮x,y是否為0的各種情況
//另須特別考慮x為y的倍數或y為x的倍數之特別情況
if(x==0&&y!=0) {
//a<=0<=b
if(a*b<=0) {
//[a,b]範圍中包含0
ans = abs(floor(abs(a)/abs(y))+floor(abs(b)/abs(y)))+1;
//a*b>0
} else if(a*b>0 ) {

ans = abs(floor(abs(a)/abs(y))-floor(abs(b)/abs(y)));
//printf("ans = %I64d\n",ans);

//a<b<0
if((a<0)&&(b<0)) {
if(abs(b)%abs(y)==0) {
ans+=1;
//printf("ans = %I64d\n",ans);
//system("PAUSE");
}

//(a>0)&&(b>0)
} else {

if(abs(a)%abs(y)==0)
ans+=1;
}
}
} else if(x!=0&&y==0) {
//a<=0<=b
if(a*b<=0) {
//[a,b]範圍中包含0
ans = abs(floor(abs(a)/abs(x))+floor(abs(b)/abs(x)))+1;
//a*b>0
} else  if(a*b>0) {
ans = abs(floor(abs(a)/abs(x))-floor(abs(b)/abs(x)));

if((a<0)&&(b<0)) {
if(abs(b)%abs(x)==0)
ans+=1;

//(a>0)&&(b>0)
} else {

if(abs(a)%abs(x)==0)
ans+=1;
}
}

} else if(x==0&&y==0) {
if(a*b<=0) {
//[a,b]範圍中包含0
ans = 1;
//a*b>0
} else if(a*b>0 ) {
ans = 0;
}

//x!=0 && y!=0
} else {
//a<=0<=b
if(a*b<=0) {
if(x%y==0) {
ans = abs(floor(abs(a)/abs(y))+floor(abs(b)/abs(y)))+1;
} else if(y%x==0) {
ans = abs(floor(abs(a)/abs(x))+floor(abs(b)/abs(x)))+1;

//(x%y! =0)&&(y%x!=0)
} else {
//為方便起見 gcd_of_x_and_y及lcm_of_x_and_y全定義為正數

gcd_of_x_and_y = gcd(abs(x),abs(y));
lcm_of_x_and_y = abs(x*y)/gcd_of_x_and_y;

// printf("gcd_of_x_and_y = %I64d\n",gcd_of_x_and_y);
//printf("lcm_of_x_and_y = %I64d\n",lcm_of_x_and_y);
// system("PAUSE");

ans = (floor(abs(a)/abs(x))+floor(abs(b)/abs(x)))+(floor(abs(a)/abs(y))+floor(abs(b)/abs(y)))-(floor(abs(a)/abs(lcm_of_x_and_y))+floor(abs(b)/abs(lcm_of_x_and_y)))+1;

if(abs(a)%abs(x)==0)
ans+=1;
if(abs(a)%abs(y)==0)
ans+=1;
if(abs(a)%abs(lcm_of_x_and_y)==0)
ans-=1;
}
//a*b==0
}
//a*b>0
else if(a*b>0) {

//為方便起見 gcd_of_x_and_y及lcm_of_x_and_y全定義為正數
gcd_of_x_and_y = gcd(abs(x),abs(y));
lcm_of_x_and_y = abs(x*y)/gcd_of_x_and_y;

//printf("gcd_of_x_and_y = %I64d\n",gcd_of_x_and_y);
//printf("lcm_of_x_and_y = %I64d\n",lcm_of_x_and_y);

if((a<0)&&(b<0)) {

if(x%y==0) {
ans = abs(floor(abs(a)/abs(y))-floor(abs(b)/abs(y)));
if(abs(b)%abs(y)==0)
ans+=1;

} else if(y%x==0) {
ans = abs(floor(abs(a)/abs(x))-floor(abs(b)/abs(x)));
if(abs(b)%abs(x)==0)
ans+=1;

//(x%y!=0)&&(y%x!=0)
} else {
ans = (floor(abs(a)/abs(x))-floor(abs(b)/abs(x)))+(floor(abs(a)/abs(y))-floor(abs(b)/abs(y)))-(floor(abs(a)/abs(lcm_of_x_and_y))-floor(abs(b)/abs(lcm_of_x_and_y)))+1;
// printf("ans = %I64d\n",ans);

if(abs(b)%abs(x)==0)
ans+=1;
//printf("ans = %I64d\n",ans);
if(abs(b)%abs(y)==0)
ans+=1;
//printf("ans = %I64d\n",ans);
if(abs(b)%abs(lcm_of_x_and_y)==0)
ans-=1;
//printf("ans = %I64d\n",ans);
}
//a>0 && b>0
} else if((a>0)&&(b>0)) {

if(x%y==0) {
ans = abs(floor(abs(a)/abs(y))-floor(abs(b)/abs(y)));
if(abs(b)%abs(y)==0) {
ans+=1;
}
} else if(y%x==0) {
ans = abs(floor(abs(a)/abs(x))-floor(abs(b)/abs(x)));
if(abs(b)%abs(x)==0) {
ans+=1;
}
//(x%y!=0)&&(y%x!=0)
} else {
ans = (floor(abs(b)/abs(x))-floor(abs(a)/abs(x)))+(floor(abs(b)/abs(y))-floor(abs(a)/abs(y)))-(floor(abs(b)/abs(lcm_of_x_and_y))-floor(abs(a)/abs(lcm_of_x_and_y)))+1;

if(abs(a)%abs(x)==0)
ans+=1;
if(abs(a)%abs(y)==0)
ans+=1;
if(abs(a)%abs(lcm_of_x_and_y)==0)
ans-=1;
}
}
}
}

printf("%I64d\n",ans);
}

return 0;
}

long long int gcd(long long int p,long long int q) {
if(p%q==0) {
return q;
} else {
return gcd(q,p%q);
}
}

ZeroJudge Forum