QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77118 | #2573. Two Permutations | Sorting | WA | 2ms | 3580kb | C++ | 2.1kb | 2023-02-13 04:05:29 | 2023-02-13 04:05:30 |
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 emp = i , half = ( n - i ) - j , both = n - 2 * half - j ;
// matching
if ( both >= 1 ) {
dp[ nxt ][ sm + i ][ j + 1 ] += dp[ prv ][ sm ][ j ] * both ;
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 ] * both * ( both - 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 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3384kb
input:
2 4
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
3 7
output:
12
result:
ok 1 number(s): "12"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3456kb
input:
4 10
output:
24
result:
ok 1 number(s): "24"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3436kb
input:
4 14
output:
96
result:
ok 1 number(s): "96"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
5 10
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 2ms
memory: 3488kb
input:
5 15
output:
120
result:
ok 1 number(s): "120"
Test #7:
score: -100
Wrong Answer
time: 2ms
memory: 3580kb
input:
5 21
output:
6240
result:
wrong answer 1st numbers differ - expected: '2400', found: '6240'