UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190638#3383. 排列计数wzj333001002131ms17044kbC++4.7kb2023-10-06 17:40:462023-10-06 18:37:27

answer

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
// 1->close(c), 0->open(o)
#define r11(i, l, r) for (int i = (l); i <= (r); ++i)
#define p11(i, l, r) for (int i = (r); i >= (l); --i)
#define r10(i, l, r) for (int i = (l); i < (r); ++i)
#define p10(i, l, r) for (int i = (r)-1; i >= (l); --i)
// #define r01(i, l, r) for (int i = (l) + 1; i <= (r); ++i)
// #define p01(i, l, r) for (int i = (r); i > (l); --i)
#define r1n(i, n) r11(i, 1, n)
#define r0n(i, n) r10(i, 0, n)
#define p1n(i, n) p11(i, 1, n)
#define p0n(i, n) p10(i, 0, n)
#define FOR(i, s, e, step) for (int i = s; i <= e; i += step)
#define fora(i, v) for (auto i : (v))
#define fori(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define all(v) (v).begin(), (v).end()
#define all_(v, n) (v) + 1, (v) + (n) + 1
const ld eps = 1e-10;
const ld pi = acosl(-1.0);
const ll mod = 1e9 + 7;
template <typename T>
void chmin(T& a, T b) {
    a = min(a, b);
}
template <typename T>
void chmax(T& a, T b) {
    a = max(a, b);
}

// mod should be <2^31
struct modint {
    int n;
    modint() : n(0) { ; }
    modint(ll m) {
        if (m < 0 || mod <= m) {
            m %= mod;
            if (m < 0) m += mod;
        }
        n = m;
    }
    operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) {
    a.n += b.n;
    if (a.n >= mod) a.n -= (int)mod;
    return a;
}
modint operator-=(modint& a, modint b) {
    a.n -= b.n;
    if (a.n < 0) a.n += (int)mod;
    return a;
}
modint operator*=(modint& a, modint b) {
    a.n = ((ll)a.n * b.n) % mod;
    return a;
}
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
    if (n == 0) return modint(1);
    modint res = (a * a) ^ (n / 2);
    if (n % 2) res = res * a;
    return res;
}

ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); }
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) {
    a = a / b;
    return a;
}

