結果

問題 No.1318 ABCD quadruplets
ユーザー PachicobuePachicobue
提出日時 2020-12-15 00:25:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 15,946 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 214,868 KB
実行使用メモリ 76,476 KB
最終ジャッジ日時 2024-09-20 01:03:38
合計ジャッジ時間 5,959 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,760 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using ld = long double;
template<typename T> using max_heap = std::priority_queue<T>;
template<typename T> using min_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; }
constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; }
constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); }
constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; }
constexpr ull ceil2(const ull v) { return 1ULL << clog(v); }
constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; }
constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; }
template<typename T> void bset(T& mask, const int ind) { mask |= ((T)1 << ind); }
template<typename T> void breset(T& mask, const int ind) { mask &= ~((T)1 << ind); }
template<typename T> void bflip(T& mask, const int ind) { mask ^= ((T)1 << ind); }
template<typename T> void bset(T& mask, const int ind, const bool b) { (b ? bset(mask, ind) : breset(mask, ind)); }
template<typename T> bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); }
template<typename T> bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); }
template<typename T> constexpr T inf_v = std::numeric_limits<T>::max() / 4;
template<typename Real> constexpr Real pi_v = Real{3.141592653589793238462643383279502884};
template<typename T> constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN<T>(n - 1) * T{10}; }
template<typename F> struct fix : F
{
    fix(F&& f) : F{std::forward<F>(f)} {}
    template<typename... Args> auto operator()(Args&&... args) const { return F::operator()(*this, std::forward<Args>(args)...); }
};
template<typename T, int n, int i = 0>
auto nd_array(int const (&szs)[n], const T x = T{})
{
    if constexpr (i == n) {
        return x;
    } else {
        return std::vector(szs[i], nd_array<T, n, i + 1>(szs, x));
    }
}
class printer
{
public:
    printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); }
    template<typename... Args> int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; }
    template<typename... Args> int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; }
