Submission #57611714
Source Code Expand
Copy
#include <stdio.h>#include <inttypes.h>#define MAX_DIGIT 20int gen;int limit[MAX_DIGIT];int memo[MAX_DIGIT][10][2][2];int memo_gen[MAX_DIGIT][10][2][2];uint64_t calc(int pos, int prev, int zero, int sati) {uint64_t ans = 0;int i, max;if (pos >= MAX_DIGIT) return !zero;if (memo_gen[pos][prev][zero][sati] == gen) return memo[pos][prev][zero][sati];max = sati ? limit[pos] : 9;for (i = 0; i <= max; i++) {if (i != prev || (i == 0 && zero)) {ans += calc(pos + 1, i, zero && i == 0, sati && i == max);}
#include <stdio.h>
#include <inttypes.h>
#define MAX_DIGIT 20
int gen;
int limit[MAX_DIGIT];
int memo[MAX_DIGIT][10][2][2];
int memo_gen[MAX_DIGIT][10][2][2];
uint64_t calc(int pos, int prev, int zero, int sati) {
uint64_t ans = 0;
int i, max;
if (pos >= MAX_DIGIT) return !zero;
if (memo_gen[pos][prev][zero][sati] == gen) return memo[pos][prev][zero][sati];
max = sati ? limit[pos] : 9;
for (i = 0; i <= max; i++) {
if (i != prev || (i == 0 && zero)) {
ans += calc(pos + 1, i, zero && i == 0, sati && i == max);
}
}
memo_gen[pos][prev][zero][sati] = gen;
memo[pos][prev][zero][sati] = ans;
return ans;
}
uint64_t query(uint64_t n) {
int i;
for (i = 0; i < MAX_DIGIT; i++) {
limit[MAX_DIGIT - 1 - i] = n % 10;
n /= 10;
}
gen++;
return calc(0, 0, 1, 1);
}
int main(void) {
int T, tc;
if (scanf("%d", &T) != 1) return 1;
for (tc = 0; tc < T; tc++) {
uint64_t K;
uint64_t no = 0, yes = UINT64_C(10000000000000000000);
if (scanf("%" SCNu64, &K) != 1) return 1;
while (no + 1 < yes) {
uint64_t m = no + (yes - no) / 2;
if (query(m) >= K) yes = m; else no = m;
}
printf("%" PRIu64 "\n", yes);
}
return 0;
}
/*
指定の数以下の "Neq Number" が何個あるかをメモ化探索
→ K以上になる最小の数を二分探索
*/
Submission Info
| Submission Time | |
|---|---|
| Task | A - Neq Number |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 0 |
| Code Size | 1347 Byte |
| Status | WA |
| Exec Time | 42 ms |
| Memory | 1712 KB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 400 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt |
| All | 00_sample_01.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, 02_handmade_01.txt, 02_handmade_02.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 1 ms | 1620 KB |
| 01_test_01.txt | AC | 22 ms | 1536 KB |
| 01_test_02.txt | AC | 23 ms | 1612 KB |
| 01_test_03.txt | WA | 41 ms | 1712 KB |
| 01_test_04.txt | WA | 42 ms | 1676 KB |
| 01_test_05.txt | AC | 24 ms | 1556 KB |
| 01_test_06.txt | AC | 24 ms | 1704 KB |
| 01_test_07.txt | WA | 41 ms | 1708 KB |
| 01_test_08.txt | WA | 42 ms | 1684 KB |
| 01_test_09.txt | WA | 40 ms | 1616 KB |
| 01_test_10.txt | WA | 41 ms | 1588 KB |
| 01_test_11.txt | WA | 40 ms | 1556 KB |
| 01_test_12.txt | WA | 40 ms | 1712 KB |
| 01_test_13.txt | WA | 40 ms | 1696 KB |
| 02_handmade_01.txt | AC | 24 ms | 1712 KB |
| 02_handmade_02.txt | WA | 33 ms | 1612 KB |