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 meets the follow condition:
M2%10x=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 <=109), 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

题意很简单 M2%10x=N (x=0,1,2,3....)就不啰嗦了,

思路:

  可以用dfs;(其实就是暴力+剪枝)

eg:n 为 21; 那么,我们从个位数1开始搜, 1 * 1 = 1, 9 * 9 = 81,他们的个位数都是1,那么,1和9可以作为我们找的那个数的个位数。
然后我们就记忆一下这个我们找到的东西,放在ans里面。
下面我们开始搜第二位,设搜的是ab的a(其中,我们的b是已经记忆下来的个位)。
ab * ab % 100 = 21; 那么,我们的2 是不是就由(b * b / 10 + a * b * 2) % 10 得到的(看不出来的可以拿纸笔算一下;)。未知数就只有a吧,那么,我们就for一遍去找a,找到a了,我们就以同样的方法去找c(如果有c的话)。这样,就是剪枝了。
最后,如果我们找的n有x位,那么,我们要找的m * m % ? == n 的m最多也只有x位。至此,就结束了。

ps:http://acm.hdu.edu.cn/showproblem.php?pid=4394

详见代码

#include <cstdio>
#include<iostream>
#define INF 0xfffffff
#define ll __int64
using namespace std;

ll t,digit[20],num,n,ans,r,final;

ll min(ll a, ll b)
{
    return a > b? b : a;
}
void get(ll t) //得到num = n的位数,digit[]存每个位上分别是什么。
{
    num=0;
    while(t)
    {
        digit[ ++num] = t % 10;
        t/=10;
    }
}
void solve(ll p,ll w,ll ans)
{
    if(p>num)
    {
        final=min(final,ans);
        return ;
    }
    for(ll k = 0;k<10;k ++)
    {
        if( (ans*ans/w + r*k%10)%10==digit[p])//(b * b / 10 + a * b * 2) % 10,,,r=2*ans
            solve(p+1,w*10,k*w+ans);
    }
}
int main()
{
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        get(n);
        if(digit[1]==2||digit[1]==3||digit[1]==7||digit[1]==8)
        {
            puts("None");
            continue;
        }
        final = INF;
        for(ll i=0;i<10;i++)
        {
            if(i*i%10==digit[1])
            {
                r=i<<1;   //r为余数2*ans
                solve(2,10,i);
            }
        }
        if(final==INF)
            puts("None");
        else
            printf("%I64d\n",final);
    }
    return 0;
}

当然也可以不用这么麻烦直接dfs

只是时间问题。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<cmath>
 6
 7
 8 using namespace std;
 9 typedef __int64 ll;
10
11
12
13 struct Node
14 {
15     ll num;
16     int len;//长度
17     bool operator < (const Node &p) const
18     {
19         return p.num<num;
20     }
21 };
22 ll n,ans;
23
24 ll kpow(int x)
25 {
26     ll kk=1;
27     for(int i=0;i<x;i++)
28         kk=kk*10;
29     return kk;
30 }
31 bool bfs()
32 {
33     priority_queue<Node>Q;
34     Node p,q;
35     p.num=0,p.len=0;
36     Q.push(p);
37     while(!Q.empty())
38     {
39         p=Q.top();
40         Q.pop();
41         ll tmp=kpow(p.len);
42         if(p.num*p.num%tmp==n)
43         {
44             ans=p.num;
45             return true;
46         }
47         //扩展
48         for(int i=0; i<10; i++)
49         {
50             q.len=p.len+1;
51             q.num=p.num+i*tmp;
52             if(q.num*q.num%(tmp*10)==n%(tmp*10))
53                 Q.push(q);
54         }
55     }
56     return false;
57 }
58
59
60 int main()
61 {
62     int T;
63     scanf("%d",&T);
64     while(T--)
65     {
66         scanf("%I64d",&n);
67         int temp=n%10;
68         if(temp==2||temp==3||temp==7||temp==8)
69         {
70             puts("None");
71             continue;
72         }
73         if(bfs())
74             printf("%I64d\n",ans);
75         else
76             puts("None");
77     }
78     return 0;
79 }

Digital Square(hdu4394)搜索

时间: 2024-08-05 11:27:42

Digital Square(hdu4394)搜索的相关文章

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是不现实的.有关模除%的问题,可以

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

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

HDU 1518 Square 搜索

Square Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 11   Accepted Submission(s) : 5 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Given a set of sticks of vario

POJ 2362:Square 觉得这才算深度搜索

Square Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21821   Accepted: 7624 Description Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the nu

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() : # 自生函数的判