标签:
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
Output
Sample Input
9
Sample Output
504
大意:输入一个人n,求不大于n的三个数的公共的最小公倍数,check小于等于3的情况,else 分成奇偶,因为奇数时,n和n-2都是奇数,肯定是互质的,如果是偶数的话那就是三种情况里面的最大值
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int GCD(long a,long b){ return b == 0 ? a : GCD(b, a % b); } int main() { int n; while(~scanf("%d",&n)){ if(n == 1) printf("1\n"); else if(n == 2) printf("2\n"); else if(n == 3) printf("6\n"); else { if((n % 2) == 1) printf("%lld\n",1ll*n*(n-1)*(n-2)); else { long long max1 = 0; max1 = max(max1,1ll*(n-1)*(n-2)*(n-3)); max1 = max(max1,1ll*n*(n-1)*(n-3)/GCD(n,n-3)); max1 = max(max1,1ll*n*(n-1)*(n-2)/GCD(n,n-2)); printf("%lld\n",max1); } } } return 0; }
GCD写法:
int GCD(long a,long b){ return b == 0 ? a : GCD(b, a % b); }
HUST_ACdream区域赛指导赛之手速赛系列(1)(2)F——GCD+1ll——LCM Challenge
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4434410.html