Remainder

 

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

*+

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
using namespace std;
int vis[2000000];
int n,k,m;
struct node{
	string a;
	int shu,to;
};
void dfs(){
	memset(vis,0,sizeof(vis));
	int km=k*m;//看了别人的博客知道的,不知道他们怎么想到的但是很容易证明 ,记住先 
	queue<node>q;
	node now;
	vis[(n%k+k)%k]=1;
	for(int i=0;i<4;i++){
		now.a="";
		if(i==0){
			now.shu=(n+m)%km;
			now.a+='+';
			now.to=1;
		}
		if(i==1){
			now.shu=(n-m)%km;
			now.a+='-';
			now.to=1;
		}
		if(i==2){
			now.shu=(n*m)%km;
			now.a+='*';
			now.to=1;
		}
		if(i==3){
			now.shu=((n%m+m)%m)%km;
			now.a+='%';
			now.to=1;
		}
		if(vis[(now.shu%k+k)%k]==0) 
			q.push(now);
		vis[(now.shu%k+k)%k]=1;
	}
	while(!q.empty()){
		now=q.front();
		q.pop();
		if(((n+1)%k+k)%k==(now.shu%k+k)%k){
			printf("%d\n",now.to);
			cout<<now.a<<endl;
			return ;
		}
		node jiu;
		for(int i=0;i<4;i++){
			jiu.a=now.a;
			if(i==0){
				jiu.shu=(now.shu+m)%km;
				jiu.a+='+';
				jiu.to=now.to+1;
			}
		    if(i==1){
				jiu.shu=(now.shu-m)%km;
				jiu.a+='-';
				jiu.to=now.to+1;
			}
		    if(i==2){
				jiu.shu=(now.shu*m)%km;
				jiu.a+='*';
				jiu.to=now.to+ 1;
			}
		    if(i==3){
				jiu.shu=((now.shu%m+m)%m)%km;
				jiu.a+='%';
				jiu.to=now.to+ 1;
			}
		if(vis[(jiu.shu%k+k)%k]==0)
			q.push(jiu);
		vis[(jiu.shu%k+k)%k]=1;
		}
	}
	printf("0\n");
}
int main(){  
	while(scanf("%d%d%d",&n,&k,&m)!=EOF && n && k && m){
		dfs();
	}
}

猜你喜欢

转载自blog.csdn.net/doublekillyeye/article/details/82429116