LightOJ 1077 - How Many Points? (线段整数点个数)

来源:互联网 发布:请叫我威廉三世知轩 编辑:程序博客网 时间:2024/05/16 08:32
1077 - How Many Points?
  PDF (English)StatisticsForum
Time Limit: 0.5 second(s)Memory Limit: 32 MB

Given two points A and B on the X-Yplane, output the number of the lattice points on the segmentAB. Notethat A and B are also lattice point. Those who are confused withthe definition of lattice point, lattice points are those points which havebothx and y co-ordinate as integer.

For example, for A (3, 3) and B (-1, -1) theoutput is5. The points are: (-1, -1), (0, 0), (1, 1), (2, 2) and (3,3).

Input

Input starts with an integer T (≤ 125),denoting the number of test cases.

Each case contains four integers, Ax, Ay,Bxand By. Each of them will be fit into a 32bit signed integer.

Output

For each test case, print the case number and the number oflattice points betweenAB.

Sample Input

Output for Sample Input

2

3 3 -1 -1

0 0 5 2

Case 1: 5

Case 2: 2

 




题意:给出一条线段的两个端点,求在这条线段上有多少个整数点

思路:水题,整数点个数为gcd(abs(x1-x2),abs(y1-y2))+1。。。



ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010100#define LL long long#define ll __int64#define INF 0x7fffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-10using namespace std;LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}int lcm(int a,int b){return a/gcd(a,b)*b;}LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headint main(){    int t,i,j;    LL x1,x2,y1,y2;    int cas=0;    scanf("%d",&t);    while(t--)    {    scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);    LL ans=gcd(abs(x1-x2),abs(y1-y2));    printf("Case %d: ",++cas);    printf("%lld\n",ans+1);    }    return 0;}


0 1
原创粉丝点击