Jun
24
SGU 106 解题手记
2007 解题报告 Popularity: 1%
SGU 106 解题手记
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <cmath> using namespace std; int extended_euclid(int a,int b,int &x,int &y) { if (b) { int r=extended_euclid(b,a%b,x,y); int t=y; y=x-(a/b)*y; x=t; return r; } else { x=1; y=0; return a; } } int main() { int oa,ob,a,b,c,x1,x2,y1,y2,x0,y0; cin>>a>>b>>c>>x1>>x2>>y1>>y2; c=-c; oa=a; ob=b; if (x1>x2 || y1>y2) { cout<<0; return 0; } if (a==0 && b==0) { if (c==0) cout<<static_cast<long long>(x2-x1+1)*(y2-y1+1); else cout<<0; return 0; } if (a==0) { if (c%b==0 && y1<=c/b && c/b<=y2) cout<<1; else cout<<0; return 0; } if (b==0) { if (c%a==0 && x1<=c/a && c/a<=x2) cout<<1; else cout<<0; return 0; } int transx=(a>0?1:-1),transy=(b>0?1:-1); if (c<0) { transx=-transx; transy=-transy; } a=abs(a); b=abs(b); c=abs(c); int d=extended_euclid(a,b,x0,y0); if (c%d) { cout<<0; return 0; } x0*=c/d; y0=(c-static_cast<long long>(a)*x0)/b; x0*=transx; y0*=transy; a=oa/d; b=ob/d; if (a<0) swap(y1,y2); if (b<0) swap(x1,x2); int mini=max(ceil(static_cast<double>(x1-x0)/b),ceil(static_cast<double>(y0-y2)/a)); int maxi=min(floor(static_cast<double>(x2-x0)/b),floor(static_cast<double>(y0-y1)/a)); if (maxi>=mini) cout<<maxi-mini+1; else cout<<0; return 0; } |
- http://d.ream.at/sgu-106/
- Tags:SGU, 不定方程, 数学
- (6)comments | leave a reply
- Trackback URI
October 9th, 2008 at 17:13 pm
extended_euclid 原来还不能应付a,b为负数的情况阿,才知道。。
Reply
October 5th, 2008 at 0:59 am
x0*=c/d; y0=(c-static_cast(a)*x0)/b;
为什么y0也写成 y0*=c/d 会挂掉第11个CASE .
Reply
June 22nd, 2008 at 18:16 pm
你说你写错了,
那为何能过?
我看amber和周源的程序也都是1
Reply
June 23rd, 2008 at 12:44 pm
测试数据没有这种情况,所以怎么写都能过。
Reply
June 22nd, 2008 at 10:50 am
如果只有a=0,
且b满足条件,我觉得是x2-x1+1组解,
为什么你程序里写得是1呢
Reply
June 22nd, 2008 at 17:15 pm
我弄错了。
Reply