1154G - Minimum Possible LCM - CodeForces Solution


brute force greedy math number theory *2200

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int long long
#define pb push_back
#define fi first
#define se second
#define el '\n'
#define ss ' '
#define ll long long
#define vii vector<pair<int,int>>
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define rall(a) a.rbegin(),a.rend()
#define all(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
#define popcount(x) __builtin_popcount(x)
#define zedanPasha ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define clr(x,y) memset(x,y,sizeof(x))

template<typename T=int>
istream& operator >>(istream& in,vector<T>& a)
{
    for(T&i:a) cin>>i;
    return in;
}
template<typename T=int>
ostream& operator <<(ostream& out,vector<T>& a)
{
    for(T&i:a) cout<<i<<ss;
    return out;
}
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

int dx[] = {0, 0, -1, 1};//right,left,up,down
int dy[] = {1, -1, 0, 0};

bool valid(int r, int c, int R, int C)
{
    if (r < 0 || r >= R || c < 0 || c >= C)
        return false;
    return true;
}

ll fp(ll a, ll b)
{
    ll ans=1;
    while (b)
    {
        if(b&1)
            ans*=a;
        a*=a;
        b>>=1;
    }
    return ans;
}
int n;
vi a;
int const N = 1e7 + 5;
int mp[N];

int f,s;
void sieve()
{
    ll mn = LLONG_MAX;
    for(int i=1 ; i<=N ; i++)
    {
        ll sm1=0,sm2=0;
        for(int j=i ; j<N ; j+=i)
        {
            if(mp[j])
            {
                int cnt = mp[j];
                if(!sm1)
                {
                    sm1 = j;
                    if(cnt-1)
                    {
                        sm2 = j;
                        break;
                    }
                }
                else
                {
                    sm2 = j;
                    break;
                }
            }
        }
        ll cur = (sm1*sm2)/i;
        if(sm1 && sm2 && mn>cur)
        {
            f = sm1,s = sm2;
            mn = cur;
        }
    }
}
void doWork()
{
    cin>>n;
    a.resize(n);
    for(int i=0 ; i<n ; i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    sieve();
    vll ans;
    for(int i=0 ; i<n ; i++)
    {
        if(a[i]==f)
        {
            ans.pb(i+1);
            f=-1;
        }
        else if(a[i]==s)
        {
            ans.pb(i+1);
            s=-1;
        }
        if(sz(ans)==2)
            break;
    }
    sort(all(ans));
    cout<<ans;
}

signed main()
{
    zedanPasha
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    while (t--)
        doWork();
    return 0;
}


Comments

Submit
0 Comments
More Questions

WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines