Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2908 Accepted Submission(s): 641
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,n可为正可为负,一般要看标记数组,但数组不能标记
负数,所以就用map容器标记,但有一个问题无法解决,就是如果不符合条件,就会无限
广搜下去,这样会RE而且也无法判断答案不存在的情况,知道看了一篇文章(链接:
http://blog.163.com/sentimental_man/blog/static/730016182008112912121283/)
最终用数论解决,读者可看一下。
代码示例:
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<queue>
#define N 1000100
using namespace std;
typedef struct
{
int value;
int step;
string dist;
}node;
int visit[N],n,m,k,km;
void bfs()
{
memset(visit,0,sizeof(visit));
node fir,nex;
queue<node>Q;
fir.value=n;
fir.step=0;
fir.dist="";
visit[(n%k+k)%k]=1;
Q.push(fir);
while(!Q.empty())
{
fir=Q.front();
Q.pop();
if((fir.value%k+k)%k==((n+1)%k+k)%k)
{
cout<<fir.step<<endl<<fir.dist<<endl;
return;
}
for(int i=0;i<4;i++)
{
if(i==0)
{
nex.value=(fir.value+m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'+';
}
if(i==1)
{
nex.value=(fir.value-m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'-';
}
if(i==2)
{
nex.value=(fir.value*m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'*';
}
if(i==3)
{
nex.value=(fir.value%m+m)%m%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'%';
}
if(visit[(nex.value%k+k)%k])
continue;
visit[(nex.value%k+k)%k]=1;
Q.push(nex);
}
}
cout<<0<<endl;
}
int main()
{
while(cin>>n>>k>>m&&(k||m||n))
{
km=k*m;
bfs();
}
return 0;
}
#include<string.h>
#include<string>
#include<iostream>
#include<queue>
#define N 1000100
using namespace std;
typedef struct
{
int value;
int step;
string dist;
}node;
int visit[N],n,m,k,km;
void bfs()
{
memset(visit,0,sizeof(visit));
node fir,nex;
queue<node>Q;
fir.value=n;
fir.step=0;
fir.dist="";
visit[(n%k+k)%k]=1;
Q.push(fir);
while(!Q.empty())
{
fir=Q.front();
Q.pop();
if((fir.value%k+k)%k==((n+1)%k+k)%k)
{
cout<<fir.step<<endl<<fir.dist<<endl;
return;
}
for(int i=0;i<4;i++)
{
if(i==0)
{
nex.value=(fir.value+m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'+';
}
if(i==1)
{
nex.value=(fir.value-m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'-';
}
if(i==2)
{
nex.value=(fir.value*m)%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'*';
}
if(i==3)
{
nex.value=(fir.value%m+m)%m%km;
nex.step=fir.step+1;
nex.dist=fir.dist+'%';
}
if(visit[(nex.value%k+k)%k])
continue;
visit[(nex.value%k+k)%k]=1;
Q.push(nex);
}
}
cout<<0<<endl;
}
int main()
{
while(cin>>n>>k>>m&&(k||m||n))
{
km=k*m;
bfs();
}
return 0;
}