搜索题
一开始以为要写高精度,后来发现自己好傻。。。。
对于一个自守数
a1a2a3⋯an¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
,去掉首位后
a2a3⋯an¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
仍然是一个自守数,于是就可以从低位往高位搜。
#include <ctime>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define fly(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
const int maxl = 2005;
const char num[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int b, n;
struct Bigint
{
char data[maxl];
void prt()
{
for(int i = n-1; i >= 0; --i)
putchar(num[data[i]]);
putchar(10);
}
bool operator < (const Bigint &r) const {
return strcmp(data, r.data) < 0;
}
};
Bigint a;
vector<Bigint> ans;
void dfs(int x, int left)
{
if(x == n)
{
if(n == 1 || a.data[n - 1])
ans.push_back(a);
return ;
}
for(int i = 0; i < b; ++i)
{
int cnt = left;
a.data[x] = i;
for(int j = 0; j <= x; ++j)
cnt += a.data[j] * a.data[x - j];
if(cnt % b == i) dfs(x + 1, cnt / b);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
scanf("%d%d", &b, &n);
dfs(0, 0);
sort(ans.begin(), ans.end());
printf("%d\n", int(ans.size()));
for(int i = 0; i < ans.size(); ++i)
ans[i].prt();
fly("%.3f\n", (double)clock() / CLOCKS_PER_SEC);
#ifndef ONLINE_JUDGE
fclose(stdin), fclose(stdout);
#endif
return 0;
}