『题解』洛谷P5436 【XR-2】缘分

Problem Portal

Portal1:Luogu

Description

一禅希望知道他和师父之间的缘分大小。可是如何才能知道呢?

一禅想了个办法,他先和师父约定一个正整数(n),接着他们各自在心里想一个不超过(n)的正整数。

一禅认为,他和师父心里想的这两个数的最小公倍数越大,则意味着他和师父之间的缘分越大。

师父觉得这个办法很合适,不过他想知道这两个数的最小公倍数最大会是多少。

师父的数学不太好,于是问一禅。一禅也觉得这个问题很困难,他希望你能告诉他答案。

Input

第一行一个正整数(T),表示数据组数。

接下来的(T)行,每行一个正整数(n),表示一禅和师父约定的正整数。

Output

对每组数据,一行一个正整数,表示答案。

Sample Input

1
3

Sample Output

6

Solution

首先,(gcd(x, x - 1) = 1),所以在(n)范围内,最小公倍数最大的一定是(mathrm{lcm}(n, n - 1) = n imes (n - 1)),于是答案就是(n imes (n - 1))

特殊地,当(n = 1)时,因为(n - 1 = 0),所以最大只能是(mathrm{lcm}(1, 1) = 1)

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;
int T;
LL n;
int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%lld", &n);//考虑到后面还需相乘,会爆int
        if (n != 1) printf("%lld
", n * (n - 1)); else printf("1
");//特判
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shenxiaohuang/p/11221074.html