Remainder

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4078    Accepted Submission(s): 1014

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 1
0
0 0 0
 
Sample Output
0
2
*+
 /*
     Name: hdu--1104--Remainder
     Copyright: ©2017 日天大帝
     Author: 日天大帝
     Date: 22/04/17 09:11
     Description: bfs
                 a = b * q + r (q > 0 and 0 <= r < q),
                 题上的取余运算和%运算不一样,%运算能产生负值所以要 (n%k+k)%k这样,才等于题意的取余
                 这个题用vis标记产生过的数据,同时用%km和%k进行剪枝优化 

 */
 #include<cstring>
 #include<iostream>
 #include<queue>
 #include<string>
 using namespace std;
 struct node{
     int num,steps;
     string str;
 };
 ;
 bool vis[MAX];
 int n,m,k,mk,final_cmp;
 void bfs(){
     node start;
     start.num = n;
     start.str = "";
     start.steps = ;
     vis[(n%k+k)%k] = ;
     queue<node> q;
     q.push(start);

     while(!q.empty()){
         node a,temp = q.front();q.pop();
         if(final_cmp == ((temp.num%k)+k)%k){
             cout<<temp.steps<<endl<<temp.str<<endl;
             return ;
         }
         ; i<; ++i){
             a = temp;
             ){
                 a.num += m;
                 a.str += '+';
             }){
                 a.num -= m;
                 a.str += '-' ;
             }){
                 a.num *= m;
                 a.str += '*';
             }else{
                 a.num = (a.num%m+m)%m;
                 a.str += '%' ;
             }
             a.num %= mk;//%k之后%m结果就错啦,10%(15) !=10%3%5
             if(vis[(a.num%k+k)%k])continue;//%k缩小范围剪枝
             a.steps++;
             vis[(a.num%k+k)%k] = ;
             q.push(a);
         }
     }
     cout<<"<<endl;
 }
 int main(){
     ios::sync_with_stdio(false);

     while(cin>>n>>k>>m,n||m||k){//输入有正负不能用n+m+k
         memset(vis,,sizeof(vis));
         mk = m*k;
         final_cmp = ((n+)%k+k)%k;
         bfs() ;
     }
     ;
 }

hdu--1104--Remainder(简单的bfs)的更多相关文章

  1. HDU 1104 Remainder (BFS)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1104 题意:给你一个n.m.k,有四种操作n+m,n-m,n*m,n%m,问你最少经过多少步,使得最后 ...

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

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

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

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

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

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

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

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

  6. HDU 1104 Remainder (BFS求最小步数 打印路径)

    题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...

  7. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

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

  8. HDU 1104 Remainder

    与前一题类似,也是BFS+记录路径, 但是有很多BUG点, 第一MOD操作与%不同i,其实我做的时候注意到了我们可以这样做(N%K+K)%K就可以化为正数,但是有一点要注意 N%K%M!=N%M%K; ...

  9. HDU 1253:胜利大逃亡(简单三维BFS)

    pid=1253">胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/ ...

  10. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

随机推荐

  1. 添加保存less报错

    编辑器在添加保存less文件弹出一下错误: re-evaluation native module sources is not supported,if you are using the grac ...

  2. java中得到图片的宽度 高度:

    java中得到图片的宽度 高度:BufferedImage srcImage = null;srcImage = ImageIO.read(new File(srcImagePath));int sr ...

  3. URL重定向

    /** * URL重定向 * @param string $url 重定向的URL地址 * @param integer $time 重定向的等待时间(秒) * @param string $msg ...

  4. Java NIO 核心组件学习笔记

    背景知识 同步.异步.阻塞.非阻塞 首先,这几个概念非常容易搞混淆,但NIO中又有涉及,所以总结一下[1]. 同步:API调用返回时调用者就知道操作的结果如何了(实际读取/写入了多少字节). 异步:相 ...

  5. 【请求之密】payload和formData有什么不同?

    最近做项目的时候,在通过post请求向服务端发送数据的时候,请求失败了.错误信息如下: 返回的400(bad request)错误,说明客户端这边发送的请求是有问题的. 和通过jquery中的ajax ...

  6. python webserver, based on SimpleHTTPServer

    #-*- coding:utf-8 -*- #author: lichmama #email: nextgodhand@163.com #filename: httpd.py import io im ...

  7. Python数据分析之路(一)查询和统计

    0. 如何入门数据分析 关注沙漠之鹰的同学一定看过沙漠君写得很多篇数据分析文章,比如分析房价,车价,预测机动车摇号这些话题.其实文章中所有的分析都使用了Python和它非常强大的数据分析库Pandas ...

  8. 如何生成Azure SAS Token

    在Azure PaaS服务密钥的安全性文章中,提到过客户端实际上发送的是Token,而不是密钥.那么Token是该如何生成呢? Azure相应服务的SDK其实都提供了或者内置了生成Token的方法,可 ...

  9. shell脚本交互:expect学习笔记及实例详解

    最近项目需求,需要写一些shell脚本交互,管道不够用时,expect可以很好的实现脚本之间交互,搜索资料,发现网上好多文章都是转载的,觉得这篇文章还不错,所以简单修改之后拿过来和大家分享一下~ 1. ...

  10. STS 配置tomcat以后,无法访问

    问题 今天在新环境下安装开发环境STS,使用的的3.9,如下图 安装完成之后配置Tomcat,在STS启动Tocat后,在浏览器输入:http://localhost:8080/ 发现无法访问... ...