Vasya and Triangle[CF1030D]
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 nm/k.
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).
Examples
input
4 3 3
output
YES
1 0
2 3
4 1
input
4 4 7
output
NO
题意:
在坐标轴上能不能找到三个点围成的三角形面积为n*m/k。其中0≤x1,x2,x3≤n, 0≤y1,y2,y3≤m,x1,x2,x3,y1,y2,y3∈N。
分析:
由于S=n*m/k,所以面积一定为有理数。又S=d*h/2,所以d*h=2*n*m/k,由于三个点的横纵坐标非负,所以很容易判断当2*n*m%k==0时存在这样的三角形。
我们可以设三点分别为(0,0),(x,0),(0,y)。那么可以得到x*y=2*n*m/k,因为2*n*m%k==0,所以2*n*m的一个因子为k。
我们令a=gcd(2*n,k):
如果a==1,则说明m%k==0,所以x=n;y=2*m/k;
如果a!=1,则说明2*n与k有共同的因子u,所以x=2*n/a;y=2*n*m/k/x;
代码:
#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
long long n,m,k;
cin>>n>>m>>k;
if(2*n*m%k!=0)
cout<<"NO"<<endl;
else
{
int a,x,y;
a=__gcd(2*n,k);
if(a==1)
{
x=n;
y=2*m/k;
}
else
{
x=2*n/a;
y=2*n*m/k/x;
}
cout<<"YES"<<endl<<"0 0"<<endl<<x<<" 0"<<endl<<"0 "<<y<<endl;
}
return 0;
}