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

时间:2023-03-08 19:06:19
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
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() ;
     }
     ;
 }