QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#77119 | #2573. Two Permutations | Sorting | AC ✓ | 110ms | 14200kb | C++ | 2.1kb | 2023-02-13 04:06:49 | 2023-02-13 04:06:52 |
Judging History
answer
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector<int> vi;
typedef unsigned int uint ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MOD = 1e9 + 7 ;
const int MAXN = 107 ;
int n , k ;
ll dp[ 2 ][ MAXN * MAXN ][ MAXN ] ;
void solve ( ) {
cin >> n >> k ;
int prv = 0 , nxt = 1 ;
dp[ prv ][ 0 ][ 0 ] = 1 ;
for ( int i = n ; i >= 1 ; -- i ) {
for ( int sm = 0 ; sm <= k ; ++ sm ) {
for ( int j = 0 ; j <= n ; ++ j ) {
if ( dp[ prv ][ sm ][ j ] == 0 ) { continue ; }
ll half = ( n - i ) - j , emp = n - 2 * half - j ;
// matching
if ( emp >= 1 ) {
dp[ nxt ][ sm + i ][ j + 1 ] += dp[ prv ][ sm ][ j ] * emp ;
dp[ nxt ][ sm + i ][ j + 1 ] %= MOD ;
}
// both covered
if ( half >= 1 ) {
dp[ nxt ][ sm ][ j + 2 ] += dp[ prv ][ sm ][ j ] * half * half ;
dp[ nxt ][ sm ][ j + 2 ] %= MOD ;
}
// one side covered
if ( half >= 1 && emp >= 1 ) {
dp[ nxt ][ sm + i ][ j + 1 ] += 2 * dp[ prv ][ sm ][ j ] * half * emp ;
dp[ nxt ][ sm + i ][ j + 1 ] %= MOD ;
}
// both sides empty
if ( emp >= 2 ) {
dp[ nxt ][ sm + 2 * i ][ j ] += dp[ prv ][ sm ][ j ] * emp * ( emp - 1 ) ;
dp[ nxt ][ sm + 2 * i ][ j ] %= MOD ;
}
dp[ prv ][ sm ][ j ] = 0 ;
}
}
prv ^= 1 , nxt ^= 1 ;
}
cout << dp[ prv ][ k ][ n ] << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 3548kb
input:
2 4
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
3 7
output:
12
result:
ok 1 number(s): "12"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3416kb
input:
4 10
output:
24
result:
ok 1 number(s): "24"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3568kb
input:
4 14
output:
96
result:
ok 1 number(s): "96"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3428kb
input:
5 10
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3460kb
input:
5 15
output:
120
result:
ok 1 number(s): "120"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3424kb
input:
5 21
output:
2400
result:
ok 1 number(s): "2400"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
6 21
output:
720
result:
ok 1 number(s): "720"
Test #9:
score: 0
Accepted
time: 2ms
memory: 3528kb
input:
6 30
output:
25920
result:
ok 1 number(s): "25920"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
6 27
output:
106560
result:
ok 1 number(s): "106560"
Test #11:
score: 0
Accepted
time: 2ms
memory: 3944kb
input:
20 300
output:
644873710
result:
ok 1 number(s): "644873710"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
20 139
output:
0
result:
ok 1 number(s): "0"
Test #13:
score: 0
Accepted
time: 3ms
memory: 3976kb
input:
30 470
output:
491424690
result:
ok 1 number(s): "491424690"
Test #14:
score: 0
Accepted
time: 0ms
memory: 4164kb
input:
30 500
output:
8436035
result:
ok 1 number(s): "8436035"
Test #15:
score: 0
Accepted
time: 5ms
memory: 4720kb
input:
40 1000
output:
617159088
result:
ok 1 number(s): "617159088"
Test #16:
score: 0
Accepted
time: 2ms
memory: 4564kb
input:
40 900
output:
729805907
result:
ok 1 number(s): "729805907"
Test #17:
score: 0
Accepted
time: 7ms
memory: 5920kb
input:
60 1830
output:
16340084
result:
ok 1 number(s): "16340084"
Test #18:
score: 0
Accepted
time: 6ms
memory: 5964kb
input:
60 2000
output:
832198921
result:
ok 1 number(s): "832198921"
Test #19:
score: 0
Accepted
time: 58ms
memory: 10324kb
input:
100 5050
output:
437918130
result:
ok 1 number(s): "437918130"
Test #20:
score: 0
Accepted
time: 6ms
memory: 3640kb
input:
100 700
output:
0
result:
ok 1 number(s): "0"
Test #21:
score: 0
Accepted
time: 110ms
memory: 14200kb
input:
100 10000
output:
0
result:
ok 1 number(s): "0"