CodeForces 1030 D. Vasya and Triangle(简单几何)
D. Vasya and Triangle
Description:
Vasya has got three integers n, m and k. He’d like to find three integer points (x1,y1), (x2,y2), (x3,y3), such that 0≤x1,x2,x3≤n, 0≤y1,y2,y3≤m and the area of the triangle formed by these points is equal to knm.
Help Vasya! Find such points (if it’s possible). If there are multiple solutions, print any of them.
Input:
The single line contains three integers n, m, k ( 1≤n,m≤109, 2≤k≤109).
Output
If there are no such points, print “NO”.
Otherwise print “YES” in the first line. The next three lines should contain integers xi,yi — coordinates of the points, one point per line. If there are multiple solutions, print any of them.
You can print each letter in any case (upper or lower).
Sample Input:
4 3 3
Sample Output:
YES
1 0
2 3
4 1
Sample Input:
4 4 7
Sample Output:
NO
题目链接
在 x∈[0,n],y∈[0,m] 的范围内找出三个整数点使得所构成三角形面积为 knm
整数顶点构成的三角形可以用一个平行坐标轴的矩形严格包围,利用增补法计算三角形的面积
此图: S△AEF=S□ABCD−S△ADE−S△CEF−S△ABF
其中 S□ABCD 一定为整数,而其减去的三个三角形面积为整数或整数的一半,所以 S△AEF 一定等于 2x
对题目给出 knm 进行约分,根据上述性质 k 仅可能有 1、2 两种结果,其余情况均判定为无解
在有解的情况下所有合法面积均可用由两坐标轴构成的直角三角形组成(具体证明移步题解 CF1030D 【Vasya and Triangle】),这样三点的形式就可以确定下来 (0,0)、(x,0)、(0,y)
根据三角形面积关系可得等式
2xy=knm
化简得
xy=k2nm
这样根据等式关系可构造出 x、y 的值
令 x=2n,y=km 并约分得 x=gcd(2n,k)2n,y=gcd(2n,k)km
因为三角形顶点横纵坐标有范围限制,所以如果结果中 x>n
则可令 x=kn,y=2m 并约分得 x=gcd(2m,k)kn,y=gcd(2m,k)2m
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M;
ll K;
ll X, Y;
int main(int argc, char *argv[]) {
scanf("%lld%lld%lld", &N, &M, &K);
if (K / __gcd(N * M, K) > 2) {
printf("NO\n");
return 0;
}
printf("YES\n");
printf("0 0\n");
X = 2 * N / __gcd(2 * N, K);
Y = M / (K / __gcd(2 * N, K));
if (X > N) {
Y = 2 * M / __gcd(2 * M, K);
X = N / (K / __gcd(2 * M, K));
}
printf("%lld 0\n", X);
printf("0 %lld\n", Y);
return 0;
}