QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88654 | #5826. 错排 | Withery | 1 | 1011ms | 165692kb | C++14 | 8.6kb | 2023-03-16 20:49:34 | 2023-03-28 12:38:08 |
Judging History
answer
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216000")
using namespace std ;
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
else v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const { return static_cast<U>(value); }
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) { Modular result(*this); *this += 1; return result; }
Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (mod())
);
value = m;
#else
value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }
template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }
template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
/*
using ModType = int;
struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/
constexpr int md = 998244353 ;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
int step = 1000 ;
Mint dp0[220][200010] ;
Mint dp1[220][200010] ;
Mint fac[200010] ;
Mint ddiv[200010] ;
Mint f[200010] ;
Mint dfs0( int n ,int m ){
if( m < 0 ) return 0 ;
if( m == 0 ) return 1 ;
if( dp0[m/step][n] != 0 ) return dp0[m/step][n] ;
if( n == m + 1 ) return f[n] + f[n-1] ;
if( n == m ) return f[n] ;
// return dp0[n/500][m] = dfs0(n,m-1) * (n-2*m+1) + dfs0(n,m-2)*(n-m+2)*(m-1) ;
return dp0[m/step][n] = (dfs0(n-1,m)*(n+1) - dfs0(n-2,m)) * ddiv[n-m] ;
}
Mint dfs1( int n ,int m ){
if( m < 0 ) return 0 ;
if( m == 0 ) return 1 ;
if( dp1[m/step][n] != 0 ) return dp1[m/step][n] ;
if( n == m + 1 ) return f[n] + f[n-1] ;
if( n == m ) return f[n] ;
return dp1[m/step][n] = (dfs1(n-1,m)*(n+1) - dfs1(n-2,m)) * ddiv[n-m] ;
// return dp1[n/500][m] = dfs1(n,m-1) * (n-2*m+1) + dfs1(n,m-2)*(n-m+2)*(m-1) ;
}
/*
Mint dfs1( int n ,int m ){
if( m < 0 ) return 0 ;
if( m == 0 ) return 1 ;
if( tmp[n] != 0 ) return dp[n] ;
if( n == m + 1 ) return f[n] + f[n-1] ;
if( n == m ) return f[n] ;
return tmp[n] = (dfs1(n-1,m)*(n+1) - dfs1(n-2,m)) / (n-m) ;
}*/
int main(){
int n ,m ;
fac[0] = 1 ;
for( int i = 1 ;i <= 200000 ;i ++ )
fac[i] = fac[i-1] * i ;
for( int i = 2 ;i <= 200000 ;i ++ ) f[i] = f[i-1] * i + (i%2?-1:1) ;
for( int i = 1 ;i <= 200000 ;i ++ ) ddiv[i] = Mint(1)/i ;
for( int i = 0 ;i <= 200000 ;i += step ){
for( int j = i ;j <= 200000 ;j ++ )
dp0[i/step][j] = dfs0( j ,i ) ;
for( int j = i+1 ;j <= 200000 ;j ++ )
dp1[i/step][j] = dfs1( j ,i+1 ) ;
}
auto c = [&]( int a ,int b ){ return fac[a] / fac[b] / fac[a-b] ; } ;
int T ;
cin >> T ;
while( T -- ){
cin >> n >> m ;
Mint ans ,a0 ,a1 ;
int s = (n-m-m)/step ;
a0 = dp0[s][n-m] ;
a1 = dp1[s][n-m] ;
if( s * step == n - m - m ) ans = dp0[s][n-m] ;
if( s * step + 1 == n - m - m ) ans = dp1[s][n-m] ;
s = s*step+2 ;
for( int i = s ;i <= n-m-m ;i ++ ){
ans = a1 * ( n - m - 2 * i + 1 ) + a0 * ( n - m - i + 2 ) * ( i - 1 ) ;
a0 = a1 ;
a1 = ans ;
}
cout << ans * fac[m] * c(n-m,m) * fac[m] << endl ;
}
// cout << dfs( n-m ,n-m-m ) * fac[m] * c(n-m,m) * fac[m] << endl ;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 1
Accepted
Test #1:
score: 1
Accepted
time: 1011ms
memory: 165692kb
input:
0
output:
result:
ok 0 number(s): ""
Subtask #2:
score: 0
Dangerous Syscalls
Test #2:
score: 0
Dangerous Syscalls
input:
10 8 6 5 1 4 2 6 3 8 1 3 1 6 2 3 1 4 1 6 2
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #7:
score: 0
Time Limit Exceeded
input:
200000 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 0 60 0 61...
output:
0 1 2 9 44 265 1854 14833 133496 1334961 14684570 176214841 294304226 127281753 910981941 600290115 222488424 11814221 224470198 496426549 442513998 751108780 305347938 340640042 530046225 804025262 745550660 910531421 451058030 554564312 221339670 95158970 145512950 954462889 464137465 737039093 31...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #8:
score: 0
Time Limit Exceeded
input:
200000 4303 1473 1276 72 967 234 3619 984 1316 384 2679 50 4426 1744 3782 1179 4919 4 805 63 3933 158 1574 528 1277 435 3826 915 2739 68 2286 349 3017 527 3036 476 4280 1764 1504 686 4584 917 1379 145 4764 2178 1881 45 4808 1565 3663 165 4730 2209 2258 103 4181 1687 1636 770 4339 1173 2355 777 3201 ...
output:
855518783 202627962 284771116 596280162 111952425 28114068 922980998 483503998 478475869 42227903 210453242 82826277 349706660 478397018 588903665 672339856 911511930 783922264 224272260 199537336 659467844 383745708 953695418 668329703 880293299 649430530 916687905 550953325 295023552 141584429 871...
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%