# bzoj5027 数学题

http://www.elijahqi.win/archives/1323
Description

Input

a,b,c,x1,x2,y1,y2的绝对值不超过10^8。
Output

Sample Input
1 1 -3 0 4 0 4
Sample Output
4

2017.10.22update 被leoly hack一波 我忽略了a==0&&b==0&&c!=0的情况 这种情况应该也是无解直接输出0

#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
inline int exgcd(int a,int b,long long &x,long long &y){
if (b==0){x=1;y=0;return a;}
int tmp=exgcd(b,a%b,x,y);
long long t=x;x=y;y=t-a/b*y;return tmp;
}
int main(){
//freopen("bzoj5027.in","r",stdin);
int a,b,c,x1,y1,x2,y2;long long x0=0,y0=0;
scanf("%d%d%d%d%d%d%d",&a,&b,&c,&x1,&x2,&y1,&y2);c=-c;if (!a&&!b&&!c){
long long ans=(long long)(x2-x1+1)*(y2-y1+1);printf("%lld",ans);return 0;
}if(!a&&!b&&c){printf("0");return 0;}
if (a==0){
int tmp=c/b;
if (tmp*b==c&&(tmp>=y1)&&(tmp<=y2)) printf("%d",x2-x1+1);else printf("0");return 0;
}
if (b==0){
int tmp=c/a;
if (tmp*a==c&&(tmp>=x1)&&(tmp<=x2)) printf("%d",y2-y1+1);else printf("0");return 0;
}
int gg=exgcd(a,b,x0,y0);int step1=0,step2=0,step3=0,step4=0;//printf("%lld %lld\n",x0,y0);
x0=x0*c/gg;y0=y0*c/gg;//step1: x0->近端点 step2=x0->远端点    //step3:y0->近端点 step4:y0->远端点;
int dx=b/gg,dy=-a/gg;if (c%gg) return printf("0"),0;
if (dx>0) step1=ceil((double)(x1-x0)/dx),step2=floor((double) (x2-x0)/dx);
if (dx<0) step1=ceil((double)(x2-x0)/dx),step2=floor((double) (x1-x0)/dx);
if (dy>0) step3=ceil((double)(y1-y0)/dy),step4=floor((double) (y2-y0)/dy);
if (dy<0) step3=ceil((double)(y2-y0)/dy),step4=floor((double) (y1-y0)/dy);
long long max1=min(step2,step4),min1=max(step1,step3);
if (min1>max1) return printf("0"),0;
printf("%lld",max1-min1+1);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120