跳到正文

题解

p2660: 一种简单的数学证明

raywxiong约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年8月5日
收录
2026年8月28日
更新
2026年11月11日
篇幅
约 1 分钟
题目
P2660

版本

  1. v1当前

题解区那个证明太繁琐了,这里提供一个简单的:

C++
令x ≤ y,记y = r0, x = r1,第一次辗转相除的余数为r3,第二次为r4,余数为0时记其前一个余数为rn,则:
r0 = r1·q1 + r2
r1 = r2·q2 + r3
...
rn-2 = rn-1·qn-1 + rn
rn-1 = rn·qn (此次余数为0)
所有等式相加相消可得:r0 + r1 - rn = r1·q1 + r2·q2 + ... + rn·qn
上式右边乘4即为完整的所求周长和,于是所求 <=> 4·(r0 + r1 - rn)

代码如下:

C++
#include <bits/stdc++.h>
using namespace std;

using ULL = unsigned long long;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    ULL x, y;
    cin >> x >> y;
    // if (x > y)
    //     swap(x, y); //代码不用swap,__gcd支持任意顺序
    cout << 4 * (y + x - __gcd(x, y)) << '\n';
    return 0;
}
原文