codeforces 1030D Vasya and Triangle【思维+gcd】
题目:戳这里
题意:选出三个点构成三角形,要求面积为n*m/k。
解题思路:因为三个点的坐标都是正整数,根据三角形面积公式(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2=n*m/k可知,若三角形存在,则2*n*m/k必为整数。若面积*2为整数,则把该三角形放置在x轴上即可。于是设x1=0,y1=0,x2=a,y2=0,x3=0,y3=b;求a*b=2*n*m/k。(0<=a<=n,0<=b<=m)
欲找到满足条件,可以利用最大公约数。若gcd(2*n,k)==1,则gcd(2*m,k)>=2; b=2*m/gcd(2*m,k) <=m,a=n/(k/gcd(2*m,k)<=n,反之若gcd(2*n,k)>=2同理。
附ac代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int inf = 0x3f3f3f3f; 5 const int maxn = 2e3 + 10; 6 #define lowbit(x) x&-x 7 ll gcd(ll a, ll b) { 8 return b?gcd(b,a%b):a; 9 } 10 int main() { 11 ll n, m, k; 12 scanf("%lld %lld %lld", &n, &m, &k); 13 if(2ll * n * m %k != 0) { 14 puts("NO"); 15 return 0; 16 } 17 puts("YES"); 18 puts("0 0"); 19 ll x2, y2, x3, y3; 20 ll sum = 2ll * n * m / k; 21 // printf("%lld\n", gcd(2ll*n,k)); 22 if(gcd(2ll*n,k) == 1) { 23 ll u = gcd(2ll*m, k); 24 printf("%lld %lld\n%lld %lld", n/(k/u), 0ll, 0ll, 2ll*m/u); 25 } else { 26 ll u = gcd(2ll*n, k); 27 printf("%lld %lld\n%lld %lld", 2ll*n/u, 0ll, 0ll, m/(k/u)); 28 } 29 return 0; 30 }