ACdream1077-LCM Challenge

LCM Challenge

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
Problem Description

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 than n. 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 ≤ 10^6) — 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 Input
9
Sample Output
504
Source
WJMZBMR
Manager

题意:求不超过n的三个数的最大公倍数。 
解题思路:首先要知道两个数论性质:1、相邻两个整数互质2、相邻两个奇数互质
然后,小数据观察规律,按n的奇偶性讨论。 
一、n为奇数时,n-2必定为奇数,而n-1与它俩都相邻,因此,两两互质。最大公倍数即为n*(n-1)*(n-2)
二、n为偶数时,由于n与n-2均为偶数,所以,退而求其次,选择n-3。又n与n-3是否互质,衍生出两种情况: 
    1、n为3的倍数,则n与n-3不互质,然而,n与n-4不互质,所以,此时,不选择n,改为选n-2。最后最大公倍数即为(n-1)*(n-2)*(n-3) 
    2、n不为3的倍数,则n与n-3互质,最后最大公倍数即为 n*(n-1)*(n-3) 

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>

using namespace std;

#define ll long long

int main()
{
    ll n;
    while(~scanf("%lld",&n))
    {
        if(n==1) printf("1\n");
        else if(n==2) printf("2\n");
        else if(n%2!=0) printf("%lld\n",n*(n-1)*(n-2));
        else if(n%3==0) printf("%lld\n",(n-1)*(n-2)*(n-3));
        else printf("%lld\n", n*(n-1)*(n-3));
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值