Submission #9166904


Source Code Expand

#include <bits/stdc++.h>

template <int MODULUS = 1000000007>
struct modulo_int {
private:

    long long value;
    constexpr void normalize() {
        value %= MODULUS;
        if (value < 0) value += MODULUS;
    }

public:

    constexpr modulo_int(const long long& value_ = 0) : value(value_) { normalize(); }
    constexpr modulo_int operator - () const { return modulo_int(MODULUS - value); }
    constexpr modulo_int operator ~ () const { return power(MODULUS - 2); }
    constexpr long long operator () () const { return value; }

    constexpr modulo_int operator + (const modulo_int& rhs) const { return modulo_int(*this) += rhs; }
    constexpr modulo_int& operator += (const modulo_int& rhs) {
        if ((value += rhs.value) >= MODULUS) value -= MODULUS;
        return (*this);
    }

    constexpr modulo_int operator - (const modulo_int& rhs) const { return modulo_int(*this) -= rhs; }
    constexpr modulo_int& operator -= (const modulo_int& rhs) {
        if ((value += MODULUS - rhs.value) >= MODULUS) value -= MODULUS;
        return (*this);
    }

    constexpr modulo_int operator * (const modulo_int& rhs) const { return modulo_int(*this) *= rhs; }
    constexpr modulo_int& operator *= (const modulo_int& rhs) {
        (value *= rhs.value) %= MODULUS;
        return (*this);
    }

    constexpr modulo_int operator / (const modulo_int& rhs) const { return modulo_int(*this) /= rhs; }
    constexpr modulo_int& operator /= (const modulo_int& rhs) {
        return (*this) *= ~rhs;
    }

    constexpr modulo_int power(const long long& power_) const {
        modulo_int result(1), mult(*this);
        for (long long t = power_; t > 0; t >>= 1) {
            if (t & 1) result *= mult;
            mult *= mult;
        }
        return result;
    }

    friend std::istream& operator >> (std::istream& stream, modulo_int& lhs) {
        stream >> lhs.value;
        lhs.normalize();
        return stream;
    }

    friend std::ostream& operator << (std::ostream& stream, const modulo_int& rhs) {
        return stream << rhs.value;
    }

};

template <class T, int MODULUS = 998244353, int PRIM = 3>
void modulo_transform(std::vector<modulo_int<MODULUS>>& F, const bool& inverse = false) {
    int F_sz = F.size();
    for (int i = 0, j = 1; j < F_sz - 1; ++j) {
        for (int k = (F_sz >> 1); k > (i ^= k); k >>= 1);
        if (i < j) std::swap(F[i], F[j]);
    }
    modulo_int<MODULUS> zeta = modulo_int<MODULUS>(PRIM).power((MODULUS - 1) / F_sz);
    if (inverse) zeta = ~zeta;
    std::vector<modulo_int<MODULUS>> coeff(F_sz);
    coeff[0] = 1;
    for (int i = 1; i < F_sz; ++i) {
        coeff[i] = coeff[i - 1] * zeta;
    }
    int index;
    modulo_int<MODULUS> first, second;
    for (int len = 1, bit = (F_sz >> 1); len < F_sz; len <<= 1, bit >>= 1) {
        for (int k = 0; k < F_sz; k += (len << 1)) {
            index = 0;
            for (int i = 0; i < len; ++i) {
                first = F[i + k];
                second = F[(i + k) ^ len];
                F[i + k] = coeff[0] * first + coeff[index] * second;
                F[(i + k) ^ len] = coeff[0] * first + coeff[index + (F_sz >> 1)] * second;
                index += bit;
            }
        }
    }
}

