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
For each case, each line contains one integer N(0<= N <=109), indicating the given number.
#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的更多相关文章
- hdu 4394 Digital Square(bfs)
Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Digital Square(hdu4394)搜索
Digital Square Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Digital Square 搜索
Digital Square Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Subm ...
- hdu Digital Square(广搜)
题目:给出n,求出最小的m,满足m^2 % 10^k = n,其中k=0,1,2 http://acm.hdu.edu.cn/showproblem.php?pid=4394 只要有一个x满足条件便 ...
- HDU 1013 Digital Roots【字符串,水】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1013 Digital Roots(to_string的具体运用)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...
- HDU(4394),数论上的BFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4394 思路很巧妙,要找到m,可以这样思考,n的个位是有m的个位决定的,从0-9搜一遍,满足情况的话就继 ...
- Hdu 1404 Digital Deletions
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1404 刚开始想采取找规律的方法解题,可以没有发现规律.无奈,只好采用求PN点的方法. 我们假 ...
- HDU 1013 Digital Roots(字符串)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
随机推荐
- EXTJS 4.2 日期控件
{ xtype: "fieldcontainer", layout: "hbox", items: [{ fieldLabel: '开始时间', name: ' ...
- c# 赋值后最后一项数据部分丢失
赋值,赋值后 原因,在添加后立即调用clear()清除数据....
- UILabel 根据内容的多少来计算label的frame
self.label.text = @"...."; 计算 frame 的最新方法 //1.设置lable最大显示行数 self.label.numberOfLines = 0; ...
- git/ TortoiseGit 在bitbucket.org 使用证书登陆
背景:使用https协议在bitbucket中进行pull,push 时每次都要输入密码,比较麻烦还耽误时间,在网上找了下保存密码的方式 使用在用户环境变量中配置_netrc 文件的方式(http:/ ...
- linux 添加用户
1.下面就开始来说怎么添加新用户 我们以添加一个用户名为jorcen的新用户为例来说明,执行下面的命令: # useradd jorcen 2.那么就新用户就添加完成,但是没有任何信息,新用户添加完成 ...
- zoj 3725
题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...
- HDU4539+状态压缩DP
/* 题意:n行m列的矩阵,1表示可以放东西,0表示不可以.曼哈顿距离为2的两个位置最多只能有一个位置放东西. 问最多放多少个东西. */ #include<stdio.h> #inclu ...
- mysql-5.7.10-winx64 安装时遇到的问题
1.修改密码# /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &am ...
- DP的简单应用
Problem A:简单的图形覆盖 Time Limit:1000MS Memory Limit:65536KTotal Submit:201 Accepted:104 Description 有一 ...
- 李洪强iOS开发之静态库
iOS开发拓展篇—静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开源库 公开源代码,能看到具体实现 ...