Submission #738409

# Submission time Handle Problem Language Result Execution time Memory
738409 2023-05-08T17:03:23 Z Sam_a17 Zapina (COCI20_zapina) C++17
110 / 110
373 ms 2744 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
 
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
 
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt(x) __builtin_popcount(x)
 
#define pb push_back
#define popf pop_front
#define popb pop_back
 
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
 
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
 
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
 
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

int ka() {
	int x = 0;
	bool z = false;
	while (1)
	{
		char y = getchar();
		if (y == '-')
			z = true;
		else if ('0' <= y && y <= '9')
			x = x * 10 + y - '0';
		else
		{
			if (z)
				x *= -1;
			return x;
		}
	}
}

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();
 
  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// const long long mod = 1e9 + 7;
const int M = 351;
int dp[M][30][M];
int n;

namespace Cnk {
  const long long mod = 1e9 + 7;
  const int N = 400, maxN = N - 2;
  long long fact[N], ifact[N];

  long long binPowByMod(long long a, long long b) {
    a %= mod;
    long long res = 1;
    while (b > 0) {
      if (b & 1)
        res = res * a % mod;
      a = a * a % mod;
      b >>= 1;
    }
    return res;
  }
  
  long long choose (long long a, long long b) {
    return (((fact[a] * ifact[b]) % mod ) * ifact[a - b] ) % mod;
  }

  void generate() {
    fact[0] = fact[1] = 1;
    for(int i = 2; i < N; i++) {
      fact[i] = (fact[i - 1] * i) % mod;
    }
  
    ifact[maxN] = binPowByMod(fact[maxN], mod - 2);
    for(int i = maxN; i >= 1; i--) {
      ifact[i - 1] = (ifact[i] * i) % mod;
    }
  }
  
  long long modInverse(long long A, long long M) {
    long long m0 = M;
    long long y = 0, x = 1;

    if (M == 1) return 0;

    while (A > 1) {
      long long q = A / M;
      long long t = M;
      M = A % M, A = t;
      t = y;
      y = x - q * y;
      x = t;
    }
  
    if (x < 0) x += m0;
    return x;
  }
}

using namespace Cnk;

long long add(long long a, long long b) {
  return (a + b) % mod;
}

long long mult(long long a, long long b) {
  return (a * b) % mod;
}

long long sub(long long a, long long b) {
  return (a - b + 2 * mod) % mod;
}

void solve_() {
  cin >> n;

  dp[0][0][n] = 1;

  for(int i = 0; i < n; i++) {
    for(int j = 0; j <= 1; j++) {
      for(int k = 0; k <= n; k++) {
        if(!dp[i][j][k]) continue;
        for(int e = 0; e <= k; e++) {
          if(e == i + 1) {
            dp[i + 1][j | 1][k - e] = add(dp[i + 1][j | 1][k - e], mult(dp[i][j][k], choose(k, e)));
          } else {
            dp[i + 1][j][k - e] = add(dp[i + 1][j][k - e], mult(dp[i][j][k], choose(k, e)));
          }
        }
      }
    }
  }

  long long answ = 0;
  for(int i = 1; i < 2; i++) {
    answ += dp[n][i][0];
    if(answ >= mod) answ -= mod;
  }
  
  cout << answ << '\n';
}
 
int main() {
  setIO();

  generate();

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };
 
  int test_cases = 1;
  // cin >> test_cases;
  solve(test_cases);
 
  return 0;
} 

Compilation message

zapina.cpp: In function 'void setIO(std::string)':
zapina.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zapina.cpp:84:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 167 ms 2000 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 36 ms 1256 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 224 ms 2324 KB Output is correct
22 Correct 26 ms 1096 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 47 ms 1336 KB Output is correct
25 Correct 33 ms 1264 KB Output is correct
26 Correct 69 ms 1448 KB Output is correct
27 Correct 302 ms 2492 KB Output is correct
28 Correct 309 ms 2552 KB Output is correct
29 Correct 317 ms 2616 KB Output is correct
30 Correct 333 ms 2564 KB Output is correct
31 Correct 340 ms 2556 KB Output is correct
32 Correct 334 ms 2656 KB Output is correct
33 Correct 373 ms 2716 KB Output is correct
34 Correct 348 ms 2744 KB Output is correct
35 Correct 339 ms 2628 KB Output is correct
36 Correct 362 ms 2636 KB Output is correct
37 Correct 353 ms 2720 KB Output is correct