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
*+
Author
Wang Yijie
第一個WA點是每一步的mod操作,mod!=%。所以需要想個辦法讓他們變為正。
第二個是每一步的取與操作,應當是對KM進行而不是K,弄K會出事情的= =。
第三個也是剛剛才發現的就是 printf(“%s”,str);語句會亂碼,用C語言的方式進行拷貝成char處理就可以解決。
一直坑到我的則是標記上的處理= = 現已改正,說出來怕誤導大家。
大家加油!
AC代碼:
#include<iostream>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
//不要小瞧人生啊~
#define INF 0x3f3f3f3f
int n, k, m;
int vis[1001000];
char c[2000];
int mod(int a, int b)//注意mod與%的區別
{
int temp = (a%b + b) % b;
return temp;
}
struct node
{
int num, step;
string oper;
};
void bfs()
{
int final = mod(n + 1, k);
int km = k*m;
queue<node> q;
while (!q.empty())
q.pop();
memset(vis, 0, sizeof(vis));
vis[mod(n, k)] = 1;
node x, tmp;
x.num = n;
x.step = 0;
x.oper = "";//√
q.push(x);
while (!q.empty())
{
x = q.front();
q.pop();
if (mod(x.num, k) == final)
{
printf("%d\n", x.step);
//printf("%s\n", x.oper);//此語句會亂碼,吃鯨!
//cout << x.oper << endl;//√
strcpy(c, x.oper.c_str());//√
printf("%s\n", c);
return;
}
tmp = x;// +
tmp.step++;
tmp.num = mod(x.num + m, km);
tmp.oper += "+";
if (!vis[tmp.num])
{
q.push(tmp);
vis[tmp.num] = 1;//對已經計算過的數字進行標記
}
tmp = x;// -
tmp.step++;
tmp.num = mod(x.num - m, km);
tmp.oper += "-";
if (!vis[tmp.num])
{
q.push(tmp);
vis[tmp.num] = 1;//對已經計算過的數字進行標記
}
tmp = x;// *
tmp.step++;
tmp.num = mod(x.num * m, km);
tmp.oper += "*";
if (!vis[tmp.num])
{
q.push(tmp);
vis[tmp.num] = 1;//對已經計算過的數字進行標記
}
tmp = x;// %
tmp.step++;
tmp.num = mod(mod(x.num, m), km);
tmp.oper += "%";
if (!vis[tmp.num])
{
q.push(tmp);
vis[tmp.num] = 1;//對已經計算過的數字進行標記
}
}
printf("0\n");
}
int main()
{
while (scanf("%d%d%d", &n, &k, &m) != EOF)
{
if (n == 0 && m == 0 && k == 0)
break;
bfs();
}
return 0;
}