QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#143330 | #7021. Counting Sheep in Ami Dongsuo | Sorting | WA | 1ms | 4072kb | C++20 | 3.9kb | 2023-08-21 07:00:09 | 2023-08-21 07:00:12 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector<int> vi;
#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()
int test_id ;
const int MAXN = 1e4 + 7 ;
const int MOD = 1e9 + 7 ;
const int MAXW = 402 ;
const ll mod = 1000000007; // faster if const
typedef complex<double> C;
typedef vector<double> vd;
void fft(vector<C>& a) {
int n = sz(a), L = 31 - __builtin_clz(n);
static vector<complex<long double>> R(2, 1);
static vector<C> rt(2, 1); // (^ 10% faster if double)
for (static int k = 2; k < n; k *= 2) {
R.resize(n); rt.resize(n);
auto x = polar(1.0L, acos(-1.0L) / k);
rep(i,k,2*k) rt[i] = R[i] = i&1 ? R[i/2] * x : R[i/2];
}
vi rev(n);
rep(i,0,n) rev[i] = (rev[i / 2] | (i & 1) << L) / 2;
rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
for (int k = 1; k < n; k *= 2)
for (int i = 0; i < n; i += 2 * k) rep(j,0,k) {
// C z = rt[j+k] * a[i+j+k]; // (25% faster if hand-rolled) /// include-line
auto x = (double *)&rt[j+k], y = (double *)&a[i+j+k]; /// exclude-line
C z(x[0]*y[0] - x[1]*y[1], x[0]*y[1] + x[1]*y[0]); /// exclude-line
a[i + j + k] = a[i + j] - z;
a[i + j] += z;
}
}
vd conv(const vd& a, const vd& b) {
if (a.empty() || b.empty()) return {};
vd res(sz(a) + sz(b) - 1);
int L = 32 - __builtin_clz(sz(res)), n = 1 << L;
vector<C> in(n), out(n);
copy(all(a), begin(in));
rep(i,0,sz(b)) in[i].imag(b[i]);
fft(in);
for (C& x : in) x *= x;
rep(i,0,n) out[i] = in[-i & (n - 1)] - conj(in[i]);
fft(out);
rep(i,0,sz(res)) res[i] = imag(out[i]) / (4 * n);
return res;
}
int n , m , lim ;
int a[ MAXN ] ;
vector < int > v[ MAXN ] ;
vector < ll > dp[ MAXN ] ;
ll ans[ MAXW ] ;
void dfs ( int x ) {
dp[ x ].resize ( lim + 1 ) ;
for ( int i = 0 ; i <= lim ; ++ i ) { dp[ x ][ i ] = 0 ; }
if ( a[ x ] <= lim ) {
dp[ x ][ a[ x ] ] = 1 ;
}
for ( auto y : v[ x ] ) {
dfs ( y ) ;
for ( int j = 0 ; j <= lim ; ++ j ) {
dp[ x ][ j ] = ( dp[ x ][ j ] + dp[ y ][ j ] ) % MOD ;
}
}
}
ll fastpow ( ll x , ll pw ) {
ll ret = 1 ;
while ( pw > 0 ) {
if ( ( pw % 2 ) == 0 ) {
x = ( x * x ) % MOD ;
pw /= 2 ;
}
else {
ret = ( ret * x ) % MOD ;
-- pw ;
}
}
return ret ;
}
void solve ( ) {
++ test_id ;
cout << "Case #" << test_id << ": " ;
cin >> n >> m >> lim ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
v[ i ].clear ( ) ;
dp[ i ].clear ( ) ;
}
for ( int i = 0 ; i <= 3 * lim ; ++ i ) { ans[ i ] = 0 ; }
for ( int i = 1 , x , y ; i <= m ; ++ i ) {
cin >> x >> y ;
v[ x ].push_back ( y ) ;
}
dfs ( 1 ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
vd one , two , three ;
two.resize ( 2 * lim + 1 , 0 ) ;
three.resize ( 3 * lim + 1 , 0 ) ;
for ( int j = 0 ; j <= lim ; ++ j ) {
one.push_back ( dp[ i ][ j ] ) ;
two[ 2 * j ] = dp[ i ][ j ] ;
three[ 3 * j ] = dp[ i ][ j ] ;
}
vd wtf = conv ( one , one ) ;
vd aux = conv ( wtf , one ) ;
vd sub = conv ( two , one ) ;
for ( int j = 0 ; j <= 3 * lim ; ++ j ) {
ll add = roundl ( aux[ j ] - 3 * sub[ j ] + 2 * three[ j ] ) ;
while ( add < 0 ) { add += MOD ; }
ans[ j ] = ( ans[ j ] + add ) % MOD ;
}
}
ll inv = fastpow ( 6 , MOD - 2 ) ;
for ( int i = 0 ; i <= 3 * lim ; ++ i ) {
cout << ( ans[ i ] * inv ) % MOD << " " ;
}
cout << "\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: 0
Wrong Answer
time: 1ms
memory: 4072kb
input:
2 4 3 4 1 2 3 4 1 2 1 3 1 4 4 6 4 1 2 3 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
Case #1: 0 0 0 0 0 0 1 1 1 1 0 0 0 Case #2: 0 0 0 0 0 0 2 5 9 16 11 13 4
result:
wrong answer 1st lines differ - expected: 'Case #1: 0 0 0 0 0 1 1 1 1 0 0 0', found: 'Case #1: 0 0 0 0 0 0 1 1 1 1 0 0 0 '