QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#516648#9167. Coprime Arrayrogi52AC ✓0ms3816kbC++1718.4kb2024-08-12 20:04:182024-08-12 20:04:18

Judging History

你现在查看的是最新测评结果

  • [2024-08-12 20:04:18]
  • 评测
  • 测评结果:AC
  • 用时:0ms
  • 内存:3816kb
  • [2024-08-12 20:04:18]
  • 提交

answer

#line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

template < class T > vector< T >& operator++(vector< T >& a) { for(T& x : a) x++; return a; }
template < class T > vector< T >& operator--(vector< T >& a) { for(T& x : a) x--; return a; }
template < class T > vector< T >  operator++(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x++; return res; }
template < class T > vector< T >  operator--(vector< T >& a, signed) { vector< T > res = a; for(T& x : a) x--; return res; }

} // namespace macro

using namespace macro;
#line 1 "cp-library/src/number/crt.hpp"
// ax + by = gcd(a,b)
// return (x, y, gcd(a,b)) [min(|x|+|y|), x<=y]
template < class Int > std::tuple<Int, Int, Int> ext_gcd(Int a, Int b) {
    if(b == 0) return {1, 0, a};
    auto [s, t, g] = ext_gcd(b, a % b); // bs + (a%b)t = g
    // ax + by = g
    // <=> ((a/b)b + (a%b))x + by = g
    // <=> b((a/b)x + y) + (a%b)x = g
    return {t, s - (a / b) * t, g};
}

// 0 <= x < m
template < class Int > Int safe_mod(Int x, Int m) {
    x %= m;
    if(x < 0) x += m;
    return x;
}

// x = r[i] (mod m[i])
template < class Int > std::pair<Int, Int> crt(std::vector<Int> r, std::vector<Int> m) {
    assert(r.size() == m.size());
    const int n = r.size();
    auto [r0, m0] = std::make_pair<Int, Int>(0, 1);
    for(int i = 0; i < n; i++) {
        auto [r1, m1] = std::make_pair(safe_mod(r[i], m[i]), m[i]);
        if(m0 < m1) {
            std::swap(r0, r1);
            std::swap(m0, m1);
        }
        if(m0 % m1 == 0) {
            if(r0 % m1 != r1) return {0, 0};
            continue;
        }
        auto [im, _, g] = ext_gcd(m0, m1);
        if((r1 - r0) % g != 0) return {0, 0};
        Int u = m1 / g;
        r0 += (r1 - r0) / g % u * im % u * m0;
        m0 *= u;
        if(r0 < 0) r0 += m0;
    }
    return {r0, m0};
}

template < class Int > Int mod_inv(Int a, Int m) {
    auto [ia, _, g] = ext_gcd(a, m);
    return safe_mod(ia, m);
}

// x = r[i] (mod m[i])
// return (x mod M, lcm(m) mod M)
template < class Int > std::pair<Int, Int> garner(std::vector<Int> r, std::vector<Int> m, Int M, bool coprime) {
    assert(r.size() == m.size());
    const int n = r.size();

    if(not coprime) {
        auto gcd = [&](auto self, Int a, Int b) -> Int {
            if(b == 0) return a;
            return self(self, b, a % b);
        };
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < i; j++) {
                Int g = gcd(gcd, m[i], m[j]);
                if((r[i] - r[j]) % g != 0) return {0, 0};
                m[i] /= g, m[j] /= g;
                Int gi = gcd(gcd, m[i], g), gj = g / gi;
                do {
                    g = gcd(gcd, gi, gj);
                    gi *= g, gj /= g;
                } while(g != 1);
                m[i] *= gi, m[j] *= gj;
                r[i] %= m[i], r[j] %= m[j];
            }
        }
    }

    m.push_back(M);
    std::vector<Int> a(n + 1, 1), b(n + 1, 0);
    for(int k = 0; k < n; k++) {
        Int t = safe_mod((r[k] - b[k]) * mod_inv(a[k], m[k]), m[k]);
        for(int i = k + 1; i <= n; i++) {
            (b[i] += t * a[i]) %= m[i];
            (a[i] *= m[k]) %= m[i];
        }
    }
    return {b[n], a[n]};
}
#line 3 "cp-library/src/utility/random.hpp"

