Codechef ACM-ICPC Asia-Kanpur Site First Round Online Contest 2013 B
呵呵好么!
Line ProblemProblem code: LINEPROB |
A Sniper is standing at the point (x1, y1) on the 2D XY Plane. He shoots from his position towards the point (x2, y2). You may assume that all points are integers.
Consider the 2D grid formed by integer points on the XY Plane. The position of the Sniper and the Target are lattice points in this grid. The bullet shot by the Sniper will follow a straight line trajectory from (x1, y1) to (x2, y2). The bullet goes no further than (x2, y2).
Consider the trajectory of the bullet when the Sniper is standing at (1, 1) and the Target lies at (4, 3).

Notice how the trajectory of the bullet touches 4 cells. A cell is considered touched by the trajectory if and only if the bullet will enter the cell. How many cells are touched by the trajectory of the bullet?

Input
The first line contains a single integer T, the number of test cases. Each of the following T lines contain one test case each. Each test case contains 4 integers x1, y1, x2 and y2. The integers are separated by single space characters.
Output
For each test case, output a single line, containing the number of cells touched by the trajectory of the bullet shot from (x1, y1) to (x2, y2). Remember that a cell is considered touched by the trejectory if and only if the bullet enters the cell - only touching a side is not enough.
Constraints
0 < T < 10100 0 ≤ x1, y1, x2, y2 ≤ 1000000000
Sample Input
3 0 0 3 2 0 0 2 2 0 0 1 0
Sample Output
4 2 0
Explanation
In the second test case, the trajectory of the bullet touches the point (1, 1). The bullet does not enter the cells with bottom left corners at (0,1) and (1,0). It directly enters the cell with the bottom left corner at (1,1). Hence, we count only two cells as touched during its trajectory.
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <vector> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm> 10 using namespace std; 11 #define maxn 101 12 #define mod 1000000007 13 #define INF 0x7fffffff 14 #define ll long long 15 //#define ll __int64 16 int n, m; 17 int a[maxn]; 18 int gcd(int n, int m){ 19 if (m == 0)return n; 20 return gcd(m, n%m); 21 } 22 int main(){ 23 int cas = 1; 24 int t; 25 scanf("%d", &t); 26 while (t--){ 27 int x, y, x1, y1, x2, y2; 28 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 29 x = abs(x2 - x1); 30 y = abs(y2 - y1); 31 int ans; 32 ans = x / gcd(x, y) + y / gcd(x, y); 33 if (x == 0 || y == 0)ans = 1; 34 printf("%d\n", (ans-1)*gcd(x,y)); 35 } 36 return 0; 37 }
浙公网安备 33010602011771号