CSES - Putka Open 2020 – 2/5 - Results
Submission details
Task:Planeetat
Sender:tykkipeli
Submission time:2020-09-27 03:43:21 +0300
Language:C++11
Status:READY
Result:49
Feedback
groupverdictscore
#1ACCEPTED24
#2ACCEPTED25
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s1, 2, 3details
#3ACCEPTED0.01 s1, 2, 3details
#4ACCEPTED0.01 s1, 2, 3details
#5ACCEPTED0.01 s1, 2, 3details
#6ACCEPTED0.01 s1, 2, 3details
#7ACCEPTED0.01 s1, 2, 3details
#8ACCEPTED0.01 s1, 2, 3details
#9ACCEPTED0.01 s1, 2, 3details
#10ACCEPTED0.01 s1, 2, 3details
#11ACCEPTED0.01 s2, 3details
#12ACCEPTED0.01 s2, 3details
#13ACCEPTED0.03 s2, 3details
#14ACCEPTED0.84 s3details
#15--3details
#16--3details
#17--3details
#18--3details
#19--3details
#20--3details
#21--3details
#22--3details
#23--3details
#24--3details
#25--3details

Compiler report

input/code.cpp: In function 'std::vector<int> mull(const std::vector<int>&, const std::vector<int>&, const FFT_mod&)':
input/code.cpp:62:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i = 0; i < left.size(); i++) {
                    ~~^~~~~~~~~~~~~
input/code.cpp: In function 'int main()':
input/code.cpp:250:8: warning: unused variable 'lol2' [-Wunused-variable]
     ll lol2[100];
        ^~~~

Code

#include <bits/stdc++.h>
#include <vector>
#include <utility>
#include <stdio.h>
using namespace std;
 
typedef long long ll;

ll M = 1e9 + 7;
ll ans[10101];
ll lol[10101];
ll fact[10101];
ll dp[5005][5005];
ll pot[5005][5005];
ll modinv[5005];
ll modinvfact[5005];
ll ncr[5005][5005];

template <typename T>
T extGcd(T a, T b, T& x, T& y) {
   if (b == 0) {
      x = 1;
      y = 0;
      return a;
   }
   else {
      int g = extGcd(b, a % b, y, x);
      y -= a / b * x;
      return g;
   }
}

template <typename T>
T modInv(T a, T m) {
   T x, y;
   extGcd(a, m, x, y);
   return (x % m + m) % m;
}

long long crt(const std::vector< std::pair<int, int> >& pp, int mod = -1);


#include <algorithm>


struct FFT_mod {
   int mod, root, root_1, root_pw;
};

extern FFT_mod suggested_fft_mods[5];
void ntt_shortmod(std::vector<int>& a, bool invert, const FFT_mod& mod_data);


const int mod = 1000000007;

vector<int> mull(const vector<int>& left, const vector<int>& right, const FFT_mod& mod_data) {
   vector<int> left1 = left, right1 = right;
   ntt_shortmod(left1, false, mod_data);
   ntt_shortmod(right1, false, mod_data);

   for (int i = 0; i < left.size(); i++) {
      left1[i] = (left1[i] * 1ll * right1[i]) % mod_data.mod;
   }

   ntt_shortmod(left1, true, mod_data);
   return left1;
}


vector<int> mult(vector<int>& left, vector<int>& right) {
   int ssss = left.size() + right.size() - 1;
   int pot2;
   for (pot2 = 1; pot2 < ssss; pot2 <<= 1);

   left.resize(pot2);
   right.resize(pot2);

   vector<int> res[3];
   for (int i = 0; i < 3; i++) {
      res[i] = mull(left, right, suggested_fft_mods[i]);
   }

   vector<int> ret(pot2);
   for (int i = 0; i < pot2; i++) {
      vector< pair<int,int> > mod_results;
      for (int j = 0; j < 3; j++) {
         mod_results.emplace_back(res[j][i], suggested_fft_mods[j].mod);
      }
      ret[i] = crt(mod_results, mod);
   }
   return ret;
}

long long crt(const std::vector< std::pair<int, int> >& a, int mod) {
   long long res = 0;
   long long mult = 1;

   int SZ = a.size();
   std::vector<int> x(SZ);
   for (int i = 0; i<SZ; ++i) {
      x[i] = a[i].first;
      for (int j = 0; j<i; ++j) {
         long long cur = (x[i] - x[j]) * 1ll * modInv(a[j].second,a[i].second);
         x[i] = (int)(cur % a[i].second);
         if (x[i] < 0) x[i] += a[i].second;
      }
      res = (res + mult * 1ll * x[i]);
      mult = (mult * 1ll * a[i].second);
      if (mod != -1) {
         res %= mod;
         mult %= mod;
      }
   }

   return res;
}


FFT_mod suggested_fft_mods[] = {
   { 7340033, 5, 4404020, 1 << 20 },
   { 415236097, 73362476, 247718523, 1<<22 },
   { 463470593, 428228038, 182429, 1<<21},
   { 998244353, 15311432, 469870224, 1 << 23 },
   { 918552577, 86995699, 324602258, 1 << 22 }
};

int FFT_w[1050000];
int FFT_w_dash[1050000];


void ntt_shortmod(std::vector<int>& a, bool invert, const FFT_mod& mod_data) {
   // only use if mod < 5*10^8
   int n = (int)a.size();
   int mod = mod_data.mod;

   for (int i = 1, j = 0; i<n; ++i) {
      int bit = n >> 1;
      for (; j >= bit; bit >>= 1)
         j -= bit;
      j += bit;
      if (i < j)
         std::swap(a[i], a[j]);
   }

   for (int len = 2; len <= n; len <<= 1) {
      int wlen = invert ? mod_data.root_1 : mod_data.root;
      for (int i = len; i<mod_data.root_pw; i <<= 1)
         wlen = int(wlen * 1ll * wlen % mod_data.mod);

      long long tt = wlen;
      for (int i = 1; i < len / 2; i++) {
         FFT_w[i] = tt;
         FFT_w_dash[i] = (tt << 31) / mod;
         int q = (FFT_w_dash[1] * 1ll * tt) >> 31;
         tt = (wlen * 1ll * tt - q * 1ll * mod) & ((1LL << 31) - 1);
         if (tt >= mod) tt -= mod;
      }
      for (int i = 0; i<n; i += len) {
         int uu = a[i], vv = a[i + len / 2] % mod;
         if (uu >= 2*mod) uu -= 2*mod;
         a[i] = uu + vv;
         a[i + len / 2] = uu - vv + 2 * mod;

         for (int j = 1; j<len / 2; ++j) {
            int u = a[i + j];
            if (u >= 2*mod) u -= 2*mod;
            int q = (FFT_w_dash[j] * 1ll * a[i + j + len / 2]) >> 31;
            int v = (FFT_w[j] * 1ll * a[i + j + len / 2] - q * 1ll * mod) & ((1LL << 31) - 1);
            a[i + j] = u + v;
            a[i + j + len / 2] = u - v + 2*mod;
         }
      }
   }
   if (invert) {
      int nrev = modInv(n, mod);
      for (int i = 0; i<n; ++i)
         a[i] = int(a[i] * 1ll * nrev % mod);
   }
}

ll binpow(ll a, ll b, ll m) {
    if (b == -1) return binpow(a,M-2,M);
    ll ans = 1;
    while (b > 0) {
        if (b&1) ans = ans*a%m;
        a = a*a%m;
        b >>= 1;
    }
    return ans;
}

ll nCr(ll n, ll k) {
    ll ans = fact[n];
    ans *= binpow(fact[k], M-2, M);
    ans %= M;
    ans *= binpow(fact[n-k], M-2, M);
    ans %= M;
    return ans;
}

void preCalcFact(int n) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++) {
        fact[i] = fact[i-1]*i;
        fact[i] %= M;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    preCalcFact(n);
    //preCalcPot:
    for (int i = 1; i <= n; i++) {
        pot[i][0] = 1;
        for (int j = 1; j <= n; j++) {
            pot[i][j] = pot[i][j-1]*i;
            if (pot[i][j] >= M) pot[i][j] %= M;
        }
    }
    //preCalcModInv:
    for (int i = 1; i <= n; i++) {
        modinv[i] = binpow(i,M-2,M);
    }
    //preCaclModInvFact:
    for (int i = 1; i <= n; i++) {
        modinvfact[i] = binpow(fact[i],M-2,M);
    }
    modinvfact[0] = 1;
    //preCalcNcr:
    
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= i; j++) {
            if (i == j) {
                ncr[i][j] = 1;
                continue;
            }
            ll ans = fact[i];
            ans *= modinvfact[j];
            if (ans >= M) ans %= M;
            ans *= modinvfact[i-j];
            if (ans >= M) ans %= M;
            ncr[i][j] = ans;
        }
    }
    
    ll lol2[100];
    for (int i = 1; i <= n; i++) {
        for (int j = 1 ; j <= i; j++) {
            //lol[i] += ((nCr(i,j)*fact[j])%M)*binpow(i,i-j-1,M);
            if (i-j-1 >= 0) {
                lol[i] += ((ncr[i][j]*fact[j])%M)*pot[i][i-j-1];
            } else {
                lol[i] += ((ncr[i][j]*fact[j])%M)*modinv[i];
            }
            if (lol[i] >= M) lol[i] %= M;
            /*
            ll x = ((nCr(i,j)*fact[j])%M)*binpow(i,i-j-1,M);
            lol2[i] += x;
            lol2[i] %= M;
            if (lol2[i] != lol[i]) {
                cout << "ERROR!" << endl;
                cout << "i: " << i << "j: " << j << endl;
                cout << pot[i][i-j-1] << endl;
                cout << ((ncr[i][j]*fact[j])%M)*pot[i][i-j-1] << endl;
                cout << x << endl;
                exit(0);
            }
            
            if (ncr[i][j] != nCr(i,j)) {
                cout << "ERROR!" << endl;
                cout << "i: " << i << "j: " << j << endl;
                cout << ncr[i][j] << " " << nCr(i,j) << endl;
                exit(0);
            }
            */
            //cout << "i: " << i << "j: " << j << endl;
            //cout << ncr[i][j] << " " << nCr(i,j) << endl;
            //cout << binpow(i, i-j-1,M) << " " << pot[i][i-j-1] << endl;
        }
    }
    dp[0][0] = 1;
    
    for (int i = 1; i <= n; i++) dp[i][1] = lol[i];
    for (int j = 2; j <= n; j++) {
        vector<int> f,g;
        for (int k = 1; k <= n-j+1; k++) {
            //f.push_back((binpow(fact[k-1],M-2,M)*lol[k])%M);
            //g.push_back((dp[j-2+k][j-1]*binpow(fact[j-2+k],M-2,M))%M);
            ll val = modinvfact[k-1]*lol[k];
            if (val >= M) val %=M;
            f.push_back(val);
            val = dp[j-2+k][j-1]*modinvfact[j-2+k];
            if (val >= M) val %= M;
            g.push_back(val);
        }
        auto p = mult(f,g);
        for (int i = j; i <= n; i++) dp[i][j] = (((ll)p[i-j])*fact[i-1])%M;
        /*
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                cout << dp[a][b] << " ";
            }
            cout << endl;
        }
        cout << endl;
        */
    }
    for (int i = 1; i <= n; i++) cout << dp[n][i] << "\n";
    
    //for (int i = 1; i <= n; i++) cout << lol[i] << " ";
    //cout << endl;
}


















Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
1

