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. 探索Windows命令行系列(2):命令行工具入门

    1.理论基础 1.1.命令行的前世今生 1.2.命令执行规则 1.3.使用命令历史 2.使用入门 2.1.启动和关闭命令行 2.2.执行简单的命令 2.3.命令行执行程序使用技巧 3.总结 1.理论基 ...

  2. php中比较两个变量是否相等的最高效写法

    <?php //php中比较两个变量是否相等的最高效写法 function isEqual($a,$b){ return $a==$b; } var_dump(isEqual(3,3));//t ...

  3. php二进制流文件

    <?php $img_file = 'test.png'; // $fp = fopen($img_file, 'rb'); // $content = fread($fp, filesize( ...

  4. smarty模板自定义变量

    一.通过smarty方式调用变量调节器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  5. Java程序设计---io流读取文件内容并将其逆值输出到控制台

    import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileR ...

  6. 在ASP.NET Core配置环境变量和启动设置

    在这一部分内容中,我们来讨论ASP.NET Core中的一个新功能:环境变量和启动设置,它将开发过程中的调试和测试变的更加简单.我们只需要简单的修改配置文件,就可以实现开发.预演.生产环境的切换. A ...

  7. Java添加JDBC

    添加JDBC 1.SQL Server SQL Server2005 下载 sqljdbc_4.0 https://www.microsoft.com/en-us/download/details.a ...

  8. Spring 加载静态资源

    <mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...

  9. JAVAEE——SSH项目实战02:客户列表和BaseDao封装

    作者: kent鹏 转载请注明出处: http://www.cnblogs.com/xieyupeng/p/7129152.html 该项目在SSH三大框架整合基础上进行开发:http://www.c ...

  10. 前端需要注意的seo

    1 合理的title ,description ,keyswords 搜索引擎对这三项的权重逐渐减小,title 强调重点即可 ,重要的关键字不要超过两次,而且要靠前. 2 不同的tilte要有所不同 ...