/*
A001100
Let T{n, k} = number of permutations of 12...n with exactly k rising or falling successions. 
Let S[n](t) = Sum_{k >= 0} T{n, k}*t^k. Then S[0] = 1; S[1] = 1; S[2] = 2*t; S[3] = 4*t+2*t^2; 
for n >= 4, S[n] = (n+1-t)*S[n-1] - (1-t)*(n-2+3*t)*S[n-2] - (1-t)^2*(n-5+t)*S[n-3] + (1-t)^3*(n-3)*S[n-4].
S[n] = (n+1-t)*S[n-1] - ((-3*t^2)+(5-n)*t+n-2)*S[n-2] - (t^3+(n-7)*t^2+(11-2*n)*t+n-5)*S[n-3] + ((3-n)*t^3+(3*n-9)*t^2+(9-3*n)*t+n-3)*S[n-4]
*/
modint T[2010][2010];
void init(int n) {
    T[0][0] = 1;
    T[1][0] = 1;
    T[2][1] = 2;
    T[3][1] = 4;
    T[3][2] = 2;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= i; j++) {
            modint k = T[i][j];
#define n (i + 1)
            if (n >= 4) T[n][j] += (modint)(n + 1) * k, T[n][j + 1] += -k;
#undef n
#define n (i + 2)
            if (n >= 4)
                T[n][j + 2] -= (modint)-3 * k,
                    T[n][j + 1] -= (modint)(5 - n) * k,
                    T[n][j] -= (modint)(n - 2) * k;
#undef n
#define n (i + 3)
            if (n >= 4)
                T[n][j + 3] -= k, T[n][j + 2] -= (modint)(n - 7) * k,
                    T[n][j + 1] -= (modint)(11 - 2 * n) * k,
                    T[n][j] -= (modint)(n - 5) * k;
#undef n
#define n (i + 4)
            if (n >= 4)
                T[n][j + 3] += (modint)(3 - n) * k,
                    T[n][j + 2] += (modint)(3 * n - 9) * k,
                    T[n][j + 1] += (modint)(9 - 3 * n) * k,
                    T[n][j] += (modint)(n - 3) * k;
#undef n
        }
    }
    // for (int i = 1; i <= 10; i++) {
    //     for (int j = 0; j <= 10; j++) {
    //         cerr << T[i][j] << " ";
    //     }
    //     cerr << endl;
    // }
}
#define MULTIPLE_TEST_CASES 1
inline void _sol() {
    int n, m;
    cin >> n >> m;
    cout << T[n][m] << '\n';
}
// signed main() {
int main() {
    // freopen(".in", "r",stdin);
    // freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    init(2000);
#ifdef MULTIPLE_TEST_CASES
    int t;
    cin >> t;
    while (t--) {
        _sol();
    }
#else
    _sol();
#endif
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 183ms
memory: 17032kb

input:

10
2 0
5 1
6 3
3 0
7 5
4 2
1 0
8 7
5 0
5 2

output:

0
40
120
0
28
10
1
2
14
48

result:

ok 10 numbers

Test #2:

score: 10
Accepted
time: 186ms
memory: 17044kb

input:

5000
7 1
6 0
6 4
10 5
5 2
5 2
1 0
9 4
3 0
4 0
1 0
1 0
5 3
9 5
6 0
10 5
2 0
1 0
10 6
7 4
6 1
7 0
4 2
...

output:

1580
90
22
54304
48
48
1
22120
0
2
1
1
16
4448
90
54304
0
1
7900
226
230
646
10
5242
2
1
256
120
256...

result:

ok 5000 numbers

Test #3:

score: 10
Accepted
time: 179ms
memory: 17036kb

input:

10
16 4
15 2
16 12
16 13
15 10
16 6
15 9
16 7
16 12
16 12

output:

581566967
460233251
68526
2710
928874
17532712
12781476
855005425
68526
68526

result:

ok 10 numbers

Test #4:

score: 10
Accepted
time: 188ms
memory: 17040kb

input:

5000
17 8
16 14
9 4
11 7
17 11
16 2
17 16
12 5
17 2
13 2
8 0
11 0
17 2
9 6
12 11
16 8
14 6
16 11
11 ...

output:

138966533
82
22120
12816
35340456
318111669
2
9086628
199486786
840969777
5242
5296790
199486786
540...

result:

ok 5000 numbers

Test #5:

score: 10
Accepted
time: 191ms
memory: 17044kb

input:

5000
1456 1455
1129 1128
1740 1739
1993 1991
13 12
1666 1665
1624 1622
324 323
1343 1341
517 515
764...

output:

2
2
2
11944
2
2
9730
2
8044
3088
2
2
2
2
2
2062
2
2
11194
2872
2
10906
8602
5014
9394
10276
2746
727...

result:

ok 5000 numbers

Test #6:

score: 10
Accepted
time: 186ms
memory: 17044kb

input:

5000
1512 1511
576 574
1001 999
17 15
62 60
321 319
1074 1073
679 678
1349 1346
479 478
1357 1354
46...

output:

2
3442
5992
88
358
1912
2
2
30781680
2
31148776
3622548
19814766
2
4654
3591226
9304
2
2
2
2
5870010...

result:

ok 5000 numbers

Test #7:

score: 10
Accepted
time: 183ms
memory: 17044kb

input:

5000
21 5
536 92
1923 1468
228 34
669 473
1877 191
1590 1240
595 283
1592 455
1431 143
412 394
1854 ...

output:

54209947
315711882
882079680
796859827
485176182
390915536
687425576
912171587
682756769
305725812
7...

result:

ok 5000 numbers

Test #8:

score: 10
Accepted
time: 188ms
memory: 17044kb

input:

5000
1289 510
1643 1246
1972 433
1980 1339
1882 1658
1748 948
1806 846
1944 1005
1941 134
1903 1216
...

output:

962754391
439704646
245586907
147985120
588126189
365471651
611789713
636487942
288153152
837841585
...

result:

ok 5000 numbers

Test #9:

score: 10
Accepted
time: 326ms
memory: 17040kb

input:

500000
425 285
1647 880
164 116
1369 1116
1448 1096
1619 744
507 247
1446 612
1111 882
1522 202
1574...

output:

872154174
396303714
583079896
887549697
727232216
329089713
362175302
526719073
427715444
511414460
...

result:

ok 500000 numbers

Test #10:

score: 10
Accepted
time: 321ms
memory: 17040kb

input:

500000
1497 392
1808 47
1786 254
1987 1279
1473 312
1723 662
1830 594
1456 319
2000 1392
1561 412
18...

output:

383156697
721088138
292394234
159045024
685313232
643610915
477741929
257583674
732843624
78339813
2...

result:

ok 500000 numbers