ACdream1077-LCM Challenge

来源:互联网 发布:ubuntu rc.local不存在 编辑:程序博客网 时间:2024/05/22 00:20

LCM Challenge

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

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

admin

题意:求不超过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 longint 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
原创粉丝点击