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. pytho中pickle、json模块

    pickle & json 模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 json模块提供了四 ...

  2. 《TDD》-火花

    1,规定对天才来说多余,对蠢才来说无效,只对中间这一部分有用(我至今没见到过天才,蠢才到是不少) 2,设计上顿悟的火花一闪而过,没有规律可循.良好的测试无法保证你在需要的时候灵感乍现,但是给人信心的良 ...

  3. BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 9280  Solved: 2421 ...

  4. 转:GitHub 万星推荐成长技术清单

    转:http://www.4hou.com/info/news/7061.html 最近两天,在reddit安全板块和Twitter上有个GitHub项目很火,叫“Awesome Hacking”. ...

  5. web资料收集

    Web安全资料:https://github.com/CHYbeta/Web-Security-Learning http://blog.pentestbegins.com/2017/07/21/ha ...

  6. Electron:将前端应用打包成桌面应用

    首先戳我下载安装对应版本的node.js. 安装完成后,打开命令行输入node -v以及npm -v查看对应版本.能够正常显示说明安装成功. 写一个最简单的hello world的nodejs应用.n ...

  7. SQLSEVER 中的那些键和约束

    SQL Server中有五种约束类型,分别是 PRIMARY KEY约束.FOREIGN KEY约束.UNIQUE约束.DEFAULT约束.和CHECK约束.查看或者创建约束都要使用到 Microso ...

  8. [BZOJ4004][JLOI2015]装备购买(贪心+线性基)

    求最小权极大线性无关组. 先将所有向量按权值排序,从小到大依次判断,若能被前面已选向量线性表出则不选,这样一定最优. 据说是用拟阵来证明,但感性理解一下感觉比较显然,首先这样个数一定是最多的,其次对于 ...

  9. 【找规律】【DFS】Gym - 101174H - Pascal's Hyper-Pyramids

    二维下,如果把杨辉三角按照题目里要求的那样摆放,容易发现,第i行第j列的数(从0开始标号)是C(i+j,i)*C(j,j). 高维下也有类似规律,比如三维下,最后一层的数其实是C(i+j+k,i)*C ...

  10. SQL Server--CheckPoint

    http://www.cnblogs.com/TeyGao/category/526201.html