235A - LCM Challenge - CodeForces Solution


number theory *1600

Please click on ads to support us..

Python Code:

def GCD(a,b):
    if(b==0):
        return a
    return GCD(b,a%b)
n=int(input())
if n==1:
    print(1)
elif(n==2):
    print(2)
else:
    if n&1:
        print(n*(n-1)*(n-2))
    else:
        if(GCD(n,n-3)==1):
            print(n*(n-3)*(n-1))
        else:
            print((n-1)*(n-2)*(n-3))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
#define mp make_pair
#define fi first
#define se second
#define fr(n) for(int i=0;i<n;i++)
#define fri(n) for(int i=1;i<=n;i++)
#define pb push_back
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b))
#define all(v) v.begin(),v.end()
#define rall(x) (x).rbegin(), (x).rend()


#define N 2e5 + 5
const int MAXN = 500005;
const int MOD = 1e9+7;



// int dfs(int node,int prev,vi &dist)
// {
//     visited[node]=true;
//     int next=0;
//     for(auto i: tree[node])
//     {
//         if(!visited[i])
//             next+=dfs(i,prev+1,dist);
//     }
//     dist.push_back(prev-next);
//     return next+1;
// }



int lcm(int a, int b){
    return (a*b)/__gcd(a,b); 
}


 void solve(){
    int n;
    cin>>n;
    if(n==1){
        cout<<1<<endl;
        return;
    }
    if(n==2){
        cout<<2<<endl;
        return;
    }
    if(n==3){
        cout<<6<<endl;
        return;
    }

    if(n%2!=0){
        cout<<n*(n-1)*(n-2);
        return;
    }
    else{
        if(n%3==0){
            cout<<(n-1)*(n-2)*(n-3);
        }
        else{
            cout<<(n)*(n-1)*(n-3);
        }

    }
    return;


 }

signed main(){
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tests;
    // cin>>tests;
    tests =1;
    while(tests--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors