标签:
Time Limit: 2 second(s) | Memory Limit: 32 MB |
You have to find the number of solutions of the following equation:
Ax + By + C = 0
Where A, B, C, x, y are integers and x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2.
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing seven integers A, B, C, x1, x2, y1, y2 (x1 ≤ x2, y1 ≤ y2). The value of each integer will lie in the range [-108, 108].
For each case, print the case number and the total number of solutions.
Sample Input |
Output for Sample Input |
5 1 1 -5 -5 10 2 4 -10 -8 80 -100 100 -90 90 2 3 -4 1 7 0 8 -2 -3 6 -2 5 -10 5 1 8 -32 0 0 1 10 |
Case 1: 3 Case 2: 37 Case 3: 1 Case 4: 2 Case 5: 1 |
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<queue> 6 #include<stack> 7 #include<set> 8 #include<math.h> 9 using namespace std; 10 typedef long long LL; 11 LL gcd(LL n,LL m) 12 { 13 if(m==0) 14 { 15 return n; 16 } 17 else if(n%m==0) 18 { 19 return m; 20 } 21 else return gcd(m,n%m); 22 } 23 pair<LL,LL>P(LL n,LL m) 24 { 25 if(m==0) 26 { 27 pair<LL,LL>ask=make_pair(1,0); 28 return ask; 29 } 30 else 31 { 32 pair<LL,LL>an=P(m,n%m); 33 LL x=an.second; 34 LL y=an.first; 35 y-=(n/m)*x; 36 an.first=x; 37 an.second=y; 38 return an; 39 } 40 } 41 int main(void) 42 { 43 LL i,j,k; 44 scanf("%lld",&k); 45 LL s; 46 LL A,B,C,x1,x2,y1,y2; 47 for(s=1; s<=k; s++) 48 { 49 LL sum=0; 50 scanf("%lld %lld %lld %lld %lld %lld %lld",&A,&B,&C,&x1,&x2,&y1,&y2); 51 C=-C; 52 if(A==0&&B==0&&C!=0) 53 sum=0; 54 else if(A==0&&B==0&&C==0) 55 { 56 sum=(LL)(x2-x1+1)*(LL)(y2-y1+1); 57 } 58 else if(A==0) 59 { 60 if(C%B) 61 { 62 sum=0; 63 } 64 else 65 { 66 LL t=(C/B); 67 if(t>=y1&&t<=y2) 68 sum=(x2-x1+1); 69 else sum=0; 70 } 71 } 72 else if(B==0) 73 { 74 if(C%A) 75 { 76 sum=0; 77 } 78 else 79 { 80 LL t=(C/A); 81 if(t>=x1&&t<=x2) 82 sum=(y2-y1+1); 83 else sum=0; 84 } 85 } 86 else 87 { if(A<0){C=-C;A=-A;B=-B;} 88 LL gc=gcd(abs(A),abs(B)); 89 if(C%gc) 90 { 91 sum=0; 92 } 93 else if((LL)A*(LL)B>0) 94 { 95 A/=gc; 96 B/=gc; 97 C/=gc; 98 pair<LL,LL>ask=P((A),(B)); 99 LL x=(LL)ask.first; 100 LL y=(LL)ask.second; 101 x*=C; 102 y*=C; 103 LL l=-1e9; 104 LL r=1e9; 105 LL id=1e9; 106 while(l<=r) 107 { 108 LL mid=(l+r)/2; 109 if(x+mid*B>=x1) 110 { 111 id=mid; 112 r=mid-1; 113 } 114 else l=mid+1; 115 } 116 l=-1e9; 117 r=1e9; 118 LL ic=1e9; 119 while(l<=r) 120 { 121 LL mid=(l+r)/2; 122 if(x+mid*B<=x2) 123 { 124 ic=mid; 125 l=mid+1; 126 } 127 else r=mid-1; 128 } 129 if(id>ic) 130 { 131 sum=0; 132 } 133 else if(id==ic) 134 { 135 LL xx=x+id*B; 136 if(xx>=x1&&xx<=x2) 137 { 138 LL yy=y-id*A; 139 if(yy>=y1&&yy<=y2) 140 { 141 sum=1; 142 } 143 } 144 else sum=0; 145 } 146 else 147 { 148 l=-1e9; 149 r=1e9; 150 LL ip=1e9,iq=1e9; 151 while(l<=r) 152 { 153 LL mid=(l+r)/2; 154 if(y-mid*A>=y1) 155 { 156 ip=mid; 157 l=mid+1; 158 } 159 else r=mid-1; 160 } 161 l=-1e9; 162 r=1e9; 163 while(l<=r) 164 { 165 LL mid=(l+r)/2; 166 if(y-mid*A<=y2) 167 { 168 iq=mid; 169 r=mid-1; 170 } 171 else l=mid+1; 172 } 173 if(ip<iq) 174 { 175 sum=0; 176 } 177 else 178 { 179 180 if(ic<iq||id>ip) 181 { 182 sum=0; 183 } 184 else 185 { 186 if(id<=iq&&ic>=ip) 187 { 188 sum=ip-iq+1; 189 } 190 else if(iq<=id&&ip>=ic) 191 { 192 sum=ic-id+1; 193 } 194 else if(iq>=id&&iq<=ic) 195 { 196 sum=ic-iq+1; 197 } 198 else if(id>=iq&&id<=ip) 199 { 200 sum=ip-id+1; 201 202 } 203 } 204 } 205 } 206 } 207 else 208 { 209 A/=gc; 210 B/=gc; 211 C/=gc; 212 pair<LL,LL>ask=P(abs(A),abs(B)); 213 LL x=(LL)ask.first; 214 LL y=(LL)ask.second; 215 y=-y; 216 x*=C; 217 y*=C; 218 LL l=-1e9; 219 LL r=1e9; 220 LL id=1e9; 221 while(l<=r) 222 { 223 LL mid=(l+r)/2; 224 if(x+mid*abs(B)>=x1) 225 { 226 id=mid; 227 r=mid-1; 228 } 229 else l=mid+1; 230 } 231 l=-1e9; 232 r=1e9; 233 LL ic=1e9; 234 while(l<=r) 235 { 236 LL mid=(l+r)/2; 237 if(x+mid*abs(B)<=x2) 238 { 239 ic=mid; 240 l=mid+1; 241 } 242 else r=mid-1; 243 } 244 if(id>ic) 245 { 246 sum=0; 247 } 248 else if(id==ic) 249 { 250 LL xx=x+id*abs(B); 251 if(xx>=x1&&xx<=x2) 252 { 253 LL yy=y+id*A; 254 if(yy>=y1&&yy<=y2) 255 { 256 sum=1; 257 } 258 } 259 else sum=0; 260 } 261 else 262 { 263 l=-1e9; 264 r=1e9; 265 LL ip=1e9,iq=1e9; 266 while(l<=r) 267 { 268 LL mid=(l+r)/2; 269 if(y+mid*A>=y1) 270 { 271 iq=mid; 272 r=mid-1; 273 } 274 else l=mid+1; 275 } 276 l=-1e9; 277 r=1e9; 278 while(l<=r) 279 { 280 LL mid=(l+r)/2; 281 if(y+mid*A<=y2) 282 { 283 ip=mid; 284 l=mid+1; 285 } 286 else r=mid-1; 287 } 288 if(ip<iq) 289 { 290 sum=0; 291 } 292 else 293 { 294 295 if(ic<iq||id>ip) 296 { 297 sum=0; 298 } 299 else 300 { 301 if(id<=iq&&ic>=ip) 302 { 303 sum=ip-iq+1; 304 } 305 else if(iq<=id&&ip>=ic) 306 { 307 sum=ic-id+1; 308 } 309 else if(iq>=id&&iq<=ic) 310 { 311 sum=ic-iq+1; 312 } 313 else if(id>=iq&&id<=ip) 314 { 315 sum=ip-id+1; 316 317 } 318 } 319 }//printf("%lld %lld %lld %lld\n",ip,iq,id,ic); 320 } 321 } }printf("Case %lld: %lld\n",s,sum); 322 323 } 324 return 0; 325 }
1306 - Solutions to an Equation
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5583123.html