답안 #70525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
70525 2018-08-23T05:08:56 Z 윤교준(#2187) Toys (CEOI18_toy) C++11
100 / 100
3827 ms 17460 KB
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void f(vector<pii>&, int, int);
void g0(vector<pii>&, int, int, vector<int>&, int, int, int, int);
void g1(vector<pii>&, int, int, vector<int>&, int, int, vector<vector<pii>>&);

const bool debug = 0;
const int MAXX = 33005;

bitset<MAXX> isnp;

vector<vector<int>> PP;
vector<pii> PV;

vector<int> Ans;
int N;

void fact(vector<pii> &V, int N) {
	V.clear(); if(N < 2) return;
	if(!(N&1)) {
		for(V.eb(2, 0); !(N&1); V.back().second++, N >>= 1);
	}
	for(int i = 3; i*i <= N; i += 2) if(!isnp[i] && !(N%i)) {
		for(V.eb(i, 1), N /= i; !(N%i); V.back().second++, N /= i);
	}
	if(1 < N) V.eb(N, 1);
}

void g2(vector<pii> &G, int i, int n, vector<int> &VC, int j, int gn, vector<vector<pii>> &GC, int lft, int cnt, int ub) {
	if(debug) {
		printf("G2 : i=%d, n=%d, j=%d, gn=%d, left=%d, cnt=%d, ub=%d\n", i, n, j, gn, lft, cnt, ub);
	}

	if(!cnt || !lft) {
		g1(G, i, n, VC, j+1, gn, GC);
		return;
	}

	for(int x = (cnt + lft - 1) / lft; x < ub; x++) {
		for(int c = max(1, cnt - x * lft + lft), cub = min(cnt/x, lft); c <= cub; c++) {
			if(debug) printf("TRY (%d,%d)\n", x, c);
			GC[j].eb(x, c);
			g2(G, i, n, VC, j, gn, GC, lft-c, cnt-x*c, x);
			GC[j].pop_back();
		}
	}
}

void g1(vector<pii> &G, int i, int n, vector<int> &VC, int j, int gn, vector<vector<pii>> &GC) {
	if(debug) {
		printf("G1 : i=%d, n=%d, j=%d, gn=%d : VC=", i, n, j, gn);
		for(int v : VC) printf("%d ", v);
		if(j < gn) {
			printf(": GC=");
			for(auto &v : GC[j]) printf("(%d,%d) ", v.first, v.second);
		}
		puts("");
	}

	if(j == gn) {
		vector<pii> NG;
		for(int k = 0; k < gn; k++) {
			int a, b; tie(a, b) = G[k];
			for(auto &v : GC[k]) {
				NG.eb(a * PP[i][v.first], v.second);
				b -= v.second;
			}
			if(b) NG.eb(a, b);
		}

		f(NG, i+1, n);
		return;
	}

	g2(G, i, n, VC, j, gn, GC, G[j].second, VC[j], 31);
}

void g0(vector<pii> &G, int i, int n, vector<int> &VC, int j, int gn, int p, int cnt) {
	if(debug) {
		printf("G0 : i=%d, n=%d, j=%d, gn=%d, p=%d, cnt=%d :: ", i, n, j, gn, p, cnt);
		for(auto &v : VC) printf("%d ", v);
		puts("");
	}

	if(!cnt) {
		vector<vector<pii>> GC(gn);
		g1(G, i, n, VC, 0, gn, GC);
		return;
	}

	if(j+1 == gn) {
		VC[j] = cnt;
		g0(G, i, n, VC, j+1, gn, p, 0);
		VC[j] = 0;
		return;
	}

	for(int k = 0; k <= cnt; k++) {
		VC[j] = k;
		g0(G, i, n, VC, j+1, gn, p, cnt-k);
	}
	VC[j] = 0;
}

void f(vector<pii> &G, int i, int n) {
	if(debug) {
		printf("F : i=%d, n=%d :: ", i, n);
		for(auto &v : G) printf("(%d,%d) ", v.first, v.second);
		puts("");
	}

	if(i == n) {
		int ret = -31;
		for(auto &v : G)
			ret += v.first * v.second;
		Ans.eb(ret);
		return;
	}

	int p, cnt; tie(p, cnt) = PV[i];
	int gn = sz(G);
	vector<int> VC(gn, 0);

	g0(G, i, n, VC, 0, gn, p, cnt);
}

int main() {
	isnp[0] = isnp[1] = true;
	for(int i = 4; i < MAXX; i += 2) isnp[i] = true;
	for(int i = 3; i*i < MAXX; i += 2) if(!isnp[i])
		for(int j = i*i; j < MAXX; j += i<<1) isnp[j] = true;
	
	cin >> N;
	if(1 == N) {
		puts("1\n0");
		return 0;
	}
	fact(PV, N);
	sort(allv(PV), [&](pii a, pii b) { return a.second > b.second; });

	for(auto &v : PV) {
		int p, cnt; tie(p, cnt) = v;
		PP.eb(vector<int>(1, 1));
		for(int i = 0; i < cnt; i++)
			PP.back().eb(PP.back().back() * p);
	}

	{
		vector<pii> G(1, pii(1, 31));
		f(G, 0, sz(PV));
	}

	sorv(Ans); univ(Ans);
	printf("%d\n", sz(Ans));
	for(int v : Ans) printf("%d ", v);
	puts("");
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 2 ms 640 KB Output is correct
35 Correct 2 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 2 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 4 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 2 ms 640 KB Output is correct
35 Correct 2 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 2 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 4 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 2 ms 640 KB Output is correct
50 Correct 5 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 5 ms 640 KB Output is correct
53 Correct 4 ms 640 KB Output is correct
54 Correct 3 ms 640 KB Output is correct
55 Correct 6 ms 640 KB Output is correct
56 Correct 7 ms 640 KB Output is correct
57 Correct 6 ms 640 KB Output is correct
58 Correct 2 ms 640 KB Output is correct
59 Correct 2 ms 640 KB Output is correct
60 Correct 2 ms 640 KB Output is correct
61 Correct 5 ms 640 KB Output is correct
62 Correct 4 ms 640 KB Output is correct
63 Correct 3 ms 640 KB Output is correct
64 Correct 3 ms 640 KB Output is correct
65 Correct 4 ms 640 KB Output is correct
66 Correct 3 ms 640 KB Output is correct
67 Correct 2 ms 640 KB Output is correct
68 Correct 2 ms 640 KB Output is correct
69 Correct 3 ms 640 KB Output is correct
70 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 2 ms 640 KB Output is correct
35 Correct 2 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 2 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 4 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 2 ms 640 KB Output is correct
50 Correct 5 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 5 ms 640 KB Output is correct
53 Correct 4 ms 640 KB Output is correct
54 Correct 3 ms 640 KB Output is correct
55 Correct 6 ms 640 KB Output is correct
56 Correct 7 ms 640 KB Output is correct
57 Correct 6 ms 640 KB Output is correct
58 Correct 2 ms 640 KB Output is correct
59 Correct 2 ms 640 KB Output is correct
60 Correct 2 ms 640 KB Output is correct
61 Correct 5 ms 640 KB Output is correct
62 Correct 4 ms 640 KB Output is correct
63 Correct 3 ms 640 KB Output is correct
64 Correct 3 ms 640 KB Output is correct
65 Correct 4 ms 640 KB Output is correct
66 Correct 3 ms 640 KB Output is correct
67 Correct 2 ms 640 KB Output is correct
68 Correct 2 ms 640 KB Output is correct
69 Correct 3 ms 640 KB Output is correct
70 Correct 3 ms 640 KB Output is correct
71 Correct 346 ms 2740 KB Output is correct
72 Correct 181 ms 2740 KB Output is correct
73 Correct 342 ms 2924 KB Output is correct
74 Correct 145 ms 2924 KB Output is correct
75 Correct 211 ms 2924 KB Output is correct
76 Correct 800 ms 4884 KB Output is correct
77 Correct 790 ms 5112 KB Output is correct
78 Correct 474 ms 5112 KB Output is correct
79 Correct 544 ms 5112 KB Output is correct
80 Correct 3 ms 5112 KB Output is correct
81 Correct 2 ms 5112 KB Output is correct
82 Correct 6 ms 5112 KB Output is correct
83 Correct 55 ms 5112 KB Output is correct
84 Correct 92 ms 5112 KB Output is correct
85 Correct 7 ms 5112 KB Output is correct
86 Correct 2 ms 5112 KB Output is correct
87 Correct 54 ms 5112 KB Output is correct
88 Correct 2 ms 5112 KB Output is correct
89 Correct 2 ms 5112 KB Output is correct
90 Correct 3 ms 5112 KB Output is correct
91 Correct 10 ms 5112 KB Output is correct
92 Correct 44 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 248 KB Output is correct
2 Correct 2 ms 356 KB Output is correct
3 Correct 2 ms 560 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 3 ms 640 KB Output is correct
8 Correct 3 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 3 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 3 ms 640 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 3 ms 640 KB Output is correct
20 Correct 3 ms 640 KB Output is correct
21 Correct 3 ms 640 KB Output is correct
22 Correct 2 ms 640 KB Output is correct
23 Correct 3 ms 640 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 3 ms 640 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 3 ms 640 KB Output is correct
29 Correct 3 ms 640 KB Output is correct
30 Correct 4 ms 640 KB Output is correct
31 Correct 4 ms 640 KB Output is correct
32 Correct 3 ms 640 KB Output is correct
33 Correct 3 ms 640 KB Output is correct
34 Correct 2 ms 640 KB Output is correct
35 Correct 2 ms 640 KB Output is correct
36 Correct 3 ms 640 KB Output is correct
37 Correct 3 ms 640 KB Output is correct
38 Correct 4 ms 640 KB Output is correct
39 Correct 2 ms 640 KB Output is correct
40 Correct 3 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 2 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 2 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 4 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 2 ms 640 KB Output is correct
50 Correct 5 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 5 ms 640 KB Output is correct
53 Correct 4 ms 640 KB Output is correct
54 Correct 3 ms 640 KB Output is correct
55 Correct 6 ms 640 KB Output is correct
56 Correct 7 ms 640 KB Output is correct
57 Correct 6 ms 640 KB Output is correct
58 Correct 2 ms 640 KB Output is correct
59 Correct 2 ms 640 KB Output is correct
60 Correct 2 ms 640 KB Output is correct
61 Correct 5 ms 640 KB Output is correct
62 Correct 4 ms 640 KB Output is correct
63 Correct 3 ms 640 KB Output is correct
64 Correct 3 ms 640 KB Output is correct
65 Correct 4 ms 640 KB Output is correct
66 Correct 3 ms 640 KB Output is correct
67 Correct 2 ms 640 KB Output is correct
68 Correct 2 ms 640 KB Output is correct
69 Correct 3 ms 640 KB Output is correct
70 Correct 3 ms 640 KB Output is correct
71 Correct 346 ms 2740 KB Output is correct
72 Correct 181 ms 2740 KB Output is correct
73 Correct 342 ms 2924 KB Output is correct
74 Correct 145 ms 2924 KB Output is correct
75 Correct 211 ms 2924 KB Output is correct
76 Correct 800 ms 4884 KB Output is correct
77 Correct 790 ms 5112 KB Output is correct
78 Correct 474 ms 5112 KB Output is correct
79 Correct 544 ms 5112 KB Output is correct
80 Correct 3 ms 5112 KB Output is correct
81 Correct 2 ms 5112 KB Output is correct
82 Correct 6 ms 5112 KB Output is correct
83 Correct 55 ms 5112 KB Output is correct
84 Correct 92 ms 5112 KB Output is correct
85 Correct 7 ms 5112 KB Output is correct
86 Correct 2 ms 5112 KB Output is correct
87 Correct 54 ms 5112 KB Output is correct
88 Correct 2 ms 5112 KB Output is correct
89 Correct 2 ms 5112 KB Output is correct
90 Correct 3 ms 5112 KB Output is correct
91 Correct 10 ms 5112 KB Output is correct
92 Correct 44 ms 5112 KB Output is correct
93 Correct 2406 ms 9328 KB Output is correct
94 Correct 878 ms 9328 KB Output is correct
95 Correct 1323 ms 9328 KB Output is correct
96 Correct 1160 ms 9328 KB Output is correct
97 Correct 1122 ms 9328 KB Output is correct
98 Correct 3087 ms 17404 KB Output is correct
99 Correct 3827 ms 17404 KB Output is correct
100 Correct 2899 ms 17460 KB Output is correct
101 Correct 3776 ms 17460 KB Output is correct
102 Correct 3270 ms 17460 KB Output is correct
103 Correct 1448 ms 17460 KB Output is correct
104 Correct 4 ms 17460 KB Output is correct
105 Correct 3 ms 17460 KB Output is correct
106 Correct 4 ms 17460 KB Output is correct
107 Correct 136 ms 17460 KB Output is correct
108 Correct 511 ms 17460 KB Output is correct
109 Correct 17 ms 17460 KB Output is correct
110 Correct 4 ms 17460 KB Output is correct
111 Correct 202 ms 17460 KB Output is correct
112 Correct 3 ms 17460 KB Output is correct
113 Correct 3 ms 17460 KB Output is correct
114 Correct 6 ms 17460 KB Output is correct
115 Correct 4 ms 17460 KB Output is correct
116 Correct 9 ms 17460 KB Output is correct
117 Correct 231 ms 17460 KB Output is correct