[codeforces 235]A. LCM Challenge
[codeforces 235]A. LCM Challenge
试题描述
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?
输入
The first line contains an integer n (1 ≤ n ≤ 106) — the n mentioned in the statement.
输出
Print a single integer — the maximum possible LCM of three not necessarily distinct positive integers that are not greater than n.
输入示例
9
输出示例
504
数据规模及约定
见“输入”
题解
对于奇数,那么显然答案是 lcm(n, n-1, n-2),对于偶数,答案只可能是这两种:
1. lcm(n, n-1, n-3)
2. lcm(n-1, n-2, n-3)
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack> #include <vector> #include <queue> #include <cstring> #include <string> #include <map> #include <set> using namespace std; const int BufferSize = 1 << 16; char buffer[BufferSize], *Head, *Tail; inline char Getchar() { if(Head == Tail) { int l = fread(buffer, 1, BufferSize, stdin); Tail = (Head = buffer) + l; } return *Head++; } int read() { int x = 0, f = 1; char c = getchar(); while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); } while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); } return x * f; } #define LL long long int n; LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); } LL lcm(LL a, LL b) { return a * b / gcd(a, b); } int main() { n = read(); if(n == 1) return puts("1"), 0; if(n & 1) return printf("%I64d\n", (LL)n * (n - 1) * (n - 2)), 0; int a1 = n - 3, b1 = n - 1, c1 = n; int a2 = n - 3, b2 = n - 2, c2 = n - 1; printf("%I64d\n", max(lcm(lcm(a1, b1), c1), lcm(lcm(a2, b2), c2))); return 0; }