HDU4394 Digital Square【分支限界法BnB】

Digital Square

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2062    Accepted Submission(s): 830


Problem Description
Given an integer N,you should come up with the minimum nonnegative integer M.M meets the follow condition: M 2%10 x=N (x=0,1,2,3....)
 

Input
The first line has an integer T( T< = 1000), the number of test cases.
For each case, each line contains one integer N(0<= N <=10 9), indicating the given number.
 

Output
For each case output the answer if it exists, otherwise print “None”.
 

Sample Input
  
  
3 3 21 25
 

Sample Output
  
  
None 11 5
 

Source


问题链接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)。


题记

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



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;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值