LCM Challenge(暴力)
2015-05-24 11:43
826 查看
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、相邻两个整数互质。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。最后,LCM = (n-1)*(n-2)*(n-3)
2、n不为3的倍数,则n与n-3互质,所以,LCM = n*(n-1)*(n-3)
代码:
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、相邻两个整数互质。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。最后,LCM = (n-1)*(n-2)*(n-3)
2、n不为3的倍数,则n与n-3互质,所以,LCM = n*(n-1)*(n-3)
代码:
#include <cstdio> using namespace std; int main() { long long n; 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; }
相关文章推荐
- Codeforces 490F. Treeland Tour 暴力+LIS
- Testing Round #14 (Unrated) B - Door Frames 全排列暴力
- 【ASC45D】【构造 电阻网络法+暴力冲精度法+比例转换法】醉汉走路二分叉 回家概率恰为p/q
- Kali Linux 暴力破解 wifi密码
- HDU 5536 Chip Factory (暴力 或者 01Trie)
- codeforces 908 C. New Year and Curling【暴力枚举+图解】
- 【HDU1572】【旅行商问题 状压DP 数据小全排列暴力】下沙小面的(2)
- 雅礼集训暴力大神hxx
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J】【暴力模拟】Cleaner Robot 机器人行走 决策固定
- (老弱病残康复训练)51nod1995 三子棋(暴力,水题)
- 2012 Asia Tianjin Regional Contest - locker 暴力DP
- 字符串匹配暴力匹配法和KMP匹配算法对比
- Educational Codeforces Round 1 B. Queries on a String 暴力
- 2017 ACM-ICPC西安现场赛 J题LOL(暴力)
- HDU 5533/ 2015长春区域 G.Dancing Stars on Me 暴力
- HDU6115 Factory(最近公共祖先lca,在线倍增,暴力)
- ZSC oj 1538(DFS||暴力)
- ACM Southwestern European Regional Programming Contest (SWERC) 2017 A map+暴力
- foj 2150 Fire Game(bfs暴力)
- CodeForces - 918C The Monster (暴力)