• [2022 杭电多校5] Count Set (生成函数 分治NTT)


    题意

    给定一个长度为 n n n 的排列 p p p { 1 , 2 , ⋯   , n } \{1, 2, \cdots, n\} {1,2,,n} 和一个非负整数 k k k,计算排列 p p p 中的子集 T T T,满足集合大小为 k k k T T T P ( T ) P(T) P(T) 没有交集, P ( T ) = { y ∣ y = p x , x ∈ T } P(T) = \{y \mid y= p_x,x \in T\} P(T)={yy=px,xT}

    分析:

    考虑将排列 p p p 看成图, i i i p i p_i pi 连边,会形成若干个环,那么原问题等价于从图中选出 k k k 个点且每个环中不能有相邻被选择的点的方案数。考虑构造每个环 i i i 的生成函数
    1 + f S i , 1 x + f S i , 2 x 2 + ⋯ + f S i , ⌊ S i 2 ⌋ x ⌊ S i 2 ⌋ 1 + f_{S_i,1}x + f_{S_i,2} x ^ 2 + \cdots + f_{S_i,\lfloor \frac{S_i}{2} \rfloor} x ^ {\lfloor \frac{S_i}{2} \rfloor} 1+fSi,1x+fSi,2x2++fSi,2Six2Si
    其中 S i S_i Si 表示环 i i i 的大小, f S i , j f_{S_i,j} fSi,j 表示大小为 S i S_i Si 的环中选出 j j j 个互不相邻的点的方案数,根据鸽巢原理,若 j > ⌊ S i 2 ⌋ j > \lfloor \dfrac{S_i}{2} \rfloor j>2Si,一定有两个点相邻,所以生成函数只需要取到 ⌊ S i 2 ⌋ \lfloor \dfrac{S_i}{2} \rfloor 2Si 项即可。那么答案就为
    [ x k ] ∏ i = 1 cnt ∑ j = 0 ⌊ S i 2 ⌋ f S i , j x j [x ^ k]\prod_{i = 1} ^ {\text{cnt}} \sum_{j = 0} ^ {\lfloor \frac{S_i}{2} \rfloor} f_{S_i,j}x^j [xk]i=1cntj=02SifSi,jxj
    cnt \text{cnt} cnt 为图中环的数量。那么现在考虑求出 f ( n , m ) f(n, m) f(n,m),即大小为 n n n 的环选出 m m m 个互不相邻的点的方案数。

    我们先考虑不是环的情况,也就是链式不相邻问题,那么可以先放 m m m 个被选择的球,考虑把中间 m − 1 m - 1 m1 个空放上一个不被选择的球,那么剩下 n − 2 × m − 1 n - 2\times m - 1 n2×m1 个球就可以随便放,问题就相当于有 m + 1 m + 1 m+1 个盒子,每个盒子可空的方案数,那么就是经典隔板法,方案数为 g ( n , m ) = ( n − 2 × m − 1 + m + 1 − 1 m + 1 − 1 ) = ( n − m + 1 m ) g(n, m) =\dbinom{n - 2\times m - 1 + m + 1 - 1}{m + 1 - 1}=\dbinom{n - m + 1}{m} g(n,m)=(m+11n2×m1+m+11)=(mnm+1)

    现在考虑是环的情况,假设对于环上一个点,有两种情况,若这个点被选择,则这个点的相邻点不能被选择,那么其他 n − 3 n - 3 n3 个点就是 g ( n − 3 , m − 1 ) g(n - 3, m - 1) g(n3,m1),若这个点不被选择,那么剩下 n − 1 n - 1 n1 个点就是 g ( n − 1 , m ) g(n - 1, m) g(n1,m),所以 f ( n , m ) = g ( n − 3 , m − 1 ) + g ( n − 1 , m ) = ( n − m − 1 m − 1 ) + ( n − m m ) f(n, m) = g(n - 3, m - 1) + g(n - 1, m) = \dbinom{n - m - 1}{m - 1} + \dbinom{n - m}{m} f(n,m)=g(n3,m1)+g(n1,m)=(m1nm1)+(mnm)

    代码:

    #include 
    using namespace std;
    using i64 = long long;
    constexpr int mod = 998244353;
    int norm(int x) {
        if (x < 0) {
            x += mod;
        }
        if (x >= mod) {
            x -= mod;
        }
        return x;
    }
    template<class T>
    T power(T a, int b) {
        T res = 1;
        for (; b; b /= 2, a *= a) {
            if (b % 2) {
                res *= a;
            }
        }
        return res;
    }
    struct Z {
        int x;
        Z(int x = 0) : x(norm(x)) {}
        int val() const {
            return x;
        }
        Z operator-() const {
            return Z(norm(mod - x));
        }
        Z inv() const {
            assert(x != 0);
            return power(*this, mod - 2);
        }
        Z &operator*=(const Z &rhs) {
            x = i64(x) * rhs.x % mod;
            return *this;
        }
        Z &operator+=(const Z &rhs) {
            x = norm(x + rhs.x);
            return *this;
        }
        Z &operator-=(const Z &rhs) {
            x = norm(x - rhs.x);
            return *this;
        }
        Z &operator/=(const Z &rhs) {
            return *this *= rhs.inv();
        }
        friend Z operator*(const Z &lhs, const Z &rhs) {
            Z res = lhs;
            res *= rhs;
            return res;
        }
        friend Z operator+(const Z &lhs, const Z &rhs) {
            Z res = lhs;
            res += rhs;
            return res;
        }
        friend Z operator-(const Z &lhs, const Z &rhs) {
            Z res = lhs;
            res -= rhs;
            return res;
        }
        friend Z operator/(const Z &lhs, const Z &rhs) {
            Z res = lhs;
            res /= rhs;
            return res;
        }
        friend istream &operator>>(istream &is, Z &a) {
            i64 v;
            is >> v;
            a = Z(v);
            return is;
        }
        friend ostream &operator<<(ostream &os, const Z &a) {
            return os << a.val();
        }
    };
    vector<int> rev;
    vector<Z> roots{0, 1};
    void dft(vector<Z> &a) {
        int n = a.size();
        if (int(rev.size()) != n) {
            int k = __builtin_ctz(n) - 1;
            rev.resize(n);
            for (int i = 0; i < n; i ++) {
                rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
            }
        }
        for (int i = 0; i < n; i ++) {
            if (rev[i] < i) {
                swap(a[i], a[rev[i]]);
            }
        }
        if (int(roots.size()) < n) {
            int k = __builtin_ctz(roots.size());
            roots.resize(n);
            while ((1 << k) < n) {
                Z e = power(Z(3), (mod - 1) >> (k + 1));
                for (int i = 1 << (k - 1); i < (1 << k); i ++) {
                    roots[i << 1] = roots[i];
                    roots[i << 1 | 1] = roots[i] * e;
                }
                k ++;
            }
        }
        for (int k = 1; k < n; k *= 2) {
            for (int i = 0; i < n; i += 2 * k) {
                for (int j = 0; j < k; j ++) {
                    Z u = a[i + j], v = a[i + j + k] * roots[k + j];
                    a[i + j] = u + v, a[i + j + k] = u - v;
                }
            }
        }
    }
    void idft(vector<Z> &a) {
        int n = a.size();
        reverse(a.begin() + 1, a.end());
        dft(a);
        Z inv = (1 - mod) / n;
        for (int i = 0; i < n; i ++) {
            a[i] *= inv;
        }
    }
    struct Poly {
        vector<Z> a;
        Poly() {}
        Poly(const vector<Z> &a) : a(a) {}
        Poly(const initializer_list<Z> &a) : a(a) {}
        int size() const {
            return a.size();
        }
        void resize(int n) {
            a.resize(n);
        }
        Z operator[](int idx) const {
            if (idx < size()) {
                return a[idx];
            } else {
                return 0;
            }
        }
        Z &operator[](int idx) {
            return a[idx];
        }
        Poly mulxk(int k) const {
            auto b = a;
            b.insert(b.begin(), k, 0);
            return Poly(b);
        }
        Poly modxk(int k) const {
            k = min(k, size());
            return Poly(vector<Z>(a.begin(), a.begin() + k));
        }
        Poly divxk(int k) const {
            if (size() <= k) {
                return Poly();
            }
            return Poly(vector<Z>(a.begin() + k, a.end()));
        }
        friend Poly operator+(const Poly &a, const Poly &b) {
            vector<Z> res(max(a.size(), b.size()));
            for (int i = 0; i < int(res.size()); i ++) {
                res[i] = a[i] + b[i];
            }
            return Poly(res);
        }
        friend Poly operator-(const Poly &a, const Poly &b) {
            vector<Z> res(max(a.size(), b.size()));
            for (int i = 0; i < int(res.size()); i ++) {
                res[i] = a[i] - b[i];
            }
            return Poly(res);
        }
        friend Poly operator*(Poly a, Poly b) {
            if (a.size() == 0 || b.size() == 0) {
                return Poly();
            }
            int sz = 1, tot = min(5000000, a.size() + b.size() - 1);
            while (sz < tot) {
                sz *= 2;
            }
            a.a.resize(sz);
            b.a.resize(sz);
            dft(a.a);
            dft(b.a);
            for (int i = 0; i < sz; i ++) {
                a.a[i] = a[i] * b[i];
            }
            idft(a.a);
            a.resize(tot);
            return a;
        }
        friend Poly operator*(Z a, Poly b) {
            for (int i = 0; i < int(b.size()); i ++) {
                b[i] *= a;
            }
            return b;
        }
        friend Poly operator*(Poly a, Z b) {
            for (int i = 0; i < int(a.size()); i ++) {
                a[i] *= b;
            }
            return a;
        }
        Poly &operator+=(Poly b) {
            return (*this) = (*this) + b;
        }
        Poly &operator-=(Poly b) {
            return (*this) = (*this) - b;
        }
        Poly &operator*=(Poly b) {
            return (*this) = (*this) * b;
        }
        Poly deriv() const {
            if (a.empty()) {
                return Poly();
            }
            vector<Z> res(size() - 1);
            for (int i = 0; i < size() - 1; i ++) {
                res[i] = (i + 1) * a[i + 1];
            }
            return Poly(res);
        }
        Poly integr() const {
            vector<Z> res(size() + 1);
            for (int i = 0; i < size(); i ++) {
                res[i + 1] = a[i] / (i + 1);
            }
            return Poly(res);
        }
        Poly inv(int m) const {
            Poly x{a[0].inv()};
            int k = 1;
            while (k < m) {
                k *= 2;
                x = (x * (Poly{2} - modxk(k) * x)).modxk(k);
            }
            return x.modxk(m);
        }
        Poly log(int m) const {
            return (deriv() * inv(m)).integr().modxk(m);
        }
        Poly exp(int m) const {
            Poly x{1};
            int k = 1;
            while (k < m) {
                k *= 2;
                x = (x * (Poly{1} - x.log(k) + modxk(k))).modxk(k);
            }
            return x.modxk(m);
        }
        Poly pow(int k, int m) const {
            int i = 0;
            while (i < size() && a[i].val() == 0) {
                i ++;
            }
            if (i == size() || 1LL * i * k >= m) {
                return Poly(vector<Z>(m));
            }
            Z v = a[i];
            auto f = divxk(i) * v.inv();
            return (f.log(m - i * k) * k).exp(m - i * k).mulxk(i * k) * power(v, k);
        }
        Poly sqrt(int m) const {
            Poly x{1};
            int k = 1;
            while (k < m) {
                k *= 2;
                x = (x + (modxk(k) * x.inv(k)).modxk(k)) * ((mod + 1) / 2);
            }
            return x.modxk(m);
        }
        Poly mulT(Poly b) const {
            if (b.size() == 0) {
                return Poly();
            }
            int n = b.size();
            reverse(b.a.begin(), b.a.end());
            return ((*this) * b).divxk(n - 1);
        }
    };
    vector<Z> fact, infact;
    void init(int n) {
        fact.resize(n + 1), infact.resize(n + 1);
        fact[0] = infact[0] = 1;
        for (int i = 1; i <= n; i ++) {
            fact[i] = fact[i - 1] * i;
        }
        infact[n] = fact[n].inv();
        for (int i = n; i; i --) {
            infact[i - 1] = infact[i] * i;
        }
    }
    Z C(int n, int m) {
        if (n < 0 || m < 0 || n < m) return Z(0);
        return fact[n] * infact[n - m] * infact[m];
    }
    struct DSU {
        vector<int> p, Size;
        DSU(int n) : p(n), Size(n, 1) {
            iota(p.begin(), p.end(), 0);
        }
        int find(int x) {
            return p[x] == x ? p[x] : p[x] = find(p[x]);
        }
        bool same(int u, int v) {
            return find(u) == find(v);
        }
        void merge(int u, int v) {
            u = find(u), v = find(v);
            if (u != v) {
                Size[v] += Size[u];
                p[u] = v;
            }
        }
    };
    void solve() {
        int n, k;
        cin >> n >> k;
        DSU p(n + 1);
        vector<int> a(n + 1);
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
            p.merge(i, a[i]);
        }
        vector<vector<Z>> f(n + 1);
        int cnt = 0;
        for (int i = 1; i <= n; i ++) {
            if (p.find(i) == i) {
                cnt ++;
                f[cnt].resize(p.Size[i] / 2 + 1);
                for (int j = 0; j <= p.Size[i] / 2; j ++) {
                    f[cnt][j] = C(p.Size[i] - j - 1, j - 1) + C(p.Size[i] - j, j);
                }
            }
        }
        function<Poly(int, int)> dc = [&](int l, int r) {
            if (l == r) return Poly(f[l]);
            int mid = l + r >> 1;
            return dc(l, mid) * dc(mid + 1, r);
        };
        Poly ans = dc(1, cnt);
        ans.resize(k + 1);
        cout << ans[k] << "\n";
    }
    signed main() {
        init(1e7);
        cin.tie(0) -> sync_with_stdio(0);
        int T;
        cin >> T;
        while (T --) {
            solve();
        }
    }
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 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
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 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
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
  • 相关阅读:
    u盘无法复制过大文件怎么解决?揭秘!
    青少年软件编程C++二级题库(51-60)
    MySQL查询性能优化七种武器之索引潜水
    快速掌握基本数据库查询语句,面试无烦恼!!
    android 快速实现 圆角矩形控件 及 圆形控件
    Mysql相关的各种类型文件
    拼接字符串和文件得到新的文件的命令
    【笔记】电商RFM模型
    终端导出mysql 下载
    rsyslog-日志管理 logrotate-日志轮转
  • 原文地址:https://blog.csdn.net/messywind/article/details/126137010