zoukankan      html  css  js  c++  java
  • [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
    ", (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
    ", max(lcm(lcm(a1, b1), c1), lcm(lcm(a2, b2), c2)));
    	
    	return 0;
    }
    
  • 相关阅读:
    python元编程(metaclass)
    STL源码剖析:序
    高效C++:定制new和delete
    高效C++:模板和泛型编程
    高效C++:继承和实现
    高效C++:实现
    高效C++:设计与声明
    高效C++:资源管理
    高效C++:构造/析构/赋值
    Noip2017退役记
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5813170.html
Copyright © 2011-2022 走看看