Submission #648644

#TimeUsernameProblemLanguageResultExecution timeMemory
648644ghostwriterBootfall (IZhO17_bootfall)C++14
28 / 100
1094 ms20464 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const db PI = acos(-1);
typedef complex<double> cd;
typedef vector<cd> pn;
void fft(pn &a, bool inv) {
	int n = sz(a), rev = 0;
	FOR(i, 1, n - 1) {
		int j = n >> 1;
		WHILE(rev & j) {
			rev ^= j;
			j >>= 1;
		}
		rev ^= j;
		if (i < rev) swap(a[i], a[rev]);
	}
	for (int i = 2; i <= n; i <<= 1) {
		db ang = 2 * PI / i * (inv? -1 : 1);
		cd wn(cos(ang), sin(ang));
		for (int j = 0; j < n; j += i) {
			cd w(1);
			for (int z = 0; z < i / 2; ++z) {
				cd u = a[j + z], v = w * a[j + z + i / 2];
				a[j + z] = u + v;
				a[j + z + i / 2] = u - v;
				w *= wn;
			}
		}
	}
	if (inv) FRN(i, n) a[i] /= n;
}
pn multiply(pn a, pn b) {
	int n = 1;
	WHILE(n < sz(a) + sz(b)) n <<= 1;
	a.resize(n);
	b.resize(n);
	fft(a, 0);
	fft(b, 0);
	FRN(i, n) a[i] *= b[i];
	fft(a, 1);
	FRN(i, n) a[i] = round(a[i].real());
	return a;
}
const int N = 502;
const int NxN = 25e4 + 1;
int n, a[N], c[NxN], sum = 0;
bitset<NxN> d[N], d1[N];
vi ans;
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n;
    FOR(i, 1, n) {
    	cin >> a[i];
    	sum += a[i];
    }
    if (sum & 1) {
    	cout << 0;
    	return 0;
    }
    d[0][0] = d1[n + 1][0] = 1;
    FOR(i, 1, n) d[i] = d[i - 1] | (d[i - 1] << a[i]);
    if (!d[n][sum / 2]) {
    	cout << 0;
    	return 0;
    }
    FOS(i, n, 1) d1[i] = d1[i + 1] | (d1[i + 1] << a[i]);
    FOR(i, 1, 500 * n) c[i] = 1;
    FOR(i, 1, n) {
    	pn p(500 * (i - 1) + 1, 0), p1(500 * (n - i) + 1, 0);
    	FOR(j, 0, 500 * (i - 1)) p[j] = d[i - 1][j];
    	FOR(j, 0, 500 * (n - i)) p1[j] = d1[i + 1][j];
    	p = multiply(p, p1);
    	FOR(j, 1, 500 * n) {
    		if (!c[j]) continue;
    		int tmp = sum + j - a[i];
    		if (tmp & 1) {
    			c[j] = 0;
    			continue;
    		}
    		tmp /= 2;
    		if (tmp >= sz(p)) c[j] = 0;
    		else {
    			tmp = p[tmp].real();
    			if (!tmp) c[j] = 0;
    		}
    	}
    }
    FOR(i, 1, 500 * n) {
    	if (!c[i]) continue;
    	ans.pb(i);
    }
    cout << sz(ans) << '\n';
    EACH(i, ans) cout << i << ' ';
    return 0;
}
/*
4
1 3 1 5
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message (stderr)

bootfall.cpp: In function 'void fft(pn&, bool)':
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:56:2: note: in expansion of macro 'FOR'
   56 |  FOR(i, 1, n - 1) {
      |  ^~~
bootfall.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
bootfall.cpp:78:11: note: in expansion of macro 'FRN'
   78 |  if (inv) FRN(i, n) a[i] /= n;
      |           ^~~
bootfall.cpp: In function 'pn multiply(pn, pn)':
bootfall.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
bootfall.cpp:87:2: note: in expansion of macro 'FRN'
   87 |  FRN(i, n) a[i] *= b[i];
      |  ^~~
bootfall.cpp:34:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   34 | #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
bootfall.cpp:89:2: note: in expansion of macro 'FRN'
   89 |  FRN(i, n) a[i] = round(a[i].real());
      |  ^~~
bootfall.cpp: In function 'int main()':
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:102:5: note: in expansion of macro 'FOR'
  102 |     FOR(i, 1, n) {
      |     ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:111:5: note: in expansion of macro 'FOR'
  111 |     FOR(i, 1, n) d[i] = d[i - 1] | (d[i - 1] << a[i]);
      |     ^~~
bootfall.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
bootfall.cpp:116:5: note: in expansion of macro 'FOS'
  116 |     FOS(i, n, 1) d1[i] = d1[i + 1] | (d1[i + 1] << a[i]);
      |     ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:117:5: note: in expansion of macro 'FOR'
  117 |     FOR(i, 1, 500 * n) c[i] = 1;
      |     ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:118:5: note: in expansion of macro 'FOR'
  118 |     FOR(i, 1, n) {
      |     ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:120:6: note: in expansion of macro 'FOR'
  120 |      FOR(j, 0, 500 * (i - 1)) p[j] = d[i - 1][j];
      |      ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:121:6: note: in expansion of macro 'FOR'
  121 |      FOR(j, 0, 500 * (n - i)) p1[j] = d1[i + 1][j];
      |      ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:123:6: note: in expansion of macro 'FOR'
  123 |      FOR(j, 1, 500 * n) {
      |      ^~~
bootfall.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
bootfall.cpp:138:5: note: in expansion of macro 'FOR'
  138 |     FOR(i, 1, 500 * n) {
      |     ^~~
bootfall.cpp:36:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
bootfall.cpp:143:5: note: in expansion of macro 'EACH'
  143 |     EACH(i, ans) cout << i << ' ';
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...