cp-documentation

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/cp-documentation

:heavy_check_mark: Test/AtCoder/abc177_e.test.cpp

Depends on

Code

// #define PROBLEM "https://atcoder.jp/contests/abc177/tasks/abc177_e"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

/*
 * AtCoder Beginner Contest 177 - E Coprime
 * https://atcoder.jp/contests/abc177/submissions/66240758
 */

#include "../../Src/Number/LinearSieve.hpp"

#include <iostream>
#include <numeric>
#include <unordered_set>

const int N{ 1000100 };

int main() {
#ifdef ATCODER
    int n; std::cin >> n;
    zawa::LinearSieve siv(N);
    int setGCD{};
    std::unordered_set<int> divisors;
    bool pairwise{true};
    for (int _{} ; _ < n ; _++) {
        int a; std::cin >> a;
        setGCD = std::gcd(setGCD, a);
        for (auto p : siv.factorize(a)) if (p.factor() > 1) {
            pairwise &= divisors.find(p.factor()) == divisors.end();
            divisors.emplace(p.factor());
        }
    }
    if (pairwise) {
        std::cout << "pairwise coprime" << std::endl;
    }
    else if (setGCD == 1) {
        std::cout << "setwise coprime" << std::endl;
    }
    else {
        std::cout << "not coprime" << std::endl;
    }
#else
    std::cout << "Hello World\n";
#endif
}
#line 1 "Test/AtCoder/abc177_e.test.cpp"
// #define PROBLEM "https://atcoder.jp/contests/abc177/tasks/abc177_e"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

/*
 * AtCoder Beginner Contest 177 - E Coprime
 * https://atcoder.jp/contests/abc177/submissions/66240758
 */

#line 2 "Src/Number/LinearSieve.hpp"

#line 2 "Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 2 "Src/Number/PrimeFactor.hpp"

#line 4 "Src/Number/PrimeFactor.hpp"

#include <type_traits>

namespace zawa {

template <class T>
class PrimeFactor {
    static_assert(std::is_unsigned_v<T>, "T must be unsigned integer");
private:
    T factor_{};
    u32 exponent_{};

public: 
    PrimeFactor() = default;

    PrimeFactor(T factor, u32 exponent) : factor_{factor}, exponent_{exponent} {}

    inline T factor() const noexcept {
        return factor_;
    }

    inline u32 exponent() const noexcept {
        return exponent_;
    }

    friend bool operator<(const PrimeFactor<T>& lhs, const PrimeFactor<T>& rhs) {
        return lhs.factor() != rhs.factor() ?
            lhs.factor() < rhs.factor() :
            lhs.exponent() < rhs.exponent();
    }

    friend bool operator>(const PrimeFactor<T>& lhs, const PrimeFactor<T>& rhs) {
        return lhs.factor() != rhs.factor() ?
            lhs.factor() > rhs.factor() :
            lhs.exponent() > rhs.exponent();
    }
};

} // namespace zawa
#line 5 "Src/Number/LinearSieve.hpp"

#include <concepts>
#include <vector>
#include <utility>
#include <cassert>

namespace zawa {

class LinearSieve {
public:

    using V = u32;
    using F = PrimeFactor<V>;

private:

    std::vector<V> primes_;
    std::vector<V> lpf_;

public:

    explicit LinearSieve(V n) : primes_{}, lpf_(n + 1) {
        for (V i{2} ; i <= n ; i++) {
            if (!lpf_[i]) {
                lpf_[i] = i;
                primes_.emplace_back(i);
            }
            for (V p : primes_) {
                if (static_cast<u64>(p) * i > n) break;
                lpf_[p * i] = p;
            }
        }
    }

    V size() const noexcept {
        return static_cast<V>(lpf_.size()) - 1;
    }

    bool isPrime(V x) const noexcept {
        assert(x < lpf_.size());
        return lpf_[x] == x;
    }

    bool operator[](V x) const noexcept {
        assert(x < lpf_.size());
        return lpf_[x] == x;
    }

    std::vector<V> primes() const {
        return primes_;
    }

    // @note: response array is not sorted.
    template <std::integral T = V>
    std::vector<T> divisor(V x) const {
        assert(0u < x and x < lpf_.size());
        std::vector<T> res(1, 1u);
        while (x > 1) {
            V factor{lpf_[x]};
            u32 exponent{};
            while (lpf_[x] == factor) {
                exponent++;
                x /= lpf_[x];
            }
            usize line{res.size()};
            V now{1};
            for (u32 i{} ; i < exponent ; i++) {
                now *= factor;
                for (usize j{} ; j < line ; j++) {
                    res.emplace_back(static_cast<T>(res[j] * now));
                }
            }
        }
        return res;
    }

    std::vector<F> factorize(V x) const {
        assert(0u < x and x < lpf_.size());
        std::vector<F> res;
        while (x > 1) {
            V factor{lpf_[x]};
            u32 exponent{};
            while (lpf_[x] == factor) {
                exponent++;
                x /= lpf_[x];
            }
            res.emplace_back(factor, exponent);
        }
        return res;
    }

    i32 mobius(V x) const {
        assert(0u < x and x < lpf_.size());
        i32 res = 1;
        while (x > 1u) {
            V factor = lpf_[x];
            u32 exp = 0;
            while (lpf_[x] == factor) {
                x /= factor;
                exp++;
            }
            if (exp >= 2u) return 0;
            res *= -1;
        }
        return res;
    }
};

} // namespace zawa
#line 10 "Test/AtCoder/abc177_e.test.cpp"

#include <iostream>
#include <numeric>
#include <unordered_set>

const int N{ 1000100 };

int main() {
#ifdef ATCODER
    int n; std::cin >> n;
    zawa::LinearSieve siv(N);
    int setGCD{};
    std::unordered_set<int> divisors;
    bool pairwise{true};
    for (int _{} ; _ < n ; _++) {
        int a; std::cin >> a;
        setGCD = std::gcd(setGCD, a);
        for (auto p : siv.factorize(a)) if (p.factor() > 1) {
            pairwise &= divisors.find(p.factor()) == divisors.end();
            divisors.emplace(p.factor());
        }
    }
    if (pairwise) {
        std::cout << "pairwise coprime" << std::endl;
    }
    else if (setGCD == 1) {
        std::cout << "setwise coprime" << std::endl;
    }
    else {
        std::cout << "not coprime" << std::endl;
    }
#else
    std::cout << "Hello World\n";
#endif
}
Back to top page