sgu159

搜索题

一开始以为要写高精度,后来发现自己好傻。。。。
对于一个自守数 a1a2a3an¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ,去掉首位后 a2a3an¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 仍然是一个自守数,于是就可以从低位往高位搜。

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值