[CCPC2020绵阳K] Knowledge is Power - 构造,找规律
Description
给你一个数字x,把x至少拆分为两个数的和,并且这些数字互质,问所有拆分方案中,在每个方案中最大值减去最小值的最小值是多少
Solution
设 (x=4y+r),对 (r) 讨论
如果 (r=0),拆成 (x/2+1,x/2-1)
如果 (r=1,3),拆成 ([x/2],[x/2]+1)
剩下的情况比较麻烦,但是经过一些尝试后,发现答案其实不可能超过 (4)
那么枚举几种拆分方案就可以
下面的代码中是暴力对拆成两个,拆成三个的方法进行尝试的
#include <bits/stdc++.h>
using namespace std;
#define int long long
int caseid = 0;
void solve()
{
++caseid;
int n;
cin >> n;
int r = n % 4;
cout << "Case #" << caseid << ": ";
if (n == 6)
{
cout << -1 << endl;
}
else if (r == 0)
{
cout << 2 << endl;
}
else if (r == 1 || r == 3)
{
cout << 1 << endl;
}
else
{
int m = n / 4;
int c = 2 * m / 3;
int ans = 1e18;
for (int i = n / 2 - 4; i <= n / 2; i++)
{
int j = n - i;
if (i <= 1 || j <= 1 || i % 2 == 0)
continue;
if (__gcd(i, j) == 1)
{
ans = min(ans, j - i);
}
}
for (int i = c - 10; i <= c + 10; i++)
{
int j = m - i;
if (i <= 0 || j < 0)
continue;
int a = 2 * i - 1;
int b = 2 * i + 1;
int c = n - a - b;
if (a <= 1)
continue;
if (c <= 1)
continue;
int tmp = abs(a - b);
tmp = max(tmp, abs(a - c));
tmp = max(tmp, abs(b - c));
if (tmp > ans)
continue;
if (__gcd(a, c) == 1 && __gcd(b, c) == 1)
ans = min(ans, tmp);
}
cout << ans << endl;
}
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
solve();
}