Submission #762657

# Submission time Handle Problem Language Result Execution time Memory
762657 2023-06-21T15:40:16 Z rainboy Weird Numeral System (CCO21_day1problem2) C
0 / 25
54 ms 79436 KB
#include <stdio.h>
#include <stdlib.h>

#define N	5001
#define M	1000000
#define MD	0x7fffffff

long long *ek[M]; int *ev[M], eo[M], X = 314159265;

void init() {
	int h;

	for (h = 0; h < M; h++) {
		ek[h] = (long long *) malloc(2 * sizeof *ek[h]);
		ev[h] = (int *) malloc(2 * sizeof *ev[h]);
	}
}

int hash(long long k) {
	long long k1, k2;

	k += (long long) MD * MD, k1 = k / MD, k2 = k % MD;
	return (k1 * X % MD + k2) % MD * X % MD % M;
}

void put(long long k, int v) {
	int h = hash(k), o;

	for (o = eo[h]; o--; )
		if (ek[h][o] == k) {
			ev[h][o] = v;
			return;
		}
	o = eo[h]++;
	if (o >= 2 && (o & o - 1) == 0) {
		ek[h] = (long long *) realloc(ek[h], o * 2 * sizeof *ek[h]);
		ev[h] = (int *) realloc(ev[h], o * 2 * sizeof *ev[h]);
	}
	ek[h][o] = k, ev[h][o] = v;
}

int get(long long k) {
	int h = hash(k), o;

	for (o = eo[h]; o--; )
		if (ek[h][o] == k)
			return ev[h][o];
	return -1;
}

int dd[N], n, b;

int solve(long long a) {
	int i;

	if (a == 0)
		return 1;
	if ((i = get(a)) == -1) {
		put(a, n);
		for (i = 0; i < n; i++)
			if ((a - dd[i]) % b == 0 && solve((a - dd[i]) / b)) {
				put(a, i);
				break;
			}
	}
	return i < n;
}

void trace(long long a) {
	int i;

	if (a == 0)
		return;
	i = get(a);
	trace((a - dd[i]) / b), printf("%d ", dd[i]);
}

int main() {
	int q, i;

	init();
	scanf("%d%d%d%*d", &b, &q, &n);
	for (i = 0; i < n; i++)
		scanf("%d", &dd[i]);
	while (q--) {
		long long a;

		scanf("%lld", &a);
		if (a == 0)
			printf("0\n");
		else if (solve(a))
			trace(a), printf("\n");
		else
			printf("IMPOSSIBLE\n");
	}
	return 0;
}

Compilation message

Main.c: In function 'put':
Main.c:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   35 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
Main.c: In function 'main':
Main.c:82:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |  scanf("%d%d%d%*d", &b, &q, &n);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:84:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |   scanf("%d", &dd[i]);
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:88:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |   scanf("%lld", &a);
      |   ^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 79436 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 54 ms 79436 KB Expected integer, but "IMPOSSIBLE" found
2 Halted 0 ms 0 KB -