hdu--1104--Remainder(简单的bfs)

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

*+

 1 /*
 2     Name: hdu--1104--Remainder
 3     Copyright: 版权2017 日天大帝
 4     Author: 日天大帝
 5     Date: 22/04/17 09:11
 6     Description: bfs
 7                 a = b * q + r (q > 0 and 0 <= r < q),
 8                 题上的取余运算和%运算不一样,%运算能产生负值所以要 (n%k+k)%k这样,才等于题意的取余
 9                 这个题用vis标记产生过的数据,同时用%km和%k进行剪枝优化
10
11 */
12 #include<cstring>
13 #include<iostream>
14 #include<queue>
15 #include<string>
16 using namespace std;
17 struct node{
18     int num,steps;
19     string str;
20 };
21 const int MAX = 1005;
22 bool vis[MAX];
23 int n,m,k,mk,final_cmp;
24 void bfs(){
25     node start;
26     start.num = n;
27     start.str = "";
28     start.steps = 0;
29     vis[(n%k+k)%k] = 1;
30     queue<node> q;
31     q.push(start);
32
33     while(!q.empty()){
34         node a,temp = q.front();q.pop();
35         if(final_cmp == ((temp.num%k)+k)%k){
36             cout<<temp.steps<<endl<<temp.str<<endl;
37             return ;
38         }
39         for(int i=0; i<4; ++i){
40             a = temp;
41             if(i == 0){
42                 a.num += m;
43                 a.str += ‘+‘;
44             }else if(i == 1){
45                 a.num -= m;
46                 a.str += ‘-‘ ;
47             }else if(i == 2){
48                 a.num *= m;
49                 a.str += ‘*‘;
50             }else{
51                 a.num = (a.num%m+m)%m;
52                 a.str += ‘%‘ ;
53             }
54             a.num %= mk;//%k之后%m结果就错啦,10%(15) !=10%3%5
55             if(vis[(a.num%k+k)%k])continue;//%k缩小范围剪枝
56             a.steps++;
57             vis[(a.num%k+k)%k] = 1;
58             q.push(a);
59         }
60     }
61     cout<<"0"<<endl;
62 }
63 int main(){
64     ios::sync_with_stdio(false);
65
66     while(cin>>n>>k>>m,n||m||k){//输入有正负不能用n+m+k
67         memset(vis,0,sizeof(vis));
68         mk = m*k;
69         final_cmp = ((n+1)%k+k)%k;
70         bfs() ;
71     }
72     return 0;
73 }
时间: 2024-10-14 15:32:22

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

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus

hdu 1104 Remainder

http://acm.hdu.edu.cn/showproblem.php?pid=1104 a%b=(a%b+b)%b; 题意:开始给了你n, k, m,每次由+m, -m, *m, modm得到新的N,继续对N这样的操作,直到(n+1) mod k== N mod k时结束,并且打印路径 1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <queue> 5

hdu - 1104 Remainder (bfs + 数论)

http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需要 a mod b = (a % b + b) % b 这样转换得到. 并且,由于新的N可以很大,所以我们每一步都要取%,而且最后要mod k,正常来说每步都%k就行了,但是由于其中的一个操作是N%m,所以我们每一步就不能%k了(%k%m混用会导致%出来的答案错误),而要%(k *m)(其实%(k,

HDU 1104 Remainder(BFS路径记录+数论)

Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4337    Accepted Submission(s): 1095 Problem Description Coco is a clever boy, who is good at mathematics. However, he is puzzled by a d

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

题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将负数变为正数的,所以取余的时候要注意,又因为各种问题…… % 的问题是:a mod b = (a % b + b) % b,不是平常的取余. 讨论里有个人是这样说的: 关于此题的用bfs搜索,大家都是知道的. 既然使用了bfs,则队列是少不了的,为了叙述的方便,记运算符集合 oper = {+,-,

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

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24937    Accepted Submission(s): 9535 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

hdu 1104 数论+bfs

题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k  输出最小路径与运算副 zsd:% 只是求余数 有正负 mod 是求模 无正负. yhd:对m*k求余对 对k求余不对 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5

Hdu 1548 A strange lift(BFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1548 一道简单的bfs,适合新手.你现在所在的电梯层为一个节点,而你从这个节点可以拜访另外两个节点(电梯向上走为一个节点,电梯向下走有一个节点),而拜访的时候自然也要避免拜访重复,否则会陷入死循环. #include <iostream> #include <queue> using namespace std; const int maxn = 200+10; int N,

HDU 2102 A计划 双层BFS

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出. 现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位

HDU 1253 胜利大逃亡(BFS)

#include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include <cstring> using namespace std; struct node{ int x,y,z,step; }; int ma[51][51][51]; int A,B,C,T; int mv[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,