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 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
 
Source
 
 
 
解析:设M = abc,即M = 100*a+10*b+c,则M2 = 10000*a2 + 1000*2*a*b + 100*(b2+2*a*c) + 10*2*b*c + c2。易知,M2的个位只与M的最后1位有关,M2的十位只与M的最后2位有关,M2的百位只与M的最后3位有关……我们可以从个位开始进行广搜,逐位满足N。如果有可行解,找出这一层当中最小的解并输出;否则输出"None"。
 
 
 
 #include <cstdio>
#include <queue>
using namespace std; struct node{
long long value,place;
}; void bfs(long long n)
{
node tmp;
tmp.value = ;
tmp.place = ;
queue<node> q;
q.push(tmp);
bool findans = false;
long long ans = 0xffffffff;
while(!q.empty()){
tmp = q.front();
q.pop();
node now;
now.place = tmp.place*;
for(int i = ; i<; ++i){
now.value = tmp.value+i*tmp.place;
if(now.value*now.value%now.place == n%now.place){
if(!findans)
q.push(now);
if(now.value*now.value%now.place == n && now.value<ans){
findans = true;
ans = now.value;
}
}
}
}
if(ans == 0xffffffff)
printf("None\n");
else
printf("%I64d\n",ans);
} int main()
{
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%I64d",&n);
bfs(n);
}
return ;
}

上面的代码找到可行解之后需要进行比较找出最优解,我们可以对此进行优化,把queue改为priority_queue,让priority_queue帮我们完成这个工作,使得找到的第一个可行解便是满足题意的最优解。

 #include <cstdio>
#include <queue>
using namespace std; struct node{
long long value,place;
bool operator < (const node& b)const
{
return value>b.value;
}
}; void bfs(long long n)
{
node tmp;
tmp.value = ;
tmp.place = ;
priority_queue<node> q;
q.push(tmp);
while(!q.empty()){
tmp = q.top();
q.pop();
if(tmp.value*tmp.value%tmp.place == n){
printf("%I64d\n",tmp.value);
return ;
}
node now;
now.place = tmp.place*;
for(int i = ; i<; ++i){
now.value = tmp.value+i*tmp.place;
if(now.value*now.value%now.place == n%now.place){
q.push(now);
}
}
}
printf("None\n");
} int main()
{
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%I64d",&n);
bfs(n);
}
return ;
}

HDU 4394 Digital Square的更多相关文章

  1. hdu 4394 Digital Square(bfs)

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

  2. Digital Square(hdu4394)搜索

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

  3. Digital Square 搜索

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

  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. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDU(4394),数论上的BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394 思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继 ...

  8. Hdu 1404 Digital Deletions

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1404 刚开始想采取找规律的方法解题,可以没有发现规律.无奈,只好采用求PN点的方法. 我们假 ...

  9. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

随机推荐

  1. EXTJS 4.2 日期控件

    { xtype: "fieldcontainer", layout: "hbox", items: [{ fieldLabel: '开始时间', name: ' ...

  2. c# 赋值后最后一项数据部分丢失

    赋值,赋值后 原因,在添加后立即调用clear()清除数据....

  3. UILabel 根据内容的多少来计算label的frame

    self.label.text = @"...."; 计算 frame 的最新方法 //1.设置lable最大显示行数 self.label.numberOfLines = 0; ...

  4. git/ TortoiseGit 在bitbucket.org 使用证书登陆

    背景:使用https协议在bitbucket中进行pull,push 时每次都要输入密码,比较麻烦还耽误时间,在网上找了下保存密码的方式 使用在用户环境变量中配置_netrc 文件的方式(http:/ ...

  5. linux 添加用户

    1.下面就开始来说怎么添加新用户 我们以添加一个用户名为jorcen的新用户为例来说明,执行下面的命令: # useradd jorcen 2.那么就新用户就添加完成,但是没有任何信息,新用户添加完成 ...

  6. zoj 3725

    题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...

  7. HDU4539+状态压缩DP

    /* 题意:n行m列的矩阵,1表示可以放东西,0表示不可以.曼哈顿距离为2的两个位置最多只能有一个位置放东西. 问最多放多少个东西. */ #include<stdio.h> #inclu ...

  8. mysql-5.7.10-winx64 安装时遇到的问题

    1.修改密码# /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &am ...

  9. DP的简单应用

    Problem A:简单的图形覆盖 Time Limit:1000MS  Memory Limit:65536KTotal Submit:201 Accepted:104 Description 有一 ...

  10. 李洪强iOS开发之静态库

    iOS开发拓展篇—静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开源库 公开源代码,能看到具体实现 ...