sgu 159 分类: sgu 2015-05-14 13:13 37人阅读 评论(0) 收藏


看了别人的题解,毅然暴搜。


#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<string>
#include<cstdio>
#include<utility>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int MAXN = 1e3+5, BASE = 40;
char fc[BASE] = {"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

int n, b, ansl;
struct LongNumber
{
    char s[MAXN<<1];
    void prt()
    {
        for(int i = 0; i < n; i++)
          putchar(fc[s[i]]);
    }
};
LongNumber ans[BASE], tmp;

bool cmp(const LongNumber &a,const LongNumber &b){return strcmp(a.s,b.s) < 0;}

void DFS(int cur,int st)
{
    if(cur == n)  
    {  
        if(cur == 1 || tmp.s[cur-1])  
        {  
            char *p = ans[++ansl].s;
            for(int i = n - 1; i >= 0; i--)  
              p[i] = tmp.s[cur- i - 1];  
        }  
        return;
    }  
    for(int i = 0; i < b; i ++)  
    {  
        int cal = 0;  tmp.s[cur] = i;  
        for(int j = 0; j <= cur; j++)  
            cal += tmp.s[j]*tmp.s[cur-j];  

        if((st + cal)%b == i)  
            DFS(cur + 1, (st + cal)/b);  
    }  
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("sgu159.in","r",stdin);
    freopen("sgu159.out","w",stdout);
#endif

    std::cin >> b >> n ;    

    DFS(0,0);

    std::sort(ans+1,ans+ansl+1,cmp);

    printf("%d\n",ansl);
    for(int i = 1; i <= ansl; i++)
      ans[i].prt(), puts("");

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;           
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-05-14 13:13  <Dash>  阅读(142)  评论(0编辑  收藏  举报