template <class T, int MODULUS = 998244353, int PRIM = 3>
std::vector<T> convolute(const std::vector<T>& A, const std::vector<T>& B) {
    int A_sz = A.size(), B_sz = B.size(), result_sz = A_sz + B_sz - 1;
    int X_sz = 1;
    while (X_sz < result_sz) {
        X_sz <<= 1;
    }
    std::vector<modulo_int<MODULUS>> C(X_sz), D(X_sz);
    for (int i = 0; i < A_sz; ++i) {
        C[i] = A[i];
    }
    for (int i = 0; i < B_sz; ++i) {
        D[i] = B[i];
    }
    modulo_transform<T, MODULUS, PRIM>(C);
    modulo_transform<T, MODULUS, PRIM>(D);
    for (int i = 0; i < X_sz; ++i) {
        C[i] *= D[i];    
    }
    modulo_transform<T, MODULUS, PRIM>(C, true);
    modulo_int<MODULUS> sz_inv = ~modulo_int<MODULUS>(X_sz);
    std::vector<T> result(result_sz);
    for (int i = 0; i < result_sz; ++i) {
        result[i] = static_cast<T>((C[i] * sz_inv)());
    }
    return result;
}

int main() {

    int N;
    scanf("%d", &N);

    std::vector<int> A(N), B(N);
    for (int i = 0; i < N; ++i) {
        scanf("%d %d", &A[i], &B[i]);
    }

    std::vector<int> answer = convolute<int, 1224736769, 3>(A, B);
    puts("0");
    for (int i = 0; i < N + N - 1; ++i) {
        printf("%d\n", answer[i]);
    }

}

Submission Info

Submission Time
Task C - 高速フーリエ変換
User KoD
Language C++14 (GCC 5.4.1)
Score 100
Code Size 4581 Byte
Status AC
Exec Time 127 ms
Memory 7168 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:125:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
                    ^
./Main.cpp:129:37: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &A[i], &B[i]);
                                     ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 33
Set Name Test Cases
Sample 00_sample_01
All 00_sample_01, 01_00_01, 01_01_19, 01_02_31, 01_03_22, 01_04_31, 01_05_40, 01_06_15, 01_07_39, 01_08_28, 01_09_30, 01_10_23, 01_11_33, 01_12_11, 01_13_28, 01_14_41, 01_15_26, 01_16_49, 01_17_34, 01_18_02, 01_19_33, 01_20_29, 02_00_51254, 02_01_82431, 02_02_17056, 02_03_34866, 02_04_6779, 02_05_65534, 02_06_65535, 02_07_65536, 02_08_65537, 02_09_65538, 02_10_100000
Case Name Status Exec Time Memory
00_sample_01 AC 1 ms 256 KB
01_00_01 AC 1 ms 256 KB
01_01_19 AC 1 ms 256 KB
01_02_31 AC 1 ms 256 KB
01_03_22 AC 1 ms 256 KB
01_04_31 AC 1 ms 256 KB
01_05_40 AC 1 ms 256 KB
01_06_15 AC 1 ms 256 KB
01_07_39 AC 1 ms 256 KB
01_08_28 AC 1 ms 256 KB
01_09_30 AC 1 ms 256 KB
01_10_23 AC 1 ms 256 KB
01_11_33 AC 1 ms 256 KB
01_12_11 AC 1 ms 256 KB
01_13_28 AC 1 ms 256 KB
01_14_41 AC 1 ms 256 KB
01_15_26 AC 1 ms 256 KB
01_16_49 AC 1 ms 256 KB
01_17_34 AC 1 ms 256 KB
01_18_02 AC 1 ms 256 KB
01_19_33 AC 1 ms 256 KB
01_20_29 AC 1 ms 256 KB
02_00_51254 AC 61 ms 3712 KB
02_01_82431 AC 121 ms 7040 KB
02_02_17056 AC 27 ms 1920 KB
02_03_34866 AC 56 ms 3584 KB
02_04_6779 AC 7 ms 640 KB
02_05_65534 AC 66 ms 3840 KB
02_06_65535 AC 66 ms 3840 KB
02_07_65536 AC 66 ms 3840 KB
02_08_65537 AC 115 ms 6912 KB
02_09_65538 AC 116 ms 6912 KB
02_10_100000 AC 127 ms 7168 KB