SDNU 1464.最大最小公倍数(思维)
生活随笔
收集整理的這篇文章主要介紹了
SDNU 1464.最大最小公倍数(思维)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
問(wèn)題描述 已知一個(gè)正整數(shù)N,問(wèn)從1~N中任選出三個(gè)數(shù),他們的最小公倍數(shù)最大可以為多少。Input
輸入一個(gè)正整數(shù) N(1<=N<=10^6)Output
輸出一個(gè)整數(shù),表示你找到的最小公倍數(shù)Sample Input
9Sample Output
504思路:網(wǎng)上說(shuō)這道題用o(n^3)的會(huì)T,都是假的!我用o(log n)的做法也T了!慘無(wú)人道。后來(lái)只能用數(shù)學(xué)的方法。
如果n是個(gè)奇數(shù),則(最大最小公倍數(shù)) = n*(n-1)*(n-2)
如果n是個(gè)偶數(shù),如果n是3的倍數(shù),則(最大最小公倍數(shù)) = (n-1)*(n-2)*(n-3)
否則(最大公約數(shù)) = n*(n-1)*(n-3)
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long const int inf = 0x3f3f3f3f; const int mod = 1e9+7;ll n, sum;int main() {scanf("%lld", &n);if(n <= 2)printf("%lld\n", n);else if(n%2 != 0){sum = n*(n-1)*(n-2);printf("%lld\n", sum);}else if(n%3 == 0){sum = (n-1)*(n-2)*(n-3);printf("%lld\n", sum);}else{sum = n*(n-1)*(n-3);printf("%lld\n", sum);}return 0; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/RootVount/p/11250412.html
總結(jié)
以上是生活随笔為你收集整理的SDNU 1464.最大最小公倍数(思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SDNU 1467.杨辉三角形(水题)
- 下一篇: 1452.接水问题(思维)