275C - k-Multiple Free Set - CodeForces Solution


binary search greedy sortings *1500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const ll INF = 1e9;

int main() {
    ios_base::sync_with_stdio(0); 
    cin.tie(0);

    long long n, k;
    cin >> n >> k;

    vector<long long> arr(n);
    set<long long> num_set;
    for (int i =  0; i < n; i++) {
        cin >> arr[i];
        num_set.insert(arr[i]);
    }
    sort(arr.begin(), arr.end());
    set<long long> seen;
    int total_size = 0;
    for (int i = 0; i < n; i++) {
        if (seen.find(arr[i]) == seen.end()) {
            long long num = arr[i];
            int count = 1;
            seen.insert(num);
            while (num_set.find(num*k) != num_set.end() && seen.find(num*k) == seen.end()) {
                count += 1;
                num *= k;
                seen.insert(num);
            }
            if (count % 2 == 0) {
                total_size += count / 2;
            } else {
                total_size += (count + 1)/2;
            }
        }
    }
    cout << total_size;

    
}


Comments

Submit
0 Comments
More Questions

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut