D. Vasya and Triangle(数学思维)

博客围绕Vasya的三角形问题展开,给定整数n、m、k,需找到满足条件的三个整数点构成面积为nm/k的三角形。通过平移旋转使一个顶点为原点,利用叉乘求面积,分析得出若2nm%k≠0无解,其他情况有解,并给出具体求解思路及分解乘积的方法。

en .感觉学弟都过了。我也没想出来。只想到了开始,没有想到怎么凑,感觉挺可惜的。

http://codeforces.com/problemset/problem/1058/D

D. Vasya and Triangle

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nmk

.

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

Copy

4 3 3

Output

Copy

YES
1 0
2 3
4 1

Input

Copy

4 4 7

Output

Copy

NO

Note

In the first example area of the triangle should be equal to nmk=4

. The triangle mentioned in the output is pictured below:

In the second example there is no triangle with area nmk=167

思路

显然,不论最终找到的三角形处于什么形态,我们都可以通过平移旋转将其一个顶点设置为原点,且满足三角形位于矩形内部。

我们设这三点分别为 A(0,0),B(x1,y1),C(x2,y2),由此得到两条向量,根据叉乘求得三角形面积为 |x1y2−x2y1|2。

于是我们只需要找到合适的两个点使得 |x1y2−x2y1|=2nmk即可。

左端必为整数,因此假若 2nm%k≠0

,则无解,对于其他情况必有解存在。

我们假设 B,C两点都位于坐标轴,即 x2=y1=0,|x1y2−x2y1|=x1y2,相当于我们只需将 2nmk分解为两个满足题意的整数乘积即可。

因为 2nm%k=0,则 2nm 必包含 k 所有的素因子,求得 u=gcd(2n,k)

若 u=1,显然 m 可以整除 k,令 x1=n,y2=2×m/k即可;

否则令 x1=2×n/u,y2=m×u/k即可。

代码:

#include <bits/stdc++.h>
#define IO                       \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;

LL n, m, k;

bool solve() {
    if (2LL * n * m % k != 0)
        return false;
    cout << "YES" << endl;
    LL a, b, gc = __gcd(2LL * n, k);
    if (gc == 1)
        b = 2LL * m / k, a = n;
    else
        a = 2LL * n / gc, b = m * gc / k;
    cout << "0 0" << endl;
    cout << 0 << " " << b << endl;
    cout << a << " " << 0 << endl;
    return true;
}

int main() {
#ifdef LOCAL_IM0QIANQIAN
    freopen("test.in", "r", stdin);
//    freopen("test.out", "w", stdout);
#else
    IO;
#endif // LOCAL_IM0QIANQIAN

    cin >> n >> m >> k;
    if (!solve()) {
        cout << "NO" << endl;
    }
    return 0;
}

 

### 关于 Vasya 和多重集的编程竞赛算法题目解决方案 #### 题目描述 给定一个多重集合 `s`,目标是将其分割成两个新的多重集合 `a` 和 `b` (其中一个可以为空),使得这两个新集合中的“好数”的数量相等。“好数”定义为在一个特定多集中恰好只出现一次的数字。 为了实现这一目标,需要考虑如何有效地统计并分配这些元素到不同的子集中去[^1]。 #### 解决思路 一种有效的解决方法是从输入数据的特点出发思考。如果某个数值在整个原始集合中出现了偶数次,则该值可以在不影响最终结果的情况下被平均分入两个子集中;而对于那些仅出现奇数次数的情况,则必须小心处理以确保能够达成平衡条件——即让尽可能多的不同类型的单例项分别进入各自的目标组内[^2]。 具体来说: - 对于任何频率大于等于两次(无论是奇还是偶)的数据点而言,总是能通过适当划分来满足上述要求; - 当遇到频度为一的情形时,就需要额外注意了:因为这直接影响着能否成功创建具有相同数目唯一成员的新分区。 因此,在实际编码过程中应该优先处理那些重复率较高的项目,并记录下所有独一无二实例的位置以便后续操作使用。 #### Python 实现代码示例 下面是一个基于此逻辑编写的Python函数,用于求解这个问题: ```python from collections import Counter def can_split_equally(s): count = Counter(s) # 统计各元素出现次数 singletons = sum(1 for v in count.values() if v == 1) return singletons % 2 == 0 # 测试用例 test_cases = [ [1, 2, 2, 3], # True 可以分成 {1} 和 {2, 2, 3} [1, 2, 3, 4, 5], # False 单独存在的数字有五个无法平分 ] for case in test_cases: print(f"Input: {case}, Can Split Equally? :{can_split_equally(case)}") ``` 这个程序首先利用 `collections.Counter` 来计算每个整数在列表里边出现过的总次数。接着它会遍历所有的键值对,累积起所有只出现过一次(也就是所谓的 “好数” 或者说是单一实例)的数量。最后一步就是判断这样的特殊案例是不是构成了一个偶数序列长度 —— 如果是的话就意味着存在至少一组可行解;反之则不存在这样的一分为二方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值