hdu1104
Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2251 Accepted Submission(s):
477
You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.
The input is terminated with three 0s. This test case is not to be processed.
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
int n,m,k,visit[1000010],t;
typedef struct
{
int num;
int step;
string s;
} point;
void BFS()
{
int i;
point now,next;
now.num=n;
now.step=0;
now.s="";
memset(visit,0,sizeof(visit));
visit[(n%k+k)%k]=1;
queue<point>q;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(((n+1)%k+k)%k==(now.num%k+k)%k)
{
printf("%d\n",now.step);
cout<<now.s<<endl;
return ;
}
for(i=0; i<4; i++)
{
if(i==0)
{
next.num=(now.num+m)%t;
next.step=now.step+1;
next.s=now.s+'+';
}
else if(i==1)
{
next.num=(now.num-m)%t;
next.step=now.step+1;
next.s=now.s+'-';
}
else if(i==2)
{
next.num=(now.num*m)%t;
next.step=now.step+1;
next.s=now.s+'*';
}
else if(i==3)
{
next.num=(now.num%m+m)%m%t;
next.step=now.step+1;
next.s=now.s+'%';
}
if (!visit[(next.num % k + k) % k])
{
visit[(next.num % k + k) % k]=1;
q.push(next);
}
}
}
printf("0\n");
}
int main()
{
while(~scanf("%d%d%d",&n,&k,&m)&&(n||m||k))
{
t=k*m;
BFS();
}
}