Submission #696435

# Submission time Handle Problem Language Result Execution time Memory
696435 2023-02-06T14:00:23 Z SanguineChameleon Gauss (COCI17_gauss) C++17
160 / 160
818 ms 95196 KB
// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

long double rand_real(long double rmin, long double rmax) {
	uniform_real_distribution<long double> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

const int inf = 1e9 + 20;
const int lim = 1e6;
int F[10020];
int L[1020];
int X[52];
int C[52];
int dp[lim + 20][22];
int Dc[lim + 20];
int Pc[lim + 20];
int best1[22];
int best2[22];
int best3[52];
int ok[52];

void just_do_it() {
	fill_n(&dp[0][0], (lim + 20) * 22, inf);
	int K;
	cin >> K;
	for (int i = 1; i <= K; i++) {
		cin >> F[i];
	}
	int M;
	cin >> M;
	for (int i = 1; i <= M; i++) {
		cin >> L[i];
	}
	int T;
	cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> X[i] >> C[i];
	}
	for (int i = 1; i <= lim; i++) {
		for (int j = i; j <= lim; j += i) {
			Dc[j]++;
			if (Dc[i] == 2) {
				int tmp = j;
				do {
					Pc[j]++;
					tmp /= i;
				}
				while (tmp % i == 0);
			}
		}
	}
	dp[1][0] = 0;
	for (int i = 1; i <= lim; i++) {
		for (int j = i + i, d = 2; j <= lim; j += i, d++) {
			for (int k = 0; k <= Pc[i]; k++) {
				dp[j][k + 1] = min(dp[j][k + 1], dp[i][k] + (Dc[d] <= K ? F[Dc[d]] : 0));
			}
		}
	}
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; i++) {
		int A, B;
		cin >> A >> B;
		if (A % B != 0) {
			cout << -M << '\n';
			continue;
		}
		int d = A / B;
		int mx = Pc[d];
		for (int j = 1; j <= mx; j++) {
			best2[j] = dp[d][j];
		}
		int cnt = 0;
		for (int j = 1; j <= T; j++) {
			best3[j] = inf;
			if (A % X[j] != 0 || X[j] % B != 0) {
				continue;
			}
			int Ad = A / X[j];
			int Bd = X[j] / B;
			for (int k = 0; k <= mx; k++) {
				best1[k] = inf;
			}
			for (int x = 0; x <= Pc[Ad]; x++) {
				for (int y = 0; y <= Pc[Bd]; y++) {
					best1[x + y] = min(best1[x + y], dp[Ad][x] + dp[Bd][y]);
				}
			}
			for (int k = 0; k <= mx; k++) {
				best3[j] = min(best3[j] + C[j], best1[k]);
				best2[k] = min(best2[k], best3[j]);
			}
			if (best3[j] != inf) {
				ok[++cnt] = j;
			}
		}
		long long res = 0;
		for (int j = 1; j <= M; j++) {
			if (L[j] <= mx) {
				if (best2[L[j]] == inf) {
					res += -1;
				}
				else {
					res += best2[L[j]];
				}
			}
			else {
				int cur = inf;
				for (int k = 1; k <= cnt; k++) {
					int id = ok[k];
					cur = min(cur, (L[j] - mx) * C[id] + best3[id]);
				}
				if (cur == inf) {
					res += -1;
				}
				else {
					res += cur;
				}
			}
		}
		cout << res << '\n';
	}
}

// END MAIN CODE

Compilation message

gauss.cpp: In function 'void file_io(std::string, std::string)':
gauss.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |   freopen(fi.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
gauss.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |   freopen(fo.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 463 ms 94244 KB Output is correct
2 Correct 485 ms 94244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 456 ms 94248 KB Output is correct
2 Correct 451 ms 94248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 489 ms 94244 KB Output is correct
2 Correct 458 ms 94252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 466 ms 94248 KB Output is correct
2 Correct 500 ms 94248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 561 ms 94672 KB Output is correct
2 Correct 553 ms 94656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 552 ms 94620 KB Output is correct
2 Correct 569 ms 94636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 598 ms 94556 KB Output is correct
2 Correct 818 ms 94604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 732 ms 94636 KB Output is correct
2 Correct 540 ms 94624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 815 ms 94756 KB Output is correct
2 Correct 581 ms 95196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 690 ms 94652 KB Output is correct
2 Correct 665 ms 95068 KB Output is correct