結果

問題 No.2770 Coupon Optimization
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-11-17 23:57:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 458 ms / 3,000 ms
コード長 4,512 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 238,772 KB
実行使用メモリ 40,340 KB
最終ジャッジ日時 2024-05-08 15:28:36
合計ジャッジ時間 9,881 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 447 ms
40,336 KB
testcase_04 AC 444 ms
40,332 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 438 ms
40,340 KB
testcase_07 AC 441 ms
40,340 KB
testcase_08 AC 444 ms
40,332 KB
testcase_09 AC 104 ms
12,452 KB
testcase_10 AC 219 ms
21,464 KB
testcase_11 AC 193 ms
20,220 KB
testcase_12 AC 236 ms
22,876 KB
testcase_13 AC 212 ms
21,276 KB
testcase_14 AC 452 ms
40,340 KB
testcase_15 AC 458 ms
40,268 KB
testcase_16 AC 442 ms
40,316 KB
testcase_17 AC 455 ms
40,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

map<int, int> _root={{998244353,3},{924844033,5}};

template<ll modc>
class modint {
    ll x;
public:
    modint(ll x=0) : x((x%modc+modc)%modc) {}
    modint operator-() const { return modint(-x);}
    modint& operator+=(const modint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    modint& operator-=(const modint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    modint& operator*=(const  modint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    modint operator+(const modint& a) const {
        modint res(*this);
        return res+=a;
    }
    modint operator-(const modint& a) const {
        modint res(*this);
        return res-=a;
    }
    modint operator*(const modint& a) const {
        modint res(*this);
        return res*=a;
    }
    modint pow(ll t) const {
        if (!t) return 1;
        modint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    modint inv() const {return pow(modc-2);}
    modint& operator/=(const modint& a){ return (*this) *= a.inv();}
    modint operator/(const modint& a) const {
        modint res(*this);
        return res/=a;
    }
    bool operator == (const modint& a) const{ return x == a.x;}
    bool operator != (const modint& a) const{ return x != a.x;}
    friend ostream& operator<<(ostream& os, const modint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, modint &m) {
        ll t;
        ip >> t;
        m = modint(t);
        return ip;
    }
    ll val(){ return x;}
    ll mod(){ return modc;}
};

template<typename T>
vector<T> ntt(vector<T> a, bool inv = false){
    T _x;
    ll N = a.size(), m = 0, modc=_x.mod(), q = (modc-1)/N, root=_root[modc];

    for (int i=0; (1<<i)<N; i++) m++;

    for (int i=0; i<N; i++){
        int j=0;
        for (int k=0; k<m; k++) j |= (i>>k & 1) << (m-1-k);
        if (i < j) swap(a[i], a[j]);
    }

    T g = (inv ? T(root).pow(q).inv() : T(root).pow(q));
    vector<T> w(m+1);
    w[m] = g;
    for (int i=m-1; i>=1; i--) w[i] = w[i+1] * w[i+1];

    for (int b=1, i=1; b<N; b*=2, i++){
        for (int k=0; k<N; k += b*2){
            T ww = 1;
            for (int j=0; j<b; j++){
                T s = a[j+k], t = a[j+k+b] * ww;
                a[j+k] = s + t;
                a[j+k+b] = s - t;
                ww *= w[i];
            }
        }
    }

    if (inv){
        T Ninv = T(N).inv();
        for (int i=0; i<N; i++) a[i] *= Ninv;
    }
    return a;
}

template<typename T>
vector<T> convolution(vector<T> a, vector<T> b){
    int s = a.size() + b.size() - 1;
    int t = 1;
    while(t < s) t *= 2;
    a.resize(t); b.resize(t);
    vector<T> A = ntt(a); //DFT
    vector<T> B = ntt(b); //DFT

    for (int i=0; i<t; i++) A[i] *= B[i];
    A = ntt(A, true); //IDFT

    return A;
}

tuple<ll, ll, ll> ext_gcd(ll a, ll b){
    if (b == 0) return make_tuple(a, 1, 0);

    ll g, x, y;
    tie(g, y, x) = ext_gcd(b, a % b);
    y -= a / b * x;
    return make_tuple(g, x, y);
}

ll mod_inv(ll a, ll m){
    assert(gcd(a, m) == 1);
    ll x, y, g;
    tie(g, x, y) = ext_gcd(a, m);
    return (x%m + m) % m;
}

pair<ll, ll> CRT(vector<ll> &b, vector<ll> &m){
    ll r=0, lcm=1;
    for (int i=0; i<b.size(); i++){
        ll p, q, g;
        tie(g, p, q) = ext_gcd(lcm, m[i]);
        if ((b[i]-r) % g != 0) return make_pair(-1, 0); //No solution
        r += lcm * ((b[i]-r) / g * p % (m[i]/g));
        lcm *= m[i]/g;
    }

    return make_pair(((r%lcm)+lcm)%lcm, lcm);
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    const ll mod1 = 998244353, mod2 = 924844033;
    using mint1 = modint<mod1>;
    using mint2 = modint<mod2>;
    ll N, M, x, y;
    cin >> N >> M;
    vector<ll> A(N), B(max(N, M), 100);
    for (int i=0; i<N; i++){
        cin >> A[i];
        A[i] /= 100;
    }
    for (int i=0; i<M; i++){
        cin >> B[i];
        B[i] = 100-B[i];
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());

    vector<mint1> a1(N), b1(N), c1;
    vector<mint2> a2(N), b2(N), c2;
    for (int i=0; i<N; i++) a1[i] = A[i], a2[i] = A[i], b1[i] = B[i], b2[i] = B[i];
    c1 = convolution(a1, b1);
    c2 = convolution(a2, b2);
    for (int i=0; i<N; i++){
        vector<ll> b={c1[i].val(), c2[i].val()}, m={mod1, mod2};
        tie(x, y) = CRT(b, m);
        cout << x << '\n';
    }

    return 0;
}
0