Remainder(难度:1)
Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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
*+
思路:
题意:给出n,m,k,反复对n做+m,-m,*m,mod m的操作,使得 (初始的n+1) mod k = (现在的n) mod k。
注意:
(1)mod的结果均为正数,%的结果可正可负,取决于左操作数。
(2)因为存在%m的操作,所以不能直接% m % k,可能产生错误结果,要% ( k * m )
方法:
(1)广度优先搜索
(2)用%运算代替mod,即a mod b = ( a % b + b ) % b
(3)即 ( ( n + 1 ) % k + k ) % k == ( cur.num + m ) % ( k * m )
AC代码:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1010
#define INF 0x3f3f3f3f
int vis[maxn*maxn];
int n,k,m,ans;
struct node{
int num;//数值
string str;//运算符
};
//广搜
void bfs()
{
node cur,cnt;//目前节点,下一个扩展节点
int km=k*m;
ans=((n+1)%k+k)%k;//目标结果
memset(vis,0,sizeof(vis));
queue<node>q;
vis[(n%k+k)%k]=1;//初始化要遍历的点mod k,避免结果重复,并设置这点为已经访问
cur.num=n;//目前值为n
q.push(cur);//将目前状态放入队列
while(!q.empty())//当前队列仍有元素可扩展
{
cur=q.front();//得到队头状态
q.pop();//从队列中弹出队头状态
for(int i=1;i<=4;i++)//扩展四个运算符
{
if(i==1)//加法
{
cnt.num=(cur.num+m)%km;
cnt.str=cur.str+'+';
}
else if(i==2)//减法
{
cnt.num=(cur.num-m)%km;
cnt.str=cur.str+'-';
}
else if(i==3)//乘法
{
cnt.num=(cur.num*m)%km;
cnt.str=cur.str+'*';
}
else//取余
{
cnt.num=((cur.num%m+m)%m)%km;
cnt.str=cur.str+'%';
}
if((cnt.num%k+k)%k==ans)//得到目标结果
{
cout<<cnt.str.length()<<endl;//输出运算符数目
cout<<cnt.str<<endl;//输出运算符
return;
}
if(!vis[(cnt.num%k+k)%k])//若还未访问
{
vis[(cnt.num%k+k)%k]=1;//设为访问
q.push(cnt);//放入队列,继续for、while循环
}
}
}
cout<<0<<endl;//没有,输出0
}
int main()
{
while(cin>>n>>k>>m)
{
if(n==0&&k==0&&m==0) break;
bfs();
}
return 0;
}