correct output
1

user output
1

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
2

correct output
3
1

user output
3
1

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
3

correct output
17
9
1

user output
17
9
1

Test 4

Group: 1, 2, 3

Verdict: ACCEPTED

input
4

correct output
142
95
18
1

user output
142
95
18
1

Test 5

Group: 1, 2, 3

Verdict: ACCEPTED

input
5

correct output
1569
1220
305
30
1

user output
1569
1220
305
30
1

Test 6

Group: 1, 2, 3

Verdict: ACCEPTED

input
6

correct output
21576
18694
5595
745
45
...

user output
21576
18694
5595
745
45
...

Test 7

Group: 1, 2, 3

Verdict: ACCEPTED

input
7

correct output
355081
334369
113974
18515
1540
...

user output
355081
334369
113974
18515
1540
...

Test 8

Group: 1, 2, 3

Verdict: ACCEPTED

input
8

correct output
6805296
6852460
2581964
484729
49840
...

user output
6805296
6852460
2581964
484729
49840
...

Test 9

Group: 1, 2, 3

Verdict: ACCEPTED

input
9

correct output
148869153
158479488
64727522
13591116
1632099
...

user output
148869153
158479488
64727522
13591116
1632099
...

Test 10

Group: 1, 2, 3

Verdict: ACCEPTED