namespace randnum {

static uint seed;
static std::mt19937 mt;
struct gen_seed {
    gen_seed() {
        seed = std::random_device()();
        mt = std::mt19937(seed);
    }
} gs;

// [L, R)
template < class T >
T gen_int(T L, T R) {
    return std::uniform_int_distribution< T >(L, R - 1)(mt);
}

template < class T >
T get_real(T L, T R) {
    return std::uniform_real_distribution< T >(L, R)(mt);
}

}
#line 4 "cp-library/src/number/modfunc.hpp"

u64 modpow64(u64 a, u64 n, u64 mod) {
    a %= mod;
    u64 res = 1;
    while(n > 0) {
        if(n % 2 == 1) res = i128(res) * a % mod;
        a = i128(a) * a % mod;
        n /= 2;
    }
    return res;
}

u64 modpow(u64 a, u64 n, u64 mod) {
    a %= mod;
    u64 res = 1;
    while(n > 0) {
        if(n % 2 == 1) res = res * a % mod;
        a = a * a % mod;
        n /= 2;
    }
    return res;
}

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}

// solve x^2 = a (mod p)
// return x
// or No Solution (-1)
i32 modsqrt(i32 a, i32 p) {
    if(p == 2) return a;
    a %= p;
    if(a == 0) return 0;
    if(modpow(a, (p - 1) / 2, p) != 1) return -1;
    i32 q = p - 1, m = 0; while(q % 2 == 0) q /= 2, m++;
    i32 z; do { z = randnum::gen_int<i32>(1, p); } while(modpow(z, (p - 1) / 2, p) != p - 1);
    i64 c = modpow(z, q, p), t = modpow(a, q, p), r = modpow(a, (q + 1) / 2, p);
    while(m > 1) {
        if(modpow(t, 1 << (m - 2), p) != 1) r = r * c % p, t = t * (c * c % p) % p;
        c = c * c % p;
        m -= 1;
    }
    return r;
}
#line 5 "cp-library/src/number/prime.hpp"

bool miller_rabin(u64 n, std::vector<u64> witness) {
    if(n == 1) return false;
    if(n % 2 == 0) return n == 2;

    u64 d = n - 1;
    while(d % 2 == 0) d /= 2;
    for(u64 a : witness) if(a < n) {
        u64 y = modpow64(a, d, n), t = d;
        while(t != n - 1 and y != 1 and y != n - 1) {
            y = i128(y) * y % n;
            t *= 2;
        }
        if(y != n - 1 and t % 2 == 0) return false;
    }
    return true;
}

