Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 491 Accepted Submission(s): 132
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 10
0 0 0
Sample Output
0
2
*+
/*
解题思路 之前我还不知道% 和mod 的区别 一直认为这二者是相等的
做了这个题之后 就意识到了
%:左边的操作符可正可负 因此 a%b的符号也是可正可负的
mod:a mod b 得到的值一定是一个正数所以需要转化一下 a mod b = (a %b + b) %b进行取余
题目中 a = b *q + r 就是这个意思
为了减少重复的次数 故我们用一个visit数组来表示 n mod k 的值
最后由于我们每一次都需要对得到的n 对k进行取余 而这一过程可能回%m进行取余 %k%m 这一顺序执行的结果可能不正确 所以我们直接对%k*m
*/
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
typedef struct LNode
{
int x;
int step ;
string res;
}node;
int visit[1000];
int n,k,m;
char way[5] = "+-*%";
void bfs()
{
queue<LNode> q;
node now;
now.x = n;
now.step =0;
now.res = "";
visit[(n%k + k)%k] = 1;//a mod k = (a %k + k)%k; visit数组的意义是保存在xmod k的值 是否已经访问过了
q.push(now);
while(!q.empty())
{
now = q.front();
q.pop();
if(((now.x%k) + k )%k==((n+1)%k + k )%k)
{
cout<<now.step<<endl;
cout<<now.res<<endl;
return;
}
node nxt;
nxt.step = now.step +1;
for(int i = 0;i<4;i++)
{
if(way[i]=='+')
{
nxt.x = (now.x + m)%(k*m);
nxt.res = now.res + '+';
}
else if(way[i]=='-')
{
nxt.x = (now.x - m)%(k*m);
nxt.res = now.res + '-';
}
else if(way[i]=='*')
{
nxt.x = (now.x * m)%(k*m);
nxt.res = now.res +'*';
}
else
{
nxt.x = (now.x%m + m)%m%(k*m);
nxt.res = now.res +'%';
}
if(!visit[(nxt.x %k + k)%k])
{
visit[(nxt.x %k + k)%k] = 1;
q.push(nxt);
}
}
}
cout<<"0"<<endl;
}
int main()
{
while(cin>>n>>k>>m)
{
if(!n&&!m&&!k)
break;
memset(visit,0,sizeof(visit));
bfs();
}
return 0;
}