QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91975#6137. Sub-cycle Graphsycheng#AC ✓107ms87868kbC++145.1kb2023-03-30 02:30:352023-03-30 02:30:36

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-30 02:30:36]
  • 评测
  • 测评结果:AC
  • 用时:107ms
  • 内存:87868kb
  • [2023-03-30 02:30:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define int long long
using poly = vector<LL>;
const int mod = 1e9 + 7;

namespace Conv {
#define ld long double
    const int _ = 1 << 19 | 1;
    const ld pi = acos(-1);
    struct comp  {
        ld x, y; comp(ld _x = 0, ld _y = 0): x(_x), y(_y){}
        friend comp operator +(comp  p, comp q)  {
            return comp(p.x + q.x, p.y + q.y);
        }
        friend comp operator -(comp  p, comp q)  {
            return comp(p.x - q.x, p.y - q.y);
        }
        friend comp operator *(comp  p, comp q)  {
            return comp(p.x * q.x - p.y * q.y, p.x * q.y + p.y * q.x);
        }
        friend comp operator /(comp  p, ld q)  {
            return comp(p.x / q, p.y / q);
        }
        friend comp operator ~(comp p)  {
            return comp(p.x, -p.y);
        }
    }A[_], B[_], C[_], D[_];
    int N, M, P;

        comp w[_];
        int dir[_], need;
        void init(int len) {
            need = 1; while(need < len) need <<= 1;
            for (int i = 1; i < need; ++ i) dir[i] = (dir[i >> 1] >> 1) | (i & 1 ? need >> 1 : 0);
            for (int i = 1; i < need; i <<= 1) {
                w[i] = comp(1, 0); comp wn(cos(pi / i), sin(pi / i));
                for (int j = 1; j < i; j ++ ) {
                    w[i + j] = w[i + j - 1] * wn;
                }
            }
        }

        void DFT(comp *A, int t) {
            for (int i = 1; i < need; ++ i) {
                if(i < dir[i]) swap(A[i], A[dir[i]]);
            }
            for (int i = 1; i < need; i <<= 1) {
                for (int j = 0; j < need; j += i << 1) {
                    for (int k = 0; k < i; k ++ ) {
                        comp x = A[j + k], y = A[i + j + k] * w[i + k];
                        A[j + k] = x + y, A[i + j + k] = x - y;
                    }
                }
            }
            if(t == -1) {reverse(A + 1, A + need); for (int i = 0; i < need; i ++ ) A[i] = A[i] / need;}
        }

        void DFT(comp *A, comp  *B) {
            static comp P[_], Q[_];
            for (int i = 0; i < need; i ++ ) P[i] = A[i] + B[i] * comp(0, 1);
            DFT(P, 1);
            Q[0] = ~P[0];
            for (int i = 1; i < need; ++ i) Q[i] = ~P[need - i];
            for (int i = 0; i < need; ++ i) {
                A[i] = (P[i] + Q[i]) / 2; B[i] = (P[i] - Q[i]) * comp(0, -0.5);
            }
        }

        void IDFT(comp *A, comp *B){
            static comp P[_];
            for(int i = 0; i < need; ++i) P[i] = A[i] + B[i] * comp(0, 1);
            DFT(P, -1);
            for(int i = 0; i < need; ++i) {
                A[i] = comp(P[i].x, 0); B[i] = comp(P[i].y, 0);
            }
        }

        void Main(int n, int m, int p, int *a, int *b, int *ans){
            N = n; M = m; P = p;
            for(int i = 0; i < need; ++i) A[i] = B[i] = C[i] = D[i] = comp();
            for(int i = 0; i <= N; ++i) {
                int x = a[i]; A[i].x = x & 32767; B[i].x = x >> 15;
            }
            for(int i = 0; i <= M; ++i) {
                int x = b[i]; C[i].x = x & 32767; D[i].x = x >> 15;
            }
            init(N + M + 1); DFT(A, B); DFT(C, D);
            static comp A1[_], B1[_], C1[_], D1[_];
            for (int i = 0; i < need; i ++ ) {
                A1[i] = A[i] * C[i], B1[i] = A[i] * D[i], C1[i] = B[i] * C[i], D1[i] = B[i] * D[i];
            }
            IDFT(A1, B1); IDFT(C1, D1);
            for (int i = 0; i <= N + M; i ++ ) {
                ans[i] = ((LL)round(A1[i].x) + (((LL)round(B1[i].x) + (LL)round(C1[i].x)) % P) << 15) + (((LL)round(D1[i].x) % P) << 30) % P;
            }
        }
};
const int N = 1e5 + 10;
int fac[N], infac[N], inv[N];

int qmi(int a, int b) {
    int res = 1;
    while(b) {
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

void init() {
    inv[1] = 1;
    for (int i = 2; i < N; i ++ ) inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    infac[0] = fac[0] = 1;
    for(int i = 1; i < N; i ++ ) fac[i] = fac[i - 1] * i % mod;
    infac[N - 1] = qmi(fac[N - 1], mod - 2);
    for (int i = N - 2; i >= 0; i -- ) infac[i] = infac[i + 1] * (i + 1) % mod;
}

int C(int a, int b) {
    if(b > a) return 0;
    return fac[a] * infac[b] % mod * infac[a - b] % mod;
}

void solve() {
    LL ans = 0;
    int n, m; cin >> n >> m;
    if(m > n) {
        cout << 0 << endl;
        return ;
    }
    if(n == m) {
        cout << fac[n - 1] * inv[2] % mod << endl;
        return ;
    }

    int k = n - m;
    int cnt = min(m, n - m);
    int INV = 1;
    for (int i = 0; i <= cnt; i ++ ) {
        ans = (ans + C(k + m - i - 1, m - i) * C(k, i) % mod * INV % mod + mod) % mod;
        INV = INV * (mod - inv[2]) % mod;
    }
    ans = ans * fac[n] % mod * infac[k] % mod;
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    init();
    int T; cin >> T;
    while(T -- ) {
        solve();
    }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 15ms
memory: 87868kb

input:

3
4 2
4 3
5 3

output:

15
12
90

result:

ok 3 number(s): "15 12 90"

Test #2:

score: 0
Accepted
time: 107ms
memory: 87704kb

input:

17446
3 0
3 1
3 2
3 3
4 0
4 1
4 2
4 3
4 4
5 0
5 1
5 2
5 3
5 4
5 5
6 0
6 1
6 2
6 3
6 4
6 5
6 6
7 0
7 1
7 2
7 3
7 4
7 5
7 6
7 7
8 0
8 1
8 2
8 3
8 4
8 5
8 6
8 7
8 8
9 0
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
9 9
10 0
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
10 10
11 0
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11...

output:

1
3
3
1
1
6
15
12
3
1
10
45
90
60
12
1
15
105
375
630
360
60
1
21
210
1155
3465
5040
2520
360
1
28
378
2940
13545
35280
45360
20160
2520
1
36
630
6552
42525
170100
393120
453600
181440
20160
1
45
990
13230
114345
643545
2286900
4762800
4989600
1814400
181440
1
55
1485
24750
273735
2047815
10239075
3...

result:

ok 17446 numbers