A - LCM Challenge

A - LCM Challenge

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

Submit Status

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

题意:  输入n,让你从1~n中找出三个数,使他们的最大公倍数最大。(补充下小学五年级的知识:两个相临的奇数互质)我们知道要是他们的最大公倍数最大,既为从最大的n,n-1,n-2开始查找,  而且如果n为奇数的话,n*(n-1)*(n-2)=>奇数*偶数*奇数(且三个数互质),一定是所求最大的公倍数。  如果n为偶数的话,则有两种情况:  1,n能够被3整除;  1,n不能够被3整除;  因为n是偶数n*(n-1)*(n-2)=>偶数*奇数*偶数(且三个数不互质)<>不会是最大公倍数,

  所以它的最大公倍数顶多为n*(n-1)*(n-3)=>偶数*奇数*奇数(此时三个数可能不互质),  这里还需要判断的是n是否能够被3整除。  为什么只考虑只能被3整除的情况呢?  因为只有三个数,如果n能够被3整除的话,如果n能够被3整除的话,这三个数也便不互质了、  它的最大公倍数不可能是为n*(n-1)*(n-3),因为n-3同样也能够被3整除,
  这么三个数也不互质,最大值顶多可能是n*(n-1)*(n-5)=>偶数*奇数*奇数(不一定互质)以及更小,  或者为(n-1)*(n-2)*(n-3)=>奇数*偶数*奇数(且三个数互质),  因为(n-1)*(n-2)*(n-3)-n*(n-1)*(n-5)    =>(n-1)*{(n-2)*(n-3)-(n)*(n-5)}    =>(n-1)*{(n*n-5*n)+6-(n*n-5*n)}    =>(n-1)*6      因为n>=1所以(n-1)*(n-2)*(n-3)>n*(n-1)(n-5)    所以,当n为偶数,且能够被3整除的话,输出(n-1)*(n-2)*(n-3)  否则输出(n)*(n-1)*(n-3)

 1 #include <stdio.h>
 2 int main()
 3 {
 4     long long n;
 5     while(scanf("%lld",&n)!=EOF)
 6     {
 7         if(n==1)printf("1\n");
 8         else if(n==2)printf("2\n");
 9         else if(n%2==1)/*奇数*/
10         {
11             printf("%lld\n",n*(n-1)*(n-2));
12         }
13         else if(n%3==0)/*能被6整除*/
14         {
15             printf("%lld\n",(n-1)*(n-2)*(n-3));
16         }
17         else    /*能被2整除*/
18         {
19             printf("%lld\n",(n)*(n-1)*(n-3));
20         }
21     }
22     return 0;
23 }

时间: 2024-10-04 15:25:39

A - LCM Challenge的相关文章

acd LCM Challenge(求1~n的任意三个数的最大公倍数)

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

ACdream1077:LCM Challenge

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

acd LCM Challenge(求1~n的随意三个数的最大公倍数)

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

acdream LCM Challenge (最小公倍数)

LCM Challenge Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) Submit Status 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

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

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 do

hdu 4497 GCD and LCM

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1092    Accepted Submission(s): 512 Problem Description Given two positive integers G and L, could you tell me how many solutions of (

题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)

题目链接:http://ac.jobdu.com/problem.php?pid=1439 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. /

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {

GCD AND LCM

1 #include <cstdio> 2 __int64 GCD(__int64 a,__int64 b) 3 { 4 if (a % b == 0) 5 return b; 6 else 7 return GCD(b,a%b); 8 } 9 __int64 LCM(__int64 a,__int64 b) // a * b ==gcd*lcm 10 { 11 return a / GCD(a,b) * b ; 12 } 13 int main() 14 { 15 __int64 a,b;