题目链接: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个数字的。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<string>
  6. using namespace std;
  7. #define MAXN 77777
  8.  
  9. struct Node{
  10. int num,val,pre,len;
  11. }que[MAXN];
  12.  
  13. int number[];
  14. int n,k,min_len,length,flag,End;
  15. string ans,tmp;
  16. bool mark[MAXN];
  17.  
  18. bool bfs(int m)
  19. {
  20. Node now,next;
  21. memset(mark,false,sizeof(mark));
  22. int head=,tail=-;
  23. for(int i=;i<=m;i++)if(number[i]){
  24. now.len=;
  25. now.num=number[i];
  26. now.pre=-;
  27. now.val=number[i]%n;
  28. que[++tail]=now;
  29. mark[now.val]=true;
  30. }
  31. while(head<=tail){
  32. now=que[head];
  33. if(now.len>min_len)break;
  34. if(now.val==){
  35. End=head;
  36. length=now.len;
  37. return true;
  38. }
  39. for(int i=;i<=m;i++){
  40. next=now;
  41. next.val=(now.val*k+number[i])%n;
  42. if(!mark[next.val]){
  43. mark[next.val]=true;
  44. next.len++;
  45. next.num=number[i];
  46. next.pre=head;
  47. que[++tail]=next;
  48. }
  49. }
  50. head++;
  51. }
  52. return false;
  53. }
  54.  
  55. void Get_String(int k)
  56. {
  57. if(k==-)return ;
  58. Get_String(que[k].pre);
  59. tmp+=(que[k].num+'');
  60. }
  61.  
  62. int main()
  63. {
  64. while(~scanf("%d%d",&n,&k)){ //input整数n和k进制
  65. ans="";
  66. min_len=;
  67. flag=;
  68. for(int i=;i<k;i++){ //先搜索只有1个数字的
  69. number[]=i;
  70. if(bfs()){
  71. flag=;
  72. tmp="";
  73. Get_String(End);
  74. if(length<min_len||(length==min_len&&tmp<ans)){
  75. ans=tmp;
  76. min_len=length;
  77. }
  78. }
  79. }
  80. if(flag){
  81. cout<<ans<<endl;
  82. continue;
  83. }
  84. for(int i=;i<k;i++){ //搜索2个数字的
  85. number[]=i;
  86. for(int j=i+;j<k;j++){
  87. number[]=j;
  88. if(bfs()){
  89. tmp="";
  90. Get_String(End);
  91. if(length<min_len||(length==min_len&&tmp<ans)){
  92. ans=tmp;
  93. min_len=length;
  94. }
  95. }
  96. }
  97. }
  98. cout<<ans<<endl;
  99. }
  100. return ;
  101. }

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. v-for设置键值 key

    总是用 key 配合 v-for.在组件上_总是_必须用 key 配合 v-for,以便维护内部组件及其子树的状态. <ul> <li v-for="todo in tod ...

  2. java String->float,float->int

    类型转换代码 : String sourceStr = "0.0"; String类型 float sourceF = Float.valueOf(sourceStr); floa ...

  3. MVC 强类型传值Model。和弱类型传值ViewData[&quot;&quot;]。及用EF进行增删查改(母版页的使用)

    <1> 控制器 </pre><pre name="code" class="csharp">using MvcTest.Mo ...

  4. Android自动化测试之MonkeyRunner录制和回放脚本

    Android自动化测试之MonkeyRunner录制和回放脚本(十一) 分类: 自动化测试 Android自动化 2013-02-22 10:57 7346人阅读 评论(2) 收藏 举报 andro ...

  5. Git-在一个电脑上同时使用两个Git的账号

    前言 又需要登录公司的账号,又想在电脑上使用自己的账号. 实现 首先是git config方面的设置,要取消掉原本对于git账号的全局设置. git config --global --unset u ...

  6. iperf使用

    1. sourceforge搜索iperf下载 2. ./configure make make install 3. server:iperf -s -p 12345 -i 1 -M: client ...

  7. NoSQL(三)

    redis集群介绍 1.官方叫cluster,redis3.0才直接的一个架构,如果数据量很大,单台机器已经无法满足存储,查询的瓶颈,所以我们需要多台机器构成一个大集群,用来解决存储空间,查询速度,负 ...

  8. shared-service.ts

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

  9. DDR 复位

    将FPGA代码和实际的数字电路对应起来. always @ (negedge clk_ref_200)     begin     if(ddr3_init_done)    'b10)   'b10 ...

  10. Docker使用Dockerfile创建支持ssh服务自启动的容器镜像

    原文链接:Docker使用Dockerfile创建支持ssh服务自启动的容器镜像 1. 首先创建一个Dockerfile文件.文件内容例如以下 # 选择一个已有的os镜像作为基础 FROM cento ...