NOIP 2026 解题报告
a_small_OIer · · 个人记录
和弦 / chord
题目链接:P18115 [NOIP2026] 和弦 / chord
题意
给定正整数
思路
观察特殊性质,发现这题肯定是和
钦定
这里发现最优的
然后就是经典的枚举因数然后钦定其为
枚举
分子上
令
枚举
需要注意的一点是分母上不能直接带
复杂度
实现
提交记录:https://www.luogu.com.cn/record/312877844。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
//十年OI一场空,不开long long见祖宗
int c , t , a , b;
inline void solve(){
scanf("%d%d" , &a , &b);
if(a > b)
swap(a , b);
int d = b - a;
if(d == 0){
printf("%d\n" , a);
return;
}
int N = sqrt(d) + 1;
LL res = LLONG_MAX;
for(int q = 1 ; q <= N ; ++q){
if(d % q)
continue;
LL n = (a / q + (a % q ? 1 : 0)) * q;
res = min(res , n * (n + d) / __gcd(n , 1LL * d));
int p = d / q;
n = (a / p + (a % p ? 1 : 0)) * p;
res = min(res , n * (n + d) / __gcd(n , 1LL * d));
}
printf("%lld\n" , res);
}
int main(){
//freopen(".in" , "r" , stdin);
//freopen(".out" , "w" , stdout);
scanf("%d%d" , &c , &t);
while(t--)
solve();
return 0;
}
优化
动用一下 P3532 积累的 trick。
我们知道我们枚举的
我们又知道,最优的
也就是说可以不必每次计算