码迷,mamicode.com
首页 > 其他好文 > 详细

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge

时间:2015-04-17 13:30:32      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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
大意:输入一个人n,求不大于n的三个数的公共的最小公倍数,check小于等于3的情况,else 分成奇偶,因为奇数时,n和n-2都是奇数,肯定是互质的,如果是偶数的话那就是三种情况里面的最大值
技术分享
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int GCD(long  a,long  b){
    return b == 0 ? a : GCD(b, a % b);
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
    if(n == 1)
        printf("1\n");
    else if(n == 2)
        printf("2\n");
    else if(n == 3)
        printf("6\n");
    else {
        if((n % 2) == 1)
        printf("%lld\n",1ll*n*(n-1)*(n-2));
        else {
            long long max1 = 0;
            max1 = max(max1,1ll*(n-1)*(n-2)*(n-3));
            max1 = max(max1,1ll*n*(n-1)*(n-3)/GCD(n,n-3));
            max1 = max(max1,1ll*n*(n-1)*(n-2)/GCD(n,n-2));
            printf("%lld\n",max1);
            }
        }
    }
    return 0;
}
View Code

GCD写法:

int GCD(long  a,long  b){
    return b == 0 ? a : GCD(b, a % b);
}

 

 

HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4434410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!