题目链接: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. Openerp开发进销存系统完毕总结

      转自:http://blog.csdn.net/heartrude/article/details/9142463 安装Openoffice 在openoffice中安装openerp repor ...

  2. ACE调试中的一个小问题——ace_main_i无法链接

    初学ace没多久,今天遇到了一个小问题. 具体是:我在写完代码之后,编译老是出现一个错误. 提示: ACE_TEST1.obj : error LNK2019: 无法解析的外部符号 "int ...

  3. Unity 逻辑层通知UI 通过接口消除他们通信的switch判断

    需求如何:  用户名字长度为2-12之间,  如果错误在界面上弹出一个消息框 写烂一点 public void PostRename(string name) { if (string.IsNullO ...

  4. Java包装类的常量池

     Integer a=Integer.valueOf(100); Integer b=Integer.valueOf(100); System.out.println(a==b); Double d1 ...

  5. ubuntu下创建.net core时出现 Failed to create prime the NuGet cache

    https://docs.microsoft.com/en-us/aspnet/core/getting-started 根据微软给出的文档运行第一个web程序出现错误 Failed to creat ...

  6. AAAA block

    [self AAAA:^(BOOL isSuccessed, id userInfo, NSString *errorMsg) { NSLog(@"AAAA: %d, userInfo: % ...

  7. C语言控制台打印3D爱心图案

    非常多程序猿都认为自己的编程工作十分的鼓噪乏味.一整天面对的都是一些写不完的代码和改不完的Bug.今天我们要给大家分享一些有趣的C语言代码,也许能够为你无聊的工作带来一丝乐趣. 这些代码能够完毕几个不 ...

  8. shared-service.ts

    shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...

  9. 【转载】 使用rman进行坏块修复(ORA-01578、ORA-01110)

    [转自]http://blog.itpub.net/21256317/viewspace-1062055/ 使用rman进行坏块修复(ORA-01578.ORA-01110) 2012年的一天,处理的 ...

  10. apache与和mysql重启命令

    修改linux服务器的http配置之后,必须重启Apache服务. 命令为: /etc/rc.d/init.d/httpd restart chown -R mysql:mysql 目录名 改变文件属 ...