QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77112 | #2573. Two Permutations | Sorting | WA | 2ms | 3476kb | C++ | 2.1kb | 2023-02-13 03:43:24 | 2023-02-13 03:43:25 |
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 ;
// 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 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3372kb
input:
2 4
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3428kb
input:
3 7
output:
12
result:
ok 1 number(s): "12"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3476kb
input:
4 10
output:
24
result:
ok 1 number(s): "24"
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 3420kb
input:
4 14
output:
288
result:
wrong answer 1st numbers differ - expected: '96', found: '288'