HDU4394 Digital Square

分支限界法有三种策略,分别是FIFO、LIFO和LC(least cost)。BFS属于分支限界法的一种,通常采用FIFO策略,采用LIFO策略的情况比较少见,因为多数情况下这两种策略效果几乎相同。分支限界法采用LC策略时,通常用BFS+优先队列来实现。

问题链接:HDU4394 Digital Square

题意简述:输入测试用例数量t,输入t个正整数n,求最小的m,满足m^2%10^x=n,其中k=0,1,2,...。

问题分析:x是不定的,用暴力法试探m是不现实的。有关模除%的问题,可以用从低位开始逐步试探的方法,即先试探个位,然后十位、百位、千位等等。已知n的情况下,m的低位是有限的。例如n=21,那么m的个位只能是1或9,其他的数字是无法满足给定条件的。解决本问题需要明确以下几点:

1.对于m,从最低位开始试探,m的每位的数字可以是0-9,这时匹配n的最低位;

2.试探完1位再试探2位、3位和4位等等,分别匹配n的最低1位、2位、3位和4位等等;

3.m起始值:0,1,2,3,......;

4.显式剪枝条件:低位不匹配;

5.LC函数(优先函数):为了找到最小的满足条件的m,低位匹配的m中,值最小的节点优先展开。

程序中,每个节点node的成员变量说明如下:

curr:当前低位匹配的m值;

len:当前已经匹配的位数;

digit:当前位正在试探的数字(0-9)。

AC的C++语言程序如下:

/* HDU4394 Digital Square */

#include <iostream>
#include <queue>

using namespace std;

typedef unsigned long long ULL;

const int MAXN = 18;
const int MAXDIGIT = 9;

struct node {
    ULL curr;
    int len, digit;
    node(){}
    node(ULL c, int l, int n):curr(c), len(l), digit(n){}
    bool operator<(const node n) const {
        return curr > n.curr;
    }
};

node start;

int n;
ULL fact[18];
int nlen;
ULL ans;

void bfs()
{
    nlen = 0;
    ULL temp = n;
    while(temp) {
        nlen++;
        temp /= 10;
    }

    ans = 0;
    priority_queue<node> q;
    q.push(node(0, 0, 0));

    while(!q.empty()) {
        node top = q.top();
        q.pop();

        if(top.len == nlen) {
            ans = top.curr;
            break;
        } else if(top.digit != MAXDIGIT)
            q.push(node(top.curr, top.len, top.digit+1));

        node v;
        v.curr = top.curr + fact[top.len] * top.digit;
        v.len = top.len + 1;
        v.digit = 0;

        if(v.curr * v.curr % fact[v.len] == n % fact[v.len])
            q.push(v);
    }
}

int main()
{
    int t;

    fact[0] = 1;
    for(int i=1; i<MAXN; i++)
        fact[i] = fact[i-1] * 10;

    cin >> t;
    while(t--) {
        cin >> n;

        bfs();

        if(ans)
            printf("%llu\n", ans);
        else
            printf("None\n");
    }

    return 0;
}
时间: 2024-09-29 04:17:13

HDU4394 Digital Square的相关文章

Digital Square(hdu4394)搜索

Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1310 Accepted Submission(s): 501 Problem Description Given an integer N,you should come up with the minimum nonnegative integer M.M me

HDU 4394 Digital Square

Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1882    Accepted Submission(s): 741 Problem Description Given an integer N,you should come up with the minimum nonnegative integer

HDOJ 题目4394 Digital Square(DFS)

Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1757    Accepted Submission(s): 677 Problem Description Given an integer N,you should come up with the minimum nonnegative integer

hdu4393Digital Square(dfs)

Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1827 Accepted Submission(s): 714 Problem Description Given an integer N,you should come up with the minimum nonnegative integer M.M m

Project Euler 80:Square root digital expansion 平方根数字展开

Square root digital expansion It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all. The square root of two i

Project Euler #80: Square root digital expansion

1 from decimal import getcontext, Decimal 2 3 4 def main(): 5 n = int(raw_input()) 6 p = int(raw_input()) 7 8 getcontext().prec = p+10 # 扩大精度,保证接过 9 sum = 0 10 11 for i in range(1,n+1): 12 nTemp = Decimal(i).sqrt() 13 if nTemp._isinteger() : # 自生函数的判

ch1:android digital imaging:formats concepts and optimization

android digital image formats:lossless versus lossy GIF is not recommended in androd os , cause is lossless , it doesn't not away image data to achieve better compression results. JEPG is lossy digial image file format. it will thorws away image data

Sitecore Digital Marketing System, Part 1: Creating personalized, custom content for site visitors(自定义SiteCore中的 Item的Personalize的Condition) -摘自网络

Sitecore’s Digital Marketing System (DMS) can help you personalize the content your site displays to your users/visitors and can be easily customized to fit your specific requirements while using all the functionality that Sitecore already offers. Le

HDU1013 POJ1519 Digital Roots(解法三)

该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.入门练习题,用C语言编写程序. 问题简述:输入若干正整数,求其数根,直到输入为0为止. 问题分析:数根是指整数的各个位的数字之和.如果其和为1位整数,则为结果:如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止.这个问题的大陷阱是,没有指出整数是多少位的.即使使用unsignde long long类型,也可能会