Submission #66142592
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <string.h>const char* for_add[2048];int for_add_len[2048];const char* for_mult[2048];int for_mult_len[2048];int main(void) {int N;int t;if (scanf("%d", &N) != 1) return 1;for (t = 1; t <= N; t++) {if (t == 1) {for_add[t] = "1";for_add_len[t] = 1;for_mult[t] = "1";for_mult_len[t] = 1;} else if (t == 11) {for_add[t] = "11";
#include <stdio.h> #include <stdlib.h> #include <string.h> const char* for_add[2048]; int for_add_len[2048]; const char* for_mult[2048]; int for_mult_len[2048]; int main(void) { int N; int t; if (scanf("%d", &N) != 1) return 1; for (t = 1; t <= N; t++) { if (t == 1) { for_add[t] = "1"; for_add_len[t] = 1; for_mult[t] = "1"; for_mult_len[t] = 1; } else if (t == 11) { for_add[t] = "11"; for_add_len[t] = 2; for_mult[t] = "11"; for_mult_len[t] = 2; } else if (t == 111) { for_add[t] = "111"; for_add_len[t] = 3; for_mult[t] = "111"; for_mult_len[t] = 3; } else if (t == 1111) { for_add[t] = "1111"; for_add_len[t] = 4; for_mult[t] = "1111"; for_mult_len[t] = 4; } else { int add_best_a = -1, add_best_b = -1, add_best_score = 22222; int mult_best_a = -1, mult_best_b = -1, mult_best_score = 22222; int i; for (i = 1; i < t; i++) { int add_candidate = for_add_len[i] + for_add_len[t - i] + 1; if (add_candidate < add_best_score) { add_best_a = i; add_best_b = t - i; add_best_score = add_candidate; } if (t % i == 0 && i > 1) { int mult_candidate = for_mult_len[i] + for_mult_len[t / i] + 1; if (mult_candidate < mult_best_score) { mult_best_a = i; mult_best_b = t / i; mult_best_score = mult_candidate; } } } if (add_best_score <= mult_best_score) { char* res = malloc(add_best_score + 1); strcpy(res, for_add[add_best_a]); strcat(res, "+"); strcat(res, for_add[add_best_b]); for_add[t] = res; for_add_len[t] = add_best_score; } else { char* res = malloc(mult_best_score + 1); strcpy(res, for_mult[mult_best_a]); strcat(res, "*"); strcat(res, for_mult[mult_best_b]); for_add[t] = res; for_add_len[t] = mult_best_score; } if (add_best_score + 2 < mult_best_score) { char* res = malloc(add_best_score + 3); strcpy(res, "("); strcat(res, for_add[add_best_a]); strcat(res, "+"); strcat(res, for_add[add_best_b]); strcat(res, ")"); for_mult[t] = res; for_mult_len[t] = add_best_score + 2; } else { char* res = malloc(mult_best_score + 1); strcpy(res, for_mult[mult_best_a]); strcat(res, "*"); strcat(res, for_mult[mult_best_b]); for_mult[t] = res; for_mult_len[t] = mult_best_score; } } } puts(for_add[N]); return 0; }
Submission Info
Submission Time | |
---|---|
Task | F - Shortest One Formula |
User | mikecat |
Language | C (gcc 12.2.0) |
Score | 500 |
Code Size | 2479 Byte |
Status | AC |
Exec Time | 5 ms |
Memory | 1852 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 500 / 500 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt, 01_test_45.txt, 01_test_46.txt, 01_test_47.txt, 01_test_48.txt, 01_test_49.txt, 01_test_50.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
00_sample_00.txt | AC | 1 ms | 1572 KB |
00_sample_01.txt | AC | 1 ms | 1652 KB |
00_sample_02.txt | AC | 1 ms | 1624 KB |
01_test_00.txt | AC | 0 ms | 1624 KB |
01_test_01.txt | AC | 0 ms | 1588 KB |
01_test_02.txt | AC | 4 ms | 1648 KB |
01_test_03.txt | AC | 4 ms | 1844 KB |
01_test_04.txt | AC | 4 ms | 1644 KB |
01_test_05.txt | AC | 4 ms | 1844 KB |
01_test_06.txt | AC | 4 ms | 1748 KB |
01_test_07.txt | AC | 4 ms | 1784 KB |
01_test_08.txt | AC | 4 ms | 1744 KB |
01_test_09.txt | AC | 4 ms | 1736 KB |
01_test_10.txt | AC | 4 ms | 1740 KB |
01_test_11.txt | AC | 4 ms | 1804 KB |
01_test_12.txt | AC | 4 ms | 1788 KB |
01_test_13.txt | AC | 1 ms | 1624 KB |
01_test_14.txt | AC | 2 ms | 1636 KB |
01_test_15.txt | AC | 2 ms | 1688 KB |
01_test_16.txt | AC | 1 ms | 1628 KB |
01_test_17.txt | AC | 2 ms | 1700 KB |
01_test_18.txt | AC | 1 ms | 1668 KB |
01_test_19.txt | AC | 0 ms | 1664 KB |
01_test_20.txt | AC | 1 ms | 1608 KB |
01_test_21.txt | AC | 1 ms | 1636 KB |
01_test_22.txt | AC | 2 ms | 1764 KB |
01_test_23.txt | AC | 1 ms | 1712 KB |
01_test_24.txt | AC | 0 ms | 1676 KB |
01_test_25.txt | AC | 2 ms | 1748 KB |
01_test_26.txt | AC | 2 ms | 1664 KB |
01_test_27.txt | AC | 1 ms | 1568 KB |
01_test_28.txt | AC | 3 ms | 1688 KB |
01_test_29.txt | AC | 1 ms | 1648 KB |
01_test_30.txt | AC | 3 ms | 1780 KB |
01_test_31.txt | AC | 1 ms | 1580 KB |
01_test_32.txt | AC | 2 ms | 1652 KB |
01_test_33.txt | AC | 4 ms | 1848 KB |
01_test_34.txt | AC | 4 ms | 1852 KB |
01_test_35.txt | AC | 4 ms | 1736 KB |
01_test_36.txt | AC | 4 ms | 1728 KB |
01_test_37.txt | AC | 4 ms | 1800 KB |
01_test_38.txt | AC | 5 ms | 1776 KB |
01_test_39.txt | AC | 4 ms | 1736 KB |
01_test_40.txt | AC | 4 ms | 1848 KB |
01_test_41.txt | AC | 4 ms | 1800 KB |
01_test_42.txt | AC | 4 ms | 1800 KB |
01_test_43.txt | AC | 2 ms | 1724 KB |
01_test_44.txt | AC | 0 ms | 1628 KB |
01_test_45.txt | AC | 1 ms | 1648 KB |
01_test_46.txt | AC | 1 ms | 1660 KB |
01_test_47.txt | AC | 3 ms | 1824 KB |
01_test_48.txt | AC | 3 ms | 1820 KB |
01_test_49.txt | AC | 4 ms | 1768 KB |
01_test_50.txt | AC | 4 ms | 1700 KB |