結果

問題 No.931 Multiplicative Convolution
ユーザー milanis48663220milanis48663220
提出日時 2021-03-20 01:20:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,346 bytes
コンパイル時間 1,387 ms
コンパイル使用メモリ 118,444 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-19 04:36:52
合計ジャッジ時間 5,889 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'bool ok(int, int)':
main.cpp:181:1: warning: control reaches end of non-void function [-Wreturn-type]
  181 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll MOD = 998244353;

class ModInt{
    public:
    ll v;
    ModInt(ll _v = 0){
        if(_v >= MOD) _v %= MOD;
        v = _v;
    }
    ModInt operator+(ll n){
        return ModInt((v+n)%MOD);
    }
    ModInt operator-(ll n){
        return ModInt((v-n+MOD)%MOD);
    }
    ModInt operator*(ll n){
        if(n >= MOD) n %= MOD;
        return ModInt((v*n)%MOD);
    }
    ModInt operator/(ll n){
        return ModInt((ModInt(n).inv()*v).v%MOD);
    }

    void operator+=(ll n){
        v = (v+n)%MOD;
    }
    void operator-=(ll n){
        v = (v-n+MOD)%MOD;
    }
    void operator*=(ll n){
        v = (v*n+MOD)%MOD;
    }
    
    
    ModInt operator+(ModInt n){
        return ModInt((v+n.v)%MOD);
    }
    ModInt operator-(ModInt n){
        return ModInt((v-n.v+MOD)%MOD);
    }
    ModInt operator*(ModInt n){
        return ModInt((v*n.v)%MOD);
    }
    ModInt operator/(ModInt n){
        return ModInt((n.inv()*v).v%MOD);
    }

    void operator+=(ModInt n){
        v = (v+n.v)%MOD;
    }
    void operator-=(ModInt n){
        v = (v-n.v+MOD)%MOD;
    }
    void operator*=(ModInt n){
        v = (v*n.v)%MOD;
    }

    void operator=(ModInt n){
        v = n.v;
    }
    bool operator==(ModInt n){
        return v == n.v;
    }
    bool operator!=(ModInt n){
        return v != n.v;
    }
    void operator=(ll n){
        v = n%MOD;
    }

    ModInt inv(){
        if(v == 1) return ModInt(1);
        else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
    }
};

ostream& operator<<(ostream& os, const ModInt& m){
    os << m.v;
    return os;
}

istream & operator >> (istream &in,  ModInt &m){
    in >> m.v;
    return in;
}

#define root 3
    
unsigned int add(const unsigned int x, const unsigned int y){
    return (x + y < MOD) ? x + y : x + y - MOD;
}
    
unsigned int sub(const unsigned int x, const unsigned int y){
    return (x >= y) ? (x - y) : (MOD - y + x);
}
    
unsigned int mul(const unsigned int x, const unsigned int y){
    return (unsigned long long)x * y % MOD;
}
    
unsigned int mod_pow(unsigned int x, unsigned int n){
    unsigned int res = 1;
    while(n > 0){
        if(n & 1){ res = mul(res, x); }
        x = mul(x, x);
        n >>= 1;
    }
    return res;
}
    
unsigned int inverse(const unsigned int x){
    return mod_pow(x, MOD - 2);
}
    
void ntt(vector<int>& a, const bool rev = false){
    unsigned int i, j, k, l, p, q, r, s;
    const unsigned int size = a.size();
    if(size == 1) return;
    vector<int> b(size);
    r = rev ? (MOD - 1 - (MOD - 1) / size) : (MOD - 1) / size;
    s = mod_pow(root, r);
    vector<unsigned int> kp(size / 2 + 1, 1);
    for(i = 0; i < size / 2; ++i) kp[i + 1] = mul(kp[i], s);
    for(i = 1, l = size / 2; i < size; i <<= 1, l >>= 1){
        for(j = 0, r = 0; j < l; ++j, r += i){
            for(k = 0, s = kp[i * j]; k < i; ++k){
                p = a[k + r], q = a[k + r + size / 2];
                b[k + 2 * r] = add(p, q);
                b[k + 2 * r + i] = mul(sub(p, q), s);
            }
        }
        swap(a, b);
    }
    if(rev){
        s = inverse(size);
        for(i = 0; i < size; i++){ a[i] = mul(a[i], s); }
    }
}
    
vector<int> convolute(const vector<int>& a, const vector<int>& b){
    const int size = (int)a.size() + (int)b.size() - 1;
    int t = 1;
    while(t < size){ t <<= 1; }
    vector<int> A(t, 0), B(t, 0);
    for(int i = 0; i < (int)a.size(); i++){ A[i] = a[i]; }
    for(int i = 0; i < (int)b.size(); i++){ B[i] = b[i]; }
    ntt(A), ntt(B);
    for (int i = 0; i < t; i++){ A[i] = mul(A[i], B[i]); }
    ntt(A, true);
    A.resize(size);
    return A;
}

bool ok(int p, int x){
    vector<bool> used(p, false);
    int cur = 1;
    for(int i = 1; i < p; i++){
        cur = (cur*x)%p;
        if(used[cur]) return false;
        used[cur] = true;
    }
}

int primitive_root(int p){
    for(int i = 1; i < p; i++){
        if(ok(p, i)) return i;
    }
    throw "unchi";
}

using mint = ModInt;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int p; cin >> p;
    vector<int> a(p), b(p);
    for(int i = 1; i < p; i++) cin >> a[i];
    for(int i = 1; i < p; i++) cin >> b[i];
    int r = primitive_root(p);
    vector<int> log_r(p);
    int cur = 1;
    for(int i = 0; i < p-1; i++){
        log_r[cur] = i;
        cur = (cur*r)%p;
    }
    vector<int> u(p-1), v(p-1);
    for(int i = 1; i < p; i++){
        u[log_r[i]] = a[i];
        v[log_r[i]] = b[i];
    }
    auto prod = convolute(u, v);
    vector<int> c(p);
    
    for(int i = 1; i < p; i++){
        c[i] += prod[log_r[i]];
        c[i] += prod[log_r[i]+p-1];
        c[i] %= MOD;
    }
    for(int i = 1; i < p; i++) cout << c[i] << ' ';
    cout << endl;
}
0