USACO Dual Palindromes

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
ID: wcr19961
PROG: dualpal
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 30;
int tmp[maxn];

bool check(int n, int k) {
int len = 0;
while (n) {
tmp[len++] = n % k;
n /= k;
}
int len2 = len >> 1;
for (int i = 0; i < len2; ++i) {
if (tmp[i] != tmp[len - 1 - i]) {
return false;
}
}
return true;
}

int main() {
freopen("dualpal.in", "r", stdin);
freopen("dualpal.out", "w", stdout);
int n, s;
scanf("%d%d", &n, &s);
for (int i = s + 1; n != 0; ++i) {
int cnt = 0;
for (int j = 2; j <= 10; ++j) {
if (check(i, j)) {
++cnt;
}
}
if (cnt >= 2) {
printf("%d\n", i);
--n;
}
}
return 0;
}