首页 > ACM题库 > HDU-杭电 > HDU 2840- Self-Replicating Numbers-DFS-[解题报告]HOJ
2014
02-17

HDU 2840- Self-Replicating Numbers-DFS-[解题报告]HOJ

Self-Replicating Numbers

问题描述 :

Sherlock is fond of playing with numbers. Two days ago he discovered that 9376^2 = 87909376 – the last four digits constitute 9376 again. He called such numbers self-replicating.

More precisely, an n-digit number is called self-replicating if it is equal to the number formed by the last n digits of its square. Now Sherlock often asks Xay to help him to find new such numbers. To make the things worse, Sherlock already knows what the scales of notation are, so he asks Xay to find, for example, hexadecimal or binary self-replicating numbers.

Xay wants to help Sherlock, but unfortunately he is very busy now: he is seriously preparing and training for the next ACM Regional Contest. So he asked you to write a program that for a given base b and length n will find all n-digit self-replicating numbers in the scale of notation with base b.

输入:

there are multiple test cases. one line of each test case contains two integer numbers b and n separated by a single space, the base b of the scale of notation (2 ≤ b ≤ 36) and the required length n (1 ≤ n ≤ 2000).

输出:

there are multiple test cases. one line of each test case contains two integer numbers b and n separated by a single space, the base b of the scale of notation (2 ≤ b ≤ 36) and the required length n (1 ≤ n ≤ 2000).

样例输入:

2 1
10 4

样例输出:

2
0
1
1
9376

题目连接:

第一题:http://acm.swust.edu.cn/oj/problem/485/

第二题:http://poj.org/problem?id=2205

两题都是自守数问题。

第一题是简单的十进制的自守数判断:

以376为例
376 被乘数
X 376 乘数
———-
2256 第一个部分积=被乘数*乘数的倒数第一位
2632 第二个部分积=被乘数*乘数的倒数第二位
1128 第三个部分积=被乘数*乘数的倒数第三位
———-
141376 积
本问题所关心的是积的最后三位。分析产生积的后三位的过程,可以看出,在每一次的部分积中,并不是它的每一位都会对积的后三位产生影响。总结规律可以得到:在三位数乘法中,对积的后三位产生影响的部分积分别为:
第一个部分积中:被乘数最后三位*乘数的倒数第一位
第二个部分积中:被乘数最后二位*乘数的倒数第二位
第三个部分积中:被乘数最后一位*乘数的倒数第三位
将以上的部分积的后三位求和后截取后三位就是三位数乘积的后三位。这样的规律可以推广到同样问题的不同位数乘积。

代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;


int main()
{
/*#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif*/
    int n;
    while(scanf(" %d",&n)!=EOF)
    {
        //数的位数
        int k = 1;
        if(n == 0 || n == 1)
        {
            printf("NO\n");
            continue;
        }
        int temp = n;
        while(temp/10!=0)
        {
            temp /= 10;
            k *= 10;
        }
        //求余的系数
        int kk = k*10;
        k = kk;
        int part = 0;
        int mul = 10;
        while(k>=10)
        {
            //被乘数
            int a = n%k;
            //乘数
            int b = ((n - (n/mul)*mul)/(mul/10))*(mul/10);
            //部分积
            part += (a*b)%kk;
            part %=kk;
            k /=10;
            mul *=10;
        }
        if(n == part)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

第二题要处理N进制K位的自守数求解问题。

对所有情况从低位到高位搜索遍历即可。

参考:http://hi.baidu.com/yxdark/item/3b4648c7dd0e0a57bcef6948

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

struct Node
{
    char self[2005];
};
Node p[1000];
int a[2005];
int m;
int b,n;

char Change(int x)
{
    if (x<=9)
        return '0'+x;
    else
        return 'A'+x-10;
}
//从低位往高位搜索
void dfs(int dep,int sum)
{
    if(dep>n)
    {
        if(a[n]>0 || n == 1)
        {
            for(int i=n; i>=1; i--)
            {
                p[m].self[n-i] = Change(a[i]);
            }
            p[m].self[n] = '\0';
            m++;
        }
        return;
    }
    int tol = 0;
    for(a[dep]=0; a[dep]<b; a[dep]++)
    {
        tol = 0;
        for(int i=1; i<=dep; i++)
        {
            int j = dep-i+1;
            tol += a[i]*a[j];
        }
        if((tol+sum)%b == a[dep])
        {
            dfs(dep+1,(tol+sum)/b);
        }
    }
}
bool cmp(Node a,Node b)
{
    return strcmp(a.self,b.self)<0;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    while(scanf(" %d %d",&b,&n)!=EOF)
    {
        m = 0;
        dfs(1,0);
        sort(p,p+m,cmp);
        printf("%d\n",m);
        for(int i=0; i<m; i++)
        {
            printf("%s\n",p[i].self);
        }
    }
    return 0;
}

解题参考:http://blog.csdn.net/niuox/article/details/8716747


,
  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.