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. 开源Inno Setup官网下载、安装、打包教程(官网安装向导中文语言包)

    安装Inno Setup篇 1.搜索Inno Setup 2.下载Inno Setup 3.选择下载最新 innosetup-5.5.9-unicode.exe 版本(innosetup-5.5.9. ...

  2. js循环处理后台返回的json数组

    <script type="text/javascript"> function gongdan_search(elm){ var dangqian_value=$(e ...

  3. Spring Boot开启https

    原文:https://github.com/x113773/testall/issues/1 1. 第一步就是用JDK的keytool工具来创建一个密钥存储(keystore)`keytool -ke ...

  4. eclipse 导入git库 Android工程

    1. 导入git库 1.1 从git库 clone 代码 在file->import中选中Git 目录下的Projects from Git 点击Next 选择 URL 点击Next 输入URL ...

  5. Java基础(3) -字符串

    字符串-String 1.定义&&初始化 使用双引号把字符括起来 String str = "test"; 2.字符串的提取-substring String a ...

  6. ionic ios项目真机运行-不用开发者账号

    ionic ios项目真机运行-不用开发者账号 1. 添加ios平台 ionic platform add ios 2.使用XCODE打开项目 3.使用APPID登录XCODE 打开XCODE账号登录 ...

  7. VB6之Mandelbrot集

    Mandelbrot真是上帝之作,数学之美最直观的表现. 围观wiki和百科(百度百科)上关于Mandelbrot的解释至今仍是不能理解,没办法我高数实在学得不好. 搜素到园友用F#写的一篇实现代码, ...

  8. Watson API - Personality Insight For Certificate

    Personality Insight For Certificate 1.Describe the intended use of the Personality Insights service ...

  9. 如何在docker配置asp.net core https协议

    本文参考自<Step by step: Expose ASP.NET Core over HTTPS with Docker> 自从微软发布.net core以来,就在许多社区掀起了讨论, ...

  10. java 中的重载与重写 抽象类与接口的区别

    . 重载与重写的区别: 重载(overload)               | 重写(override) 1 方法的名称相同,参数个数.类型不同 | 方法名称.参数列表.返回值类型与父类完全相同 2 ...