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

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

只是时间问题。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath> using namespace std;
typedef __int64 ll; struct Node
{
ll num;
int len;//长度
bool operator < (const Node &p) const
{
return p.num<num;
}
};
ll n,ans; ll kpow(int x)
{
ll kk=;
for(int i=;i<x;i++)
kk=kk*;
return kk;
}
bool bfs()
{
priority_queue<Node>Q;
Node p,q;
p.num=,p.len=;
Q.push(p);
while(!Q.empty())
{
p=Q.top();
Q.pop();
ll tmp=kpow(p.len);
if(p.num*p.num%tmp==n)
{
ans=p.num;
return true;
}
//扩展
for(int i=; i<; i++)
{
q.len=p.len+;
q.num=p.num+i*tmp;
if(q.num*q.num%(tmp*)==n%(tmp*))
Q.push(q);
}
}
return false;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
int temp=n%;
if(temp==||temp==||temp==||temp==)
{
puts("None");
continue;
}
if(bfs())
printf("%I64d\n",ans);
else
puts("None");
}
return ;
}

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

  1. Digital Square 搜索

    Digital Square Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  2. hdu 4394 Digital Square(bfs)

    Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 4394 Digital Square

    Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu Digital Square(广搜)

    题目:给出n,求出最小的m,满足m^2  % 10^k = n,其中k=0,1,2 http://acm.hdu.edu.cn/showproblem.php?pid=4394 只要有一个x满足条件便 ...

  5. 2012 Multi-University #10

    容斥原理 A Number Sequence 题意:给出n个数,b1,b2,b3……bn,构造n个数,a1,a2,……an(ai>1),使得a1*a2*a3……an=b1*b2……bn 分析:容 ...

  6. 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 ...

  7. ural 1698. Square Country 5(记忆化搜索)

    1698. Square Country 5 Time limit: 2.0 secondMemory limit: 64 MB The first arithmetical operation ta ...

  8. 杭电1518 Square(构成正方形) 搜索

    HDOJ1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

随机推荐

  1. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  2. C#6.0语言规范(六) 转换

    转换能够被视为是一个特定类型的表达式.转换可能会导致给定类型的表达式被视为具有不同的类型,或者它可能导致没有类型的表达式获取类型.转换可以是隐式或显式的,这决定了是否需要显式转换.例如,从类型int到 ...

  3. eslint 配置及规则说明

    中文官方网站 安装 可以全局安装,也可以在项目下面安装. 如下是在项目中安装示例,只需要在 package.json 中添加如下配置,并进行安装: “eslint”: “^4.11.0” 配置 配置方 ...

  4. Swift 里 Set (三)Inspecting a Set

    isEmpty /// A Boolean value that indicates whether the set is empty. @inlinable public var isEmpty: ...

  5. SpringBoot从入门到逆天(1)

    1.SpringBoot是什么? <1>为Sping开发提供一个更 快捷更广泛的入门体验. <2>开箱即用,不合适时特可以快速抛弃. <3>提供一系列大型项目常用的 ...

  6. C# SqlHelper类的数据库操作

    #region 私有构造函数和方法 private SqlHelper() { } /// <summary> /// 将SqlParameter参数数组(参数值)分配给SqlComman ...

  7. easyui datebox 只选择月份的方法

    easyui datebox 只选择月份的方法 效果如下图: 代码如下: <html > <head> <meta charset="utf-8"&g ...

  8. dubbo源码阅读之SPI

    dubbo SPI SPI,全程Service Provider interface, java中的一种借口扩展机制,将借口的实现类注明在配置文件中,程序在运行时通过扫描这些配置文件从而获取全部的实现 ...

  9. xamarin 遇到的奇葩问题

    未能找到路径“E:\platforms”的一部分. xamarin 安卓存档出现这样的错误 建议检查下ndk是否配置完整

  10. tensorflow进阶篇-5(反向传播1)

    这里将讲解tensorflow是如何通过计算图来更新变量和最小化损失函数来反向传播误差的:这步将通过声明优化函数来实现.一旦声明好优化函数,tensorflow将通过它在所有的计算图中解决反向传播的项 ...