Remainder

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2260 Accepted Submission(s): 481
Problem Description
Coco is a clever boy, who is good at mathematics. However, he is puzzled by a difficult mathematics problem. The problem is: Given three integers N, K and M, N may adds (‘+’) M, subtract (‘-‘) M, multiples (‘*’) M or modulus (‘%’) M (The definition of ‘%’ is given below), and the result will be restored in N. Continue the process above, can you make a situation that “[(the initial value of N) + 1] % K” is equal to “(the current value of N) % K”? If you can, find the minimum steps and what you should do in each step. Please help poor Coco to solve this problem.

You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.

 
Input
There are multiple cases. Each case contains three integers N, K and M (-1000 <= N <= 1000, 1 < K <= 1000, 0 < M <= 1000) in a single line.

The input is terminated with three 0s. This test case is not to be processed.

 
Output
For each case, if there is no solution, just print 0. Otherwise, on the first line of the output print the minimum number of steps to make “[(the initial value of N) + 1] % K” is equal to “(the final value of N) % K”. The second line print the operations to do in each step, which consist of ‘+’, ‘-‘, ‘*’ and ‘%’. If there are more than one solution, print the minimum one. (Here we define ‘+’ < ‘-‘ < ‘*’ < ‘%’. And if A = a1a2...ak and B = b1b2...bk are both solutions, we say A < B, if and only if there exists a P such that for i = 1, ..., P-1, ai = bi, and for i = P, ai < bi)

 
Sample Input
2 2 2
-1 12 10
0 0 0
 
Sample Output
0
2
*+

BFS(广度优先搜索)

 
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public String str="+-*%";
  6. public int n,m,k,sum,km;
  7. public boolean boo[]=new boolean[1000*1000*10+1];
  8. public Queue<Node1> list=new LinkedList<Node1>();
  9. public static void main(String[] args) {
  10.  
  11. new Main().work();
  12. }
  13. public void work(){
  14. Scanner sc=new Scanner(new BufferedInputStream(System.in));
  15. while(sc.hasNext()){
  16. list.clear();
  17. Arrays.fill(boo,false);
  18. n=sc.nextInt();
  19. k=sc.nextInt();
  20. m=sc.nextInt();
  21. if(n==0&&k==0&&m==0)
  22. System.exit(0);
  23. Node1 node=new Node1();
  24. node.n=n;
  25. node.s="";
  26. sum=getMode(n+1,k);
  27. km=m*k;
  28. boo[getMode(n,km)]=true;
  29. list.add(node);
  30. BFS();
  31. }
  32. }
  33. public void BFS(){
  34. while(!list.isEmpty()){
  35. Node1 node=list.poll();
  36. if(getMode(node.n,k)==sum){
  37. System.out.println(node.s.length());
  38. System.out.println(node.s);
  39. return;
  40. }
  41. for(int i=0;i<str.length();i++){
  42. int temp=0;
  43. if(str.charAt(i)=='+'){
  44. temp=getMode(node.n+m,km);
  45. }
  46. else if(str.charAt(i)=='-'){
  47. temp=getMode(node.n-m,km);
  48. }
  49. else if(str.charAt(i)=='*'){
  50. temp=getMode(node.n*m,km);
  51. }
  52. else if(str.charAt(i)=='%'){
  53. temp=getMode(getMode(node.n,m),km);
  54. }
  55. if(!boo[temp]){
  56. boo[temp]=true;
  57. Node1 t=node.getNode();
  58. t.n=temp;
  59. t.s=t.s+str.charAt(i);
  60. list.add(t);
  61. }
  62. }
  63. }
  64. System.out.println(0);
  65. }
  66. public int getMode(int a,int b){
  67. return (a%b+b)%b;
  68. }
  69. }
  70. class Node1{
  71. int n;
  72. String s;
  73. Node1(){
  74. n=0;
  75. s="";
  76. }
  77. public Node1 getNode(){
  78. Node1 node=new Node1();
  79. node.n=0;
  80. node.s=s;
  81. return node;
  82. }
  83. }

 

HDU 1104 Remainder (BFS(广度优先搜索))的更多相关文章

  1. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  2. hdu - 1104 Remainder (bfs + 数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...

  3. BFS广度优先搜索 poj1915

    Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...

  4. 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想

    dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...

  5. 图的遍历BFS广度优先搜索

    图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...

  6. 算法竞赛——BFS广度优先搜索

    BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...

  7. HDU 1104 Remainder( BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. 步步为营(十六)搜索(二)BFS 广度优先搜索

    上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...

  9. GraphMatrix::BFS广度优先搜索

    查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...

随机推荐

  1. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

  2. POJ 1860 Currency Exchange【SPFA判环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  3. POJ 1182 食物链 【带权并查集/补集法】

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...

  4. 解决PHPExcel长数字串显示为科学计数

    在excel中如果在一个默认的格中输入或复制超长数字字符串,它会显示为科学计算法,例如身份证号码,解决方法是把表格设置文本格式或在输入前加一个单引号. 使用PHPExcel来生成excel,也会遇到同 ...

  5. zoj 1375||poj 1230(贪心)

    Pass-Muraille Time Limit: 2 Seconds      Memory Limit: 65536 KB In modern day magic shows, passing t ...

  6. HDU 6040 Hints of sd0061(划分高低位查找)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6040 [题目大意] 给出一个随机数生成器,有m个询问,问第bi小的元素是啥 询问中对于bi< ...

  7. 企鹅----sap+裂点

    企鹅 题目描述 在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃,但是它们的跳跃距离,有一 ...

  8. vue-cli创建vue项目

    原文出处:https://segmentfault.com/a/1190000008922234 第一步 node环境安装 1.1 如果本机没有安装node运行环境,请下载node 安装包进行安装1. ...

  9. PHP与Web页面的交互

    1.form表单默认情况下提交数据的方式为get方式. 2.PHP脚本用来处理表单数据的预定义变量是$_GET,$_POST(区分大小写) 代码示例:(特别注意复选框属性name的时候加数组) sim ...

  10. HDU 4584 Building bridges (水题)

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...