Codeforces Round #146 (Div. 2) C. LCM Challenge

本文介绍了一个数学编程问题:如何选择三个不超过n的正整数,使得它们的最小公倍数最大。文章提供了一种解决方案,并通过示例展示了如何计算最大可能的最小公倍数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. LCM Challenge
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it.

But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater thann. Can you help me to find the maximum possible least common multiple of these three integers?

Input

The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.

Output

Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.

Sample test(s)
input
9
output
504
input
7
output
210
Note

The least common multiple of some positive integers is the least positive integer which is multiple for each of them.

The result may become very large, 32-bit integer won't be enough. So using 64-bit integers is recommended.

For the last example, we can chose numbers 765 and the LCM of them is 7·6·5 = 210. It is the maximum value we can get.

水题。

判断n是奇数还是偶数,如果n是奇数,(n-2)也是奇数,满足条件的情况就是 n*(n-1)*(n-2)

若n是偶数,则比它小的奇数是(n-1),一种情况是(n-1)*(n-2)*(n-3),另一种就是n*(n-1)*(n-3)/gcd(n,n-3) ,求这两者的最大值  整的来说可以使一个模3体系

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
__int64 gcd(__int64 a,__int64 b)
{
    if(b==0)
    return a;
    return gcd(b,a%b);
}
int main()
{
    __int64 n,ans=1;
    scanf("%I64d",&n);
    if(n==1)
    {
        printf("1\n");
        return 0;
    }
    else if(n==2)
    {
        printf("2\n");
        return 0;
    }
    if(n%2==1)
    {
        ans=n*(n-1)*(n-2);
        printf("%I64d\n",ans);
        return 0;
    }
    else
    {
    ans=n*(n-1)*(n-3);
    ans/=gcd(n,n-3);
    ans=max(ans,(n-3)*(n-1)*(n-2));
    printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值