牛客网暑期ACM多校训练营(第五场)G max【数学】

https://www.nowcoder.com/acm/contest/143/G

给出一个 c c ,给出一个取值范围 [1,n] [ 1 , n ] 。在范围中取两个数 a,b a , b 使得 gcd(a,b)=c g c d ( a , b ) = c ,最大化 ab a ∗ b

答案相当于是 ccxy c ∗ c ∗ x ∗ y 其中 x,y x , y 互质,那么最好的办法就是两者相差为 1 1 x=y=1 x = y = 1 特判。

#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e5 + 12;
#define ll long long int 
int main() {
    ll c, n; ll a = -1, b = -1, ans = 0;
    scanf("%lld %lld", &c, &n);
    if (c > n) { puts("-1"); return 0; }
    a = ((n / c)*c);
    b = ((n / c - 1)*c);
    ans = a * b;
    if (n / c == 1) ans = c * c;
    printf("%lld\n", ans);

    return 0;
}

你可能感兴趣的:(Mathematics)