QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#258982#1262. Justice For Everyonechenxinyang2006AC ✓573ms4268kbC++145.7kb2023-11-20 15:08:222023-11-20 15:08:23

Judging History

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

  • [2023-11-20 15:08:23]
  • 评测
  • 测评结果:AC
  • 用时:573ms
  • 内存:4268kb
  • [2023-11-20 15:08:22]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
    if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
    if(x > y) x = y;
}

inline int popcnt(int x){
    return __builtin_popcount(x);
}

inline int ctz(int x){
    return __builtin_ctz(x);
}

template <int P>
class mod_int
{
    using Z = mod_int;

private:
    static int mo(int x) { return x < 0 ? x + P : x; }

public:
    int x;
    int val() const { return x; }
    mod_int() : x(0) {}
    template <class T>
    mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
    bool operator==(const Z &rhs) const { return x == rhs.x; }
    bool operator!=(const Z &rhs) const { return x != rhs.x; }
    Z operator-() const { return Z(x ? P - x : 0); }
    Z pow(long long k) const
    {
        Z res = 1, t = *this;
        while (k)
        {
            if (k & 1)
                res *= t;
            if (k >>= 1)
                t *= t;
        }
        return res;
    }
    Z &operator++()
    {
        x < P - 1 ? ++x : x = 0;
        return *this;
    }
    Z &operator--()
    {
        x ? --x : x = P - 1;
        return *this;
    }
    Z operator++(int)
    {
        Z ret = x;
        x < P - 1 ? ++x : x = 0;
        return ret;
    }
    Z operator--(int)
    {
        Z ret = x;
        x ? --x : x = P - 1;
        return ret;
    }
    Z inv() const { return pow(P - 2); }
    Z &operator+=(const Z &rhs)
    {
        (x += rhs.x) >= P && (x -= P);
        return *this;
    }
    Z &operator-=(const Z &rhs)
    {
        (x -= rhs.x) < 0 && (x += P);
        return *this;
    }
    Z operator-()
    {
        return -x;
    }
    Z &operator*=(const Z &rhs)
    {
        x = 1ULL * x * rhs.x % P;
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                  \
    friend T operator o(const Z &lhs, const Z &rhs) \
    {                                               \
        Z res = lhs;                                \
        return res o## = rhs;                       \
    }
    setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
    
    friend istream& operator>>(istream& is, mod_int& x)
    {
        long long tmp;
        is >> tmp;
        x = tmp;
        return is;
    }
    friend ostream& operator<<(ostream& os, const mod_int& x)
    {
        os << x.val();
        return os;
    }
};

using Z = mod_int<mod>;
Z power(Z p,ll k){
    Z ans = 1;
    while(k){
        if(k % 2 == 1) ans *= p;
        p *= p;
        k /= 2;
    }
    return ans;
}
int n,m,sum;
int a[35],b[35];
Z fact[6005],ifac[6005];
void init(int L){
    fact[0] = 1;
    rep(i,1,L) fact[i] = fact[i - 1] * i;
    ifac[L] = Z(1) / fact[L];
    per(i,L - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
}
Z val[205],P[205],M[35][35];
Z det(){
    Z ans = 1,iv,rr;
    rep(j,1,n){
        int pos = -1;
        rep(i,j,n) if(M[i][j].val()) pos = i;
        if(pos == -1) return 0;
        if(pos != j){
            ans *= -1;
            rep(k,1,n) swap(M[j][k],M[pos][k]);
        }
        iv = Z(1) / M[j][j];
        rep(i,j + 1,n){
            rr = M[i][j] * iv;
            rep(k,1,n) M[i][k] -= rr * M[j][k];
        }
    } 
    rep(i,1,n) ans *= M[i][i];
    return ans;
}
Z dp[15][6005],res[6005],ans[6005];
void add(int d,int p){
    per(i,sum / 2,0){
        dp[d][i + 1] += dp[d][i];
        dp[d][i] *= -p;
    }
}

void cdq(int l,int r,int d){
    if(l == r){
        Z prd = 1;
        rep(i,0,sum / 2) if(i != l) prd *= l - i;
        prd = Z(1) / prd * res[l];
        rep(i,0,sum / 2) ans[i] += dp[d][i] * prd;
        return;
    }
    int mid = (l + r) >> 1;
    rep(i,0,sum / 2) dp[d + 1][i] = dp[d][i];
    rep(i,mid + 1,r) add(d + 1,i);
    cdq(l,mid,d + 1);

    rep(i,0,sum / 2) dp[d + 1][i] = dp[d][i];
    rep(i,l,mid) add(d + 1,i);
    cdq(mid+1,r,d + 1);
}

int main(){
//    freopen("test.in","r",stdin);
    scanf("%d",&n);
    rep(i,1,n) scanf("%d",&a[i]);
    rep(i,1,n) scanf("%d",&b[i]);
    sum = 0;
    rep(i,1,n){
        sum += b[i] - a[i];
        if(a[i] > b[i]){
            printf("0\n");
            return 0;
        }
        chkmax(m,b[i]);
    }
    if(sum % 2){
        printf("0\n");
        return 0;
    }
    init(sum);
    rep(i,1,n){
        rep(j,i + 1,n){
            if((a[i] - a[j]) * (b[i] - b[j]) < 0){
                printf("0\n");
                return 0;
            }
        }
    }

    rep(p,0,sum / 2){
        P[0] = 1;
        rep(i,1,m) P[i] = -P[i - 1] * p;

        rep(i,0,m){
            val[i] = 0;
            rep(j,0,i / 2) val[i] += P[j] * ifac[j] * ifac[i - 2 * j];
        }
        rep(i,1,n){
            rep(j,1,n){
                if(b[j] >= a[i]) M[i][j] = val[b[j] - a[i]];
                else M[i][j] = 0;
            }
        }
        res[p] = det();
    }

    dp[0][0] = 1;
    cdq(0,sum / 2,0);
    Z answer = 0;
    rep(i,0,sum / 2) answer += fact[sum / 2] * ifac[sum / 2 - i] * fact[sum - 2 * i] * ans[i];
    answer /= power(2,sum / 2);
    printf("%d\n",answer.val());
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4056kb

input:

3
1 2 3
3 4 5

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 1ms
memory: 4016kb

input:

3
1 2 3
7 8 9

output:

42

result:

ok answer is '42'

Test #3:

score: 0
Accepted
time: 1ms
memory: 4044kb

input:

3
1 4 7
3 6 9

output:

6

result:

ok answer is '6'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3964kb

input:

1
1
3

output:

0

result:

ok answer is '0'

Test #5:

score: 0
Accepted
time: 1ms
memory: 4028kb

input:

1
1
1

output:

1

result:

ok answer is '1'

Test #6:

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

input:

2
1 4
4 7

output:

1

result:

ok answer is '1'

Test #7:

score: 0
Accepted
time: 1ms
memory: 3988kb

input:

10
10 9 8 7 6 1 2 3 4 5
11 12 13 114 115 120 129 128 127 126

output:

0

result:

ok answer is '0'

Test #8:

score: 0
Accepted
time: 1ms
memory: 4020kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 131

output:

0

result:

ok answer is '0'

Test #9:

score: 0
Accepted
time: 197ms
memory: 4076kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

output:

936606510

result:

ok answer is '936606510'

Test #10:

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

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199

output:

0

result:

ok answer is '0'

Test #11:

score: 0
Accepted
time: 17ms
memory: 4072kb

input:

30
16 21 33 44 51 60 71 81 91 100 110 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 199
110 111 112 113 114 115 116 117 118 119 120 122 131 144 155 160 162 171 172 174 177 179 187 188 189 191 192 193 194 200

output:

836228983

result:

ok answer is '836228983'

Test #12:

score: 0
Accepted
time: 20ms
memory: 4024kb

input:

10
10 9 8 7 6 5 4 3 2 1
110 109 108 107 106 105 104 103 102 101

output:

422463757

result:

ok answer is '422463757'

Test #13:

score: 0
Accepted
time: 573ms
memory: 4268kb

input:

30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

output:

575061951

result:

ok answer is '575061951'