Remainder
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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.
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 *+
该题是一个关于队列的题目,题意是 将n一系列加减乘除m 使得和k的余数等于 原来的n+1%k的余数 但是保证了余数是正的,且因为
n%k%m和n%m%k是不等的 那么就用k*m开代替,从而解决了这个问题,还有一个wa点,就是 宏定义,因为没有宏定义,超时了好几发啊~~
</pre><br /><pre name="code" class="cpp">#include <iostream> #include <math.h> #include <map> #pragma comment(linker, "/STACK:1024000000,1024000000") #include <stack> #include <queue> #include <vector> #include <algorithm> #include <stdio.h> #include <string.h> using namespace std; #define maxn 1000010 int n,k,m,km; int flag; int biaoji[maxn]; char zifu[maxn]; int cishu[maxn]; int lai[maxn]; void shu(int x) //输出原来的符号 { if(x!=lai[x]) { shu(lai[x]); printf("%c",zifu[x]); } } int main() { int u,v,t; while(~scanf("%d%d%d",&n,&k,&m),n|k|m) { memset(biaoji,0,sizeof(biaoji)); km=k*m; for(int i=0;i<=km;i++) lai[i]=i; int ans=((n+1)%k+k)%k; //求原来的 queue<int> q; n=(n%km+km)%km; q.push(n); biaoji[n]=1; //标记 保证不循环 cishu[n]=0; //次数 flag=0; while(!q.empty()) { u=q.front(); t=q.front(); q.pop(); if(u%k==ans) //如果%k余数为ans 表示ok 退出 { flag=1; break; } v=((u+m)%km+km)%km; //加法 if(biaoji[v]==0) { biaoji[v]=1; lai[v]=t; //记录结点 cishu[v]=cishu[t]+1; //次数加一 zifu[v]='+'; //符号存入 q.push(v); //入队列 } v=((u-m)%km+km)%km; //减法 if(biaoji[v]==0) { biaoji[v]=1; lai[v]=t; cishu[v]=cishu[t]+1; zifu[v]='-'; q.push(v); } v=((u*m)%km+km)%km; //乘法 if(biaoji[v]==0) { biaoji[v]=1; lai[v]=t; cishu[v]=cishu[t]+1; zifu[v]='*'; q.push(v); } v=((u%m)%km+km)%km; //除法 if(biaoji[v]==0) { biaoji[v]=1; lai[v]=t; cishu[v]=cishu[t]+1; zifu[v]='%'; q.push(v); } } if(flag==0) printf("0\n"); else { printf("%d\n",cishu[t]); //输出次数 shu(t); printf("\n"); } } return 0; }