QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#226875#5411. 杏仁chenxinyang200610 2972ms273824kbC++145.9kb2023-10-26 17:34:572023-10-26 17:34:58

Judging History

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

  • [2023-10-26 17:34:58]
  • 评测
  • 测评结果:10
  • 用时:2972ms
  • 内存:273824kb
  • [2023-10-26 17:34:57]
  • 提交

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;
}
Z fact[1000005],ifac[1000005],inv[1000005];

Z C(int N,int M){
    if(N < M || M < 0) return 0;
    return fact[N] * ifac[M] * ifac[N - M];
}

void init(int L){
    fact[0] = 1;
    rep(i,1,L) fact[i] = fact[i - 1] * i;
    ifac[L] = 1 / fact[L];
    per(i,L - 1,0) ifac[i] = ifac[i + 1] * (i + 1);
    rep(i,1,L) inv[i] = ifac[i] * fact[i - 1];
}
int n,m,N,s,t,q;
int val[25][25],id[25];
Z a[25],b[25];
Z ext;

Z dp[1 << 20][20];

Z f[21][1 << 20],g[21][1 << 20],res[1 << 20];
void FWT(Z *F){
    rep(i,0,n - 1){
        rep(S,0,(1 << n) - 1) if((S >> i) & 1) F[S] += F[S - (1 << i)];
    }
}

void iFWT(Z *F){
    rep(i,0,n - 1){
        rep(S,0,(1 << n) - 1) if((S >> i) & 1) F[S] -= F[S - (1 << i)];
    }
}

Z F[21],G[21],ans[21];
int main(){
//    freopen("test.in","r",stdin);
    scanf("%d%d%d%d",&n,&m,&s,&t);
    init(n);
    N = 0;
    rep(u,1,n){
        if(u == s || u == t) continue;
        id[u] = N++;
    }

    rep(i,1,m){
        int u,v;
        scanf("%d%d",&u,&v);
        if(u == s && v == t){
            ext++;
            continue;
        }
        if(v == s || u == t) continue;

        if(u == s) a[id[v]]++;
        else if(v == t) b[id[u]]++;
        else val[id[u]][id[v]]++;
    }

    n = N;
    rep(S,1,(1 << n) - 1){
        if(popcnt(S) == 1){
            int u = __lg(S);
            dp[S][u] = b[u];
            f[popcnt(S)][S] += a[u] * b[u];
            continue;
        }
        Z temp = 0;
        rep(u,0,n - 1){
            if(!((S >> u) & 1)) continue;
            int R = S - (1 << u);
            rep(v,0,n - 1) if((R >> v) & 1) dp[S][u] += val[u][v] * dp[R][v];
            temp += a[u] * dp[S][u];
        }
        f[popcnt(S)][S] += temp;
//        printf("%d %d\n",S,temp.val());
    }
/*    printf("A:\n");
    rep(i,0,n - 1) printf("%d ",a[i].val());
    printf("\n");
    printf("B:\n");
    rep(i,0,n - 1) printf("%d ",b[i].val());
    printf("\n");
    rep(u,0,n - 1){
        rep(v,0,n - 1) printf("%d ",val[u][v]);
        printf("\n");
    }*/
    rep(i,1,n) FWT(f[i]);

    rep(S,0,(1 << n) - 1){
        rep(i,0,n) F[i] = f[i][S];
        G[0] = 1;
        rep(i,0,n - 1){
            G[i + 1] = 0;
            rep(j,0,i) G[i + 1] += (j + 1) * F[j + 1] * G[i - j];
            G[i + 1] *= inv[i + 1];
        }
        rep(i,0,n) g[i][S] = G[i];
    }
    rep(i,1,n) iFWT(g[i]);
    rep(S,0,(1 << n) - 1) res[S] = g[popcnt(S)][S];

    FWT(res);
    rep(S,0,(1 << n) - 1){
        rep(u,0,n - 1) ans[u] += dp[S][u] * a[u] * res[(1 << n) - 1 - S];
    }

    scanf("%d",&q);
    rep(i,1,q){
        int u;
        scanf("%d",&u);
        if(u == t) printf("%d\n",(res[(1 << n) - 1] * ext).val());
        else printf("%d\n",(ans[id[u]] * (ext + 1)).val());
    }
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 10
Accepted
time: 7ms
memory: 273548kb

input:

5 10
1 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
4
2
3
4
5

output:

20
14
10
15

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 12ms
memory: 273708kb

input:

2 0
1 2
0

output:


result:

ok 0 lines

Test #3:

score: -10
Wrong Answer
time: 8ms
memory: 273572kb

input:

4 20
1 4
1 4
2 4
1 4
2 4
1 1
3 4
3 1
2 4
1 3
1 3
3 2
2 3
4 1
3 1
2 3
1 4
3 2
4 4
3 1
1 4
4
2
4
3
2

output:

0
60
70
0

result:

wrong answer 2nd lines differ - expected: '225', found: '60'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 10
Accepted

Test #15:

score: 10
Accepted
time: 79ms
memory: 273796kb

input:

18 324
9 8
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
4 11
...

output:

256104819
256104819
238677015
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819
256104819

result:

ok 18 lines

Test #16:

score: 0
Accepted
time: 367ms
memory: 273540kb

input:

20 400
13 20
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
3 19
3 20
4 1
4 2
4 3
4 ...

output:

681008066
681008066
681008066
681008066
681008066
681008066
681008066
681008066
205106617
681008066
681008066
681008066
681008066
681008066
681008066
681008066
681008066
681008066
681008066
681008066

result:

ok 20 lines

Test #17:

score: 0
Accepted
time: 779ms
memory: 273576kb

input:

21 441
16 17
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
2 21
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
3 19
3 20
3 21...

output:

689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
689426186
319626977
689426186
689426186
689426186
689426186
689426186
689426186
689426186

result:

ok 21 lines

Test #18:

score: 0
Accepted
time: 1718ms
memory: 273796kb

input:

22 484
10 1
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
2 14
2 15
2 16
2 17
2 18
2 19
2 20
2 21
2 22
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
3 11
3 12
3 13
3 14
3 15
3 16
3 17
3 18
3 19
...

output:

761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899
761174899

result:

ok 16 lines

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Wrong Answer

Test #23:

score: 0
Wrong Answer
time: 2972ms
memory: 273824kb

input:

22 9384
9 19
9 19
3 18
9 17
9 19
15 19
14 17
9 18
9 14
9 18
9 19
16 19
3 10
9 2
2 1
21 10
1 19
4 3
19 3
9 16
18 21
9 22
9 8
13 8
2 7
4 16
9 12
15 12
3 19
19 10
16 10
14 19
19 16
14 19
1 4
11 5
5 13
3 19
9 6
17 22
4 18
1 13
2 18
19 16
18 17
12 22
21 3
5 11
18 7
14 19
8 19
9 20
16 19
22 19
18 12
9 19
...

output:

596857653

result:

wrong answer 1st lines differ - expected: '667211711', found: '596857653'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%