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(广度优先搜索)

 
import java.io.*;
import java.util.*; public class Main {
public String str="+-*%";
public int n,m,k,sum,km;
public boolean boo[]=new boolean[1000*1000*10+1];
public Queue<Node1> list=new LinkedList<Node1>();
public static void main(String[] args) { new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
list.clear();
Arrays.fill(boo,false);
n=sc.nextInt();
k=sc.nextInt();
m=sc.nextInt();
if(n==0&&k==0&&m==0)
System.exit(0);
Node1 node=new Node1();
node.n=n;
node.s="";
sum=getMode(n+1,k);
km=m*k;
boo[getMode(n,km)]=true;
list.add(node);
BFS();
}
}
public void BFS(){
while(!list.isEmpty()){
Node1 node=list.poll();
if(getMode(node.n,k)==sum){
System.out.println(node.s.length());
System.out.println(node.s);
return;
}
for(int i=0;i<str.length();i++){
int temp=0;
if(str.charAt(i)=='+'){
temp=getMode(node.n+m,km);
}
else if(str.charAt(i)=='-'){
temp=getMode(node.n-m,km);
}
else if(str.charAt(i)=='*'){
temp=getMode(node.n*m,km);
}
else if(str.charAt(i)=='%'){
temp=getMode(getMode(node.n,m),km);
}
if(!boo[temp]){
boo[temp]=true;
Node1 t=node.getNode();
t.n=temp;
t.s=t.s+str.charAt(i);
list.add(t);
}
}
}
System.out.println(0);
}
public int getMode(int a,int b){
return (a%b+b)%b;
}
}
class Node1{
int n;
String s;
Node1(){
n=0;
s="";
}
public Node1 getNode(){
Node1 node=new Node1();
node.n=0;
node.s=s;
return node;
}
}

 

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. linux文件简单操作

    1.vim常用快捷键 dd/ndd 删除1行/删除n行 yy/nyy  复制1行/复制n行 p 粘贴 u 撤销 dw/ndw 删除一个单词/删除n个单词 G /nG  到一行尾/第n行尾 :!+命令  ...

  2. python之面向对象编程二

    类的成员 类的成员可以分为三大类:字段.方法.属性. 字段:普通字段.静态字段. 方法:普通方法.类方法.静态方法 属性:普通属性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多 ...

  3. 洛谷——P1208 [USACO1.3]混合牛奶 Mixing Milk

    P1208 [USACO1.3]混合牛奶 Mixing Milk 题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业 ...

  4. Spring和依赖注入的价值

    javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...

  5. jquery获取iframe里的元素

    var eleInIframe  = $("iframe").contents().find("#eleId"); 绑定事件: eleInIframe.clic ...

  6. 【BZOJ 4567】【SCOI 2016】背单词

    http://www.lydsy.com/JudgeOnline/problem.php?id=4567 贪心. 任何不用第一种情况的方案吃的泡椒数都小于\(n^2\),所以最小泡椒数的方案一定不包含 ...

  7. 【动态规划】mr354-坐车看球

    [题目大意] 两个球队的支持者要一起坐车去看球,他们已经排成了一列.我们要让他们分乘若干辆巴士,同一辆巴士上的人必须在队伍中是连续的.为了在车上不起冲突,希望两队的支持者人数尽量相等,差至多是D.有一 ...

  8. xml和集合混合使用-图书管理器

    package com.book; public class Book { private int id; //图书编号 private String name; //图书名称 private Str ...

  9. 浙南联合训练赛 H - The number of positions

    Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...

  10. Android Content Provider Security(转)

    四大组件之一-content provider安全详解 原帖地址:http://drops.wooyun.org/tips/4314 0x00 科普 内容提供器用来存放和获取数据并使这些数据可以被所有 ...