bool prime_test(u64 n) {
    if(n < (u64(1) << 32)) return miller_rabin(n, {2, 7, 61});
    return miller_rabin(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
}

u64 pollard_rho(u64 n) {
    if(n % 2 == 0) return 2;
    if(prime_test(n)) return n;
    while(true) {
        u64 R = randnum::gen_int<u64>(2, n), x, y = randnum::gen_int<u64>(2, n), ys, q = 1, g = 1, m = 128;
        auto f = [&](u64 x) {
            return (i128(x) * x % n + R) % n;
        };
        for(int r = 1; g == 1; r *= 2) {
            x = y;
            for(int i : rep(r)) y = f(y);
            for(int k = 0; g == 1 and k < r; k += m) {
                ys = y;
                for(int i = 0; i < m and i < r - k; i++) {
                    q = i128(q) * ((x - (y = f(y)) + n) % n) % n;
                }
                g = gcd(q, n);
            }
        }
        if(g == n) { do { g = gcd((x - (ys = f(ys))), n); } while(g == 1); }
        if(g != n) return g;
    }
    return 0;
}

std::vector<u64> factor(u64 n) {
    function<std::vector<u64>(u64)> dfs = [&](u64 n) {
        if(n <= 1) return std::vector<u64>{};
        u64 d = pollard_rho(n);
        if(d == n) return std::vector<u64>{n};
        std::vector<u64> L = dfs(d), R = dfs(n / d);
        L.insert(L.end(), R.begin(), R.end());
        return L;
    };
    std::vector<u64> res = dfs(n);
    sort(res.begin(), res.end());
    return res;
}

std::vector<std::pair<u64, i32>> factor_pair(u64 n) {
    std::vector<u64> pf = factor(n);
    std::vector<std::pair<u64, i32>> res;
    if(pf.empty()) return res;
    res.push_back({pf[0], 1});
    for(int i : rep(1, int(pf.size()))) {
        if(res.back().first == pf[i]) res.back().second++;
        else res.push_back({pf[i], 1});
    }
    return res;
}

u64 euler_phi(u64 n) {
    std::vector<std::pair<u64,i32>> pf = factor_pair(n);
    for(auto [p, e] : pf) n -= n / p;
    return n;
}
#line 5 "A.cpp"

pair<i64, i64> f(i64 s, i64 x) {
    assert(x % 2 == 1 or s % 2 == 0);
    vector<i64> r, m;
    for(auto [p, e] : factor_pair(x)) {
        r.push_back(1 + (s % p == 1));
        m.push_back(p);
    }
    auto [a, L] = crt(r, m);
    assert(gcd(a, x) == 1 and gcd(s - a, x) == 1);
    return make_pair(a, s - a);
}

int main() {
    i64 s = in(), x = in();
    if(gcd(s, x) == 1) {
        print(1);
        print(s);
    } else {
        if(x % 2 == 1 or s % 2 == 0) {
            auto [a, b] = f(s, x);
            print(2);
            print(a, b);
        } else {
            auto [a, b] = f(s - 1, x);
            print(3);
            print(1, a, b);
        }
    }
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3776kb

input:

9 6

output:

3
1 1 7

result:

ok Correct

Test #2:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

14 34

output:

2
1 13

result:

ok Correct

Test #3:

score: 0
Accepted
time: 0ms
memory: 3580kb

input:

1000000000 223092870

output:

2
148728581 851271419

result:

ok Correct

Test #4:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

2 1000000000

output:

2
1 1

result:

ok Correct

Test #5:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

649557664 933437700

output:

2
83981 649473683

result:

ok Correct

Test #6:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

33396678 777360870

output:

2
1 33396677

result:

ok Correct

Test #7:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

48205845 903124530

output:

3
1 18811 48187033

result:

ok Correct

Test #8:

score: 0
Accepted
time: 0ms
memory: 3516kb

input:

251037078 505905400

output:

2
1 251037077

result:

ok Correct

Test #9:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

30022920 172746860

output:

2
1 30022919

result:

ok Correct

Test #10:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

63639298 808058790

output:

2
248711 63390587

result:

ok Correct

Test #11:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

76579017 362768406

output:

3
1 1 76579015

result:

ok Correct

Test #12:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

40423669 121437778

output:

3
1 1 40423667

result:

ok Correct

Test #13:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

449277309 720915195

output:

2
1 449277308

result:

ok Correct

Test #14:

score: 0
Accepted
time: 0ms
memory: 3512kb

input:

81665969 919836918

output:

3
1 68069 81597899

result:

ok Correct

Test #15:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

470578680 280387800

output:

2
1 470578679

result:

ok Correct

Test #16:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

58450340 803305503

output:

2
1 58450339

result:

ok Correct

Test #17:

score: 0
Accepted
time: 0ms
memory: 3452kb

input:

125896113 323676210

output:

3
1 59281 125836831

result:

ok Correct

Test #18:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

381905348 434752500

output:

2
1 381905347

result:

ok Correct

Test #19:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

78916498 653897673

output:

1
78916498

result:

ok Correct

Test #20:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

35787885 270845190

output:

3
1 1 35787883

result:

ok Correct

Extra Test:

score: 0
Extra Test Passed