Technocup 2019 - Elimination Round 1 D. Vasya and Triangle

地址:http://codeforces.com/contest/1030/problem/D

思路:因为三个点是整数点, S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2) = (n * m) / k;
如果2 * n * m / k是分数的话,那就不存在三个整数点,又因为k >= 2;所以必存在三个点,可以这样构造以原点作直角三角形,三点都在坐标轴上,找两个这样的数x * y = (2 * n * m / k),0 <= x <= n,0 <= y <= m;
注意:我n,m在比赛的时候用的int,到死都没想到爆了,要用long long。。。
我脑子笨,想到一个分解质因子找这两个数的方法,因为遍历找会超时,分解质因子,然后再dfs找这样的数;
886ms

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;

vector<LL>ve;
LL ptr;
LL n,m,k;
bool flag = false;

void dfs(int pos,int num,LL ans)
{
    LL x = ptr / ans;
    if(flag) return ;
    if(ans > n) return ;
    if(ans <= n && x > 0 && x <= m){
        flag = true;
        printf("YES\n");
        printf("0 0\n");
        printf("%lld 0\n",ans);
        printf("0 %lld\n",x);
        return ;
    }
    int len = ve.size();
    for(int i = pos + 1;i < len;++i)
    {
        dfs(i,num + 1,ans * ve[i]);
    }
}

int main()
{
    scanf("%lld %lld %lld",&n,&m,&k);
    LL ans = n * m * 2LL;
    //cout << ans << " " << ans % k << endl;
    if(ans % k == 0){
        LL x = ans / k;
        //cout << x << endl;
        ptr = x;
        for(LL i = 2; i * i <= x; i++)
        {
            if(x % i == 0)
            {
                while(x % i == 0) {
                    x /= i;
                    ve.push_back(i);
                }
            }
        }
        if(x > 1)
            ve.push_back(x);  //这个不可以缺少
        LL res = 1;
        int len = ve.size();
        //cout << len << endl;
        flag = false;
        dfs(-1,0,1LL);
        //其实这里写不写无所谓,因为永远都能找到
        if(!flag){
            printf("NO\n");
        }
    }else{
        printf("NO\n");
    }
    return 0;
}

有个31ms做法,利用n / gcd(n,k),这样就可以快速找到最大的x

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;

LL gcd(LL a,LL b)
{
    return (b == 0) ? a : gcd(b,a % b);
}

int main()
{
    LL n,m,k;
    cin >> n >> m >> k;
    LL ans = n * m * 2LL;
    if(ans % k != 0){
        printf("NO\n");
        return 0;
    }
    ans /= k;
    LL ptr = gcd(n,k);
    LL a = n / ptr;
    LL b = ans / a;
    if(b > m){
        b /= 2LL;
        a *= 2LL;
    }
    if(b > m){
        printf("NO\n");
    }
    printf("YES\n");
    printf("0 0\n%lld 0\n0 %lld\n",a,b);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值