private:
    template<typename T> void dump(const T& v) { m_os << v; }
    template<typename T> void dump(const std::vector<T>& vs)
    {
        for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); }
    }
    template<typename T> void dump(const std::vector<std::vector<T>>& vss)
    {
        for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); }
    }
    template<typename T, typename... Args> int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; }
    std::ostream& m_os;
};
printer out;
class scanner
{
public:
    scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); }
    template<typename T> T val()
    {
        T v;
        return m_is >> v, v;
    }
    template<typename T> T val(const T offset) { return val<T>() - offset; }
    template<typename T> std::vector<T> vec(const int n)
    {
        return make_v<T>(n, [this]() { return val<T>(); });
    }
    template<typename T> std::vector<T> vec(const int n, const T offset)
    {
        return make_v<T>(n, [this, offset]() { return val<T>(offset); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1)
    {
        return make_v<std::vector<T>>(n0, [this, n1]() { return vec<T>(n1); });
    }
    template<typename T> std::vector<std::vector<T>> vvec(const int n0, const int n1, const T offset)
    {
        return make_v<std::vector<T>>(n0, [this, n1, offset]() { return vec<T>(n1, offset); });
    }
    template<typename... Args> auto tup() { return std::tuple<std::decay_t<Args>...>{val<Args>()...}; }
    template<typename... Args> auto tup(const Args&... offsets) { return std::tuple<std::decay_t<Args>...>{val<Args>(offsets)...}; }
private:
    template<typename T, typename F>
    std::vector<T> make_v(const int n, F f)
    {
        std::vector<T> ans;
        for (int i = 0; i < n; i++) { ans.push_back(f()); }
        return ans;
    }
    std::istream& m_is;
};
scanner in;
template<typename T>
struct complex
{
    complex() : real{T{0}}, imag{T{0}} {}
    complex(const complex&) = default;
    complex(const T& r, const T& i) : real{r}, imag{i} {}
    friend complex operator+(const complex& c) { return c; }
    friend complex operator-(const complex& c) { return complex{-c.real, -c.imag}; }
    friend complex operator+(const complex& c1, const complex& c2) { return complex{c1.real + c2.real, c1.imag + c2.imag}; }
    friend complex operator-(const complex& c1, const complex& c2) { return complex{c1.real - c2.real, c1.imag - c2.imag}; }
    friend complex operator*(const complex& c1, const complex& c2) { return complex{c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.imag + c1.imag * c2.real}; }
    friend complex operator/(complex& c1, complex& c2) { c1* c2.conj() / c2.norm(); }
    friend complex operator+(const complex& c, const T& v) { return complex{c.real + v, c.imag}; }
    friend complex operator-(const complex& c, const T& v) { return complex{c.real - v, c.imag}; }
    friend complex operator*(const complex& c, const T& v) { return complex{c.real * v, c.imag * v}; }
    friend complex operator/(const complex& c, const T& v) { return complex{c.real / v, c.imag / v}; }
    friend complex operator+(const T& v, const complex& c) { return complex{v + c.real, c.imag}; }
    friend complex operator-(const T& v, const complex& c) { return complex{v - c.real, -c.imag}; }
    friend complex operator*(const T& v, const complex& c) { return complex{v * c.real, v * c.imag}; }
    friend complex operator/(const T& v, const complex& c) { return v * c.conj() / c.norm(); }
    friend bool operator==(const complex& c1, const complex& c2) { return c1.real == c2.real and c1.imag == c2.imag; }
    friend bool operator!=(const complex& c1, const complex& c2) { return not(c1 == c2); }
    friend complex& operator+=(complex& c1, const complex& c2) { return c1.real += c2.real, c1.imag += c2.imag, c1; }
    friend complex& operator-=(complex& c1, const complex& c2) { return c1.real += c2.real, c1.imag += c2.imag, c1; }
    friend complex& operator*=(complex& c1, const complex& c2) { return c1 = c1 * c2; }
    friend complex& operator/=(complex& c1, const complex& c2) { return c1 = c1 / c2; }
    friend complex& operator+=(complex& c, const T& v) { return c = c + v; }
    friend complex& operator-=(complex& c, const T& v) { return c = c - v; }
    friend complex& operator*=(complex& c, const T& v) { return c = c * v; }
    complex conj() const { return complex{real, -imag}; }
    T norm() const { return real * real + imag * imag; }
    T abs() const { return std::sqrt(norm()); }
    T arg() const { return std::atan2(imag, real); }
    friend std::ostream& operator<<(std::ostream& os, const complex& c) { return os << c.real << "+" << c.imag << "i"; }
    T real, imag;
};
template<const uint& mod>
class modint
{
public:
    modint() : m_val{0} {}
    modint(const ll v) : m_val{normll(v)} {}
    modint(const modint& m) = default;
    modint& operator =(const modint& m) { return m_val = m(), (*this); }
    modint& operator =(const ll v) { return m_val = normll(v), (*this); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint{0} - (*this); }
    modint& operator+=(const modint& m) { return m_val = norm(m_val + m()), *this; }
    modint& operator-=(const modint& m) { return m_val = norm(m_val + mod - m()), *this; }
    modint& operator*=(const modint& m) { return m_val = normll((ll)m_val * (ll)m() % (ll)mod), *this; }
    modint& operator/=(const modint& m) { return *this *= m.inv(); }
    modint& operator+=(const ll val) { return *this += modint{val}; }
    modint& operator-=(const ll val) { return *this -= modint{val}; }
    modint& operator*=(const ll val) { return *this *= modint{val}; }
    modint& operator/=(const ll val) { return *this /= modint{val}; }
    modint operator+(const modint& m) const { return modint{*this} += m; }
    modint operator-(const modint& m) const { return modint{*this} -= m; }
    modint operator*(const modint& m) const { return modint{*this} *= m; }
    modint operator/(const modint& m) const { return modint{*this} /= m; }
    modint operator+(const ll v) const { return *this + modint{v}; }
    modint operator-(const ll v) const { return *this - modint{v}; }
    modint operator*(const ll v) const { return *this * modint{v}; }
    modint operator/(const ll v) const { return *this / modint{v}; }
    bool operator==(const modint& m) const { return m_val == m(); }
    bool operator!=(const modint& m) const { return not(*this == m); }
    friend modint operator+(const ll v, const modint& m) { return modint{v} + m; }
    friend modint operator-(const ll v, const modint& m) { return modint{v} - m; }
    friend modint operator*(const ll v, const modint& m) { return modint{v} * m; }
    friend modint operator/(const ll v, const modint& m) { return modint{v} / m; }
    friend std::istream& operator>>(std::istream& is, modint& m)
    {
        ll v;
        return is >> v, m = v, is;
    }
    friend std::ostream& operator<<(std::ostream& os, const modint& m) { return os << m(); }
    uint operator()() const { return m_val; }
    modint pow(ull n) const
    {
        modint ans = 1;
        for (modint x = *this; n > 0; n >>= 1, x *= x) {
            if (n & 1ULL) { ans *= x; }
        }
        return ans;
    }
    modint inv() const { return m_val <= 2000000 ? sinv(m_val) : pow(mod - 2); }
    static modint fact(const uint n)
    {
        static std::vector<modint> fs{1, 1};
        for (uint i = (uint)fs.size(); i <= n; i++) { fs.push_back(fs.back() * i); }
        return fs[n];
    }
    static modint ifact(const uint n)
    {
        static std::vector<modint> ifs{1, 1};
        for (uint i = (uint)ifs.size(); i <= n; i++) { ifs.push_back(ifs.back() * sinv(i)); }
        return ifs[n];
    }
    static modint perm(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k); }
    static modint comb(const int n, const int k) { return k > n or k < 0 ? modint{0} : fact(n) * ifact(n - k) * ifact(k); }
private:
    static constexpr uint norm(const uint x) { return x < mod ? x : x - mod; }
    static constexpr uint normll(const ll x) { return norm(uint(x % (ll)mod + (ll)mod)); }
    static modint sinv(const uint n)
    {
        static std::vector<modint> is{1, 1};
        for (uint i = (uint)is.size(); i <= n; i++) { is.push_back(-is[mod % i] * (mod / i)); }
        return is[n];
    }
    uint m_val;
};
template<typename R>
class fft
{
private:
    static constexpr R pi = pi_v<R>;
    static constexpr int D = 30;
    static constexpr int B = 14;
public:
    static void trans(std::vector<complex<R>>& as, const int lg, const bool rev)
    {
        const int N = as.size();
        static std::vector<complex<R>> root[D];
        if (root[lg].empty()) {
            root[lg].resize(N);
            for (int i = 0; i < N; i++) {
                const R theta = pi * R(2 * i) / R(N);
                root[lg][i] = complex<R>(std::cos(theta), std::sin(theta));
            }
        }
        std::vector<complex<R>> tmp(N);
        for (int w = (N >> 1); w > 0; w >>= 1) {
            for (int y = 0; y < (N >> 1); y += w) {
                const complex<R> r = rev ? root[lg][y].conj() : root[lg][y];
                for (int x = 0; x < w; x++) {
                    const auto u = as[y << 1 | x], v = as[y << 1 | x | w] * r;
                    tmp[y | x] = u + v, tmp[y | x | (N >> 1)] = u - v;
                }
            }
            std::swap(tmp, as);
        }
    }
    fft() = delete;
    template<typename T, typename I>
    static std::vector<T> convolute_neg(const std::vector<I>& as, const std::vector<I>& bs)
    {
        const int need = as.size() + bs.size() - 1, lg = clog(need), sz = 1UL << lg;
        std::vector<complex<R>> xs(sz), ys(sz);
        for (int i = 0; i < as.size(); i++) { xs[i] = {(R)as[i], (R)0}; }
        for (int i = 0; i < bs.size(); i++) { ys[i] = {(R)bs[i], (R)0}; }
        trans(xs, lg, false), trans(ys, lg, false);
        for (int i = 0; i < sz; i++) { xs[i] *= ys[i]; }
        trans(xs, lg, true);
        std::vector<T> ans(need);
        for (int i = 0; i < need; i++) { ans[i] = (T)std::round(xs[i].real / (R)sz); }
        return ans;
    }
    template<const uint& mod>
    static std::vector<modint<mod>> conv_mod(const std::vector<modint<mod>>& as, const std::vector<modint<mod>>& bs, const int division = 2)
    {
        using T = modint<mod>;
        const int bitnum = (D + division - 1) / division;
        const int need = as.size() + bs.size() - 1, lg = clog(need), sz = 1UL << lg;
        std::vector<std::vector<complex<R>>> xs(division, std::vector<complex<R>>(sz)), ys(division, std::vector<complex<R>>(sz));
        std::vector<complex<R>> tmp(sz);
        for (int i = 0; i < division; i++) {
            std::fill(tmp.begin() + std::min(as.size(), bs.size()), tmp.end(), complex<R>{});
            for (int j = 0; j < (int)as.size(); j++) { tmp[j].real = R(((ll)as[j]() >> (bitnum * i)) & ((1 << bitnum) - 1)); }
            for (int j = 0; j < (int)bs.size(); j++) { tmp[j].imag = R(((ll)bs[j]() >> (bitnum * i)) & ((1 << bitnum) - 1)); }
            trans(tmp, lg, false);
            for (int j = 0; j < sz; j++) { tmp[j] *= R(0.5); }
            for (int j = 0; j < sz; j++) {
                const int k = j == 0 ? 0UL : sz - j;
                xs[i][j] = complex<R>{tmp[j].real + tmp[k].real, tmp[j].imag - tmp[k].imag}, ys[i][j] = complex<R>{tmp[j].imag + tmp[k].imag, -tmp[j].real + tmp[k].real};
            }
        }
        std::vector<std::vector<complex<R>>> zs(division, std::vector<complex<R>>(sz));
        for (int a = 0; a < division; a++) {
            for (int b = 0; b < division; b++) {
                for (int i = 0; i < sz; i++) {
                    if (a + b < division) {
                        zs[a + b][i] += xs[a][i] * ys[b][i];
                    } else {
                        zs[a + b - division][i] += xs[a][i] * ys[b][i] * complex<R>(0, 1);
                    }
                }
            }
        }
        for (int i = 0; i < division; i++) { trans(zs[i], lg, true); }
        std::vector<T> ans(need);
        T base = 1;
        for (int k = 0; k < 2 * division - 1; k++, base *= (1LL << bitnum)) {
            for (int i = 0; i < need; i++) {
                if (k < division) {
                    ans[i] += base * T((ll)std::round(zs[k][i].real / R(sz)));
                } else {
                    ans[i] += base * T((ll)std::round(zs[k - division][i].imag / R(sz)));
                }
            }
        }
        return ans;
    }
};
int main()
{
    const auto [N, M] = in.tup<int, int>();
    auto dp = nd_array<ll>({2 * M + 1, 2 * N + 1}, 0);
    for (int a = 0; a <= M; a++) {
        for (int b = 0; b <= M; b++) {
            const int s = a * a + b * b;
            if (s > 2 * N) { break; }
            dp[a + b][s]++;
        }
    }
    std::vector<ll> ans(2 * N + 1, 0);
    for (int i = 0; i <= 2 * M; i++) {
        for (int j = 0; j <= 2 * M; j++) {
            if ((i + j) * (i + j) > 2 * N) { break; }
            const int s = (i + j) * (i + j);
            const auto conv = fft<double>::convolute_neg<ll>(dp[i], dp[j]);
            for (int k = 0; k + s <= 2 * N; k++) { ans[k + s] += conv[k]; }
        }
    }
    for (int i = 0; i <= N; i++) { out.ln(ans[2 * i]); }
    return 0;
}
0