input
10

correct output
660215659
85349908
783995053
409987640
55545735
...

user output
660215659
85349908
783995053
409987640
55545735
...

Test 11

Group: 2, 3

Verdict: ACCEPTED

input
20

correct output
8033007
474885151
998010619
720259168
345757330
...

user output
8033007
474885151
998010619
720259168
345757330
...

Test 12

Group: 2, 3

Verdict: ACCEPTED

input
50

correct output
637699856
613177596
194234103
50828885
988168359
...

user output
637699856
613177596
194234103
50828885
988168359
...

Test 13

Group: 2, 3

Verdict: ACCEPTED

input
100

correct output
894456323
406549429
962038245
430640330
61348310
...

user output
894456323
406549429
962038245
430640330
61348310
...

Test 14

Group: 3

Verdict: ACCEPTED

input
666

correct output
189730587
968711879
553374698
53051125
139917248
...

user output
189730587
968711879
553374698
53051125
139917248
...

Test 15

Group: 3

Verdict:

input
3333

correct output
79235821
455292218
627100211
591681254
695866885
...

user output
(empty)

Test 16

Group: 3

Verdict:

input
4991

correct output
482116496
245260697
151422537
180441123
318466624
...

user output
(empty)

Test 17

Group: 3

Verdict:

input
4992

correct output
141010647
787351178
684701591
872974815
631476284
...

user output
(empty)

Test 18

Group: 3

Verdict:

input
4993

correct output
504034249
588971460
281533415
928250892
416697844
...

user output
(empty)

Test 19

Group: 3

Verdict:

input
4994

correct output
266134603
90079109
544661648
812099750
17249410
...

user output
(empty)

Test 20

Group: 3

Verdict:

input
4995

correct output
833898560
663839791
109127071
321675160
86285359
...

user output
(empty)

Test 21

Group: 3

Verdict:

input
4996

correct output
721158645
167929822
115103278
491345159
114397872
...

user output
(empty)

Test 22

Group: 3

Verdict:

input
4997

correct output
691405606
436947443
82656395
514529009
783319673
...

user output
(empty)

Test 23

Group: 3

Verdict:

input
4998

correct output
829675470
688714502
189351950
956110193
20883331
...

user output
(empty)

Test 24

Group: 3

Verdict:

input
4999

correct output
343936737
47032567
190931571
827280581
160866637
...

user output
(empty)

Test 25

Group: 3

Verdict:

input
5000

correct output
364064601
633559852
352848841
666954216
428009512
...

user output
(empty)