题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294

思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不同数字最小。首先明确最多的不同数字需要2种,证明如下a,aa,aaa,aaaa,......找出n个连续的,那么中间至少有两个mod n的值是相等的,那么这两个数字相减,得到aaa...000肯定能被n整除,因此可以bfs搜索只含有1个数字的和只含有2个数字的。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
#define MAXN 77777 struct Node{
int num,val,pre,len;
}que[MAXN]; int number[];
int n,k,min_len,length,flag,End;
string ans,tmp;
bool mark[MAXN]; bool bfs(int m)
{
Node now,next;
memset(mark,false,sizeof(mark));
int head=,tail=-;
for(int i=;i<=m;i++)if(number[i]){
now.len=;
now.num=number[i];
now.pre=-;
now.val=number[i]%n;
que[++tail]=now;
mark[now.val]=true;
}
while(head<=tail){
now=que[head];
if(now.len>min_len)break;
if(now.val==){
End=head;
length=now.len;
return true;
}
for(int i=;i<=m;i++){
next=now;
next.val=(now.val*k+number[i])%n;
if(!mark[next.val]){
mark[next.val]=true;
next.len++;
next.num=number[i];
next.pre=head;
que[++tail]=next;
}
}
head++;
}
return false;
} void Get_String(int k)
{
if(k==-)return ;
Get_String(que[k].pre);
tmp+=(que[k].num+'');
} int main()
{
while(~scanf("%d%d",&n,&k)){ //input整数n和k进制
ans="";
min_len=;
flag=;
for(int i=;i<k;i++){ //先搜索只有1个数字的
number[]=i;
if(bfs()){
flag=;
tmp="";
Get_String(End);
if(length<min_len||(length==min_len&&tmp<ans)){
ans=tmp;
min_len=length;
}
}
}
if(flag){
cout<<ans<<endl;
continue;
}
for(int i=;i<k;i++){ //搜索2个数字的
number[]=i;
for(int j=i+;j<k;j++){
number[]=j;
if(bfs()){
tmp="";
Get_String(End);
if(length<min_len||(length==min_len&&tmp<ans)){
ans=tmp;
min_len=length;
}
}
}
}
cout<<ans<<endl;
}
return ;
}

hdu 4294(bfs)的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  3. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  4. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  5. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  6. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  7. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  8. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  9. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

随机推荐

  1. Cannot make a static reference to the non-static method的解决方法

    报错原因:在一个类中写了一个public String getContent()方法和一个main()方法,getContent()方法中包含了getClass()方法,在main()方法中直接调用了 ...

  2. 03-spring学习-属性配置细节

    配置bean的一些细节 字面值 如果包含特殊符号,直接写会报错.可以用这个<![CDATA[]]>包裹起来. 比如这里的配置属性里面的value值包含<>等特殊符号,直接写会报 ...

  3. jrebel 7免费激活(非破解) 和 IntelliJ Idea 2017 免费激活方法

    转自http://www.cnblogs.com/suiyueqiannian/p/6734412.html http://www.cnblogs.com/suiyueqiannian/p/67540 ...

  4. 人工智能 VS 机器学习 VS 深度学习

    (原文:) The Difference Between AI, Machine Learning, and Deep Learning? (译文:) 人工智能 . 机器学习 和 深度学习的区别? 作 ...

  5. Image.Save出错 GDI 一般错误

    一般是路径问题 需要转化为绝对路径 \转换为/ if (HttpContext.Current!=null) outPath = HttpContext.Current.Server.MapPath( ...

  6. STL容器分析--set

    STL 对这个序列可以进行查找,插入删除序列中的任意一个元素,而完成这些操作的时间同这个序列中元素个数的对数成比例关系,并且当游标指向一个已删除的元素时,删除操作无效.而一个经过更正的和更加实际的定义 ...

  7. Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)

    D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...

  8. ado连接sql server

    //ado连接sql server //头文件加上以下这句. #import "C:\Windows\system\msado15.dll" no_namespace rename ...

  9. CefSharp 集成谷歌浏览器详解---(一)环境搭建(没测试过,不知道好不好用)

    https://blog.csdn.net/zpyxman/article/details/78538808

  10. CCNA2.0笔记_IPv4

    ipv4对于OSI是个网络层协议,对于TCP/IP是个Internet层协议 ipv4是一个无连接/尽力传输协议 Ipv4定义了两大类广播地址 1,全向广播:255.255.255.255 2,定向广 ...