HDU 1104 remainder

难点在于取模, km的问题,现在仍然不是很懂! 查了题解, 又发现了新的打印路径的方法。
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 *+
<span style="font-family: 'Courier New', Courier, monospace;">#include<cstdio></span>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int n,m,k,fina;
char ch[4]={'+','-','*','%'};
struct node
{
    int num,step;
    string road;
}ans;
int deal(int x,int key)
{
    if(key==0)
        return x+m;
    else if(key==1)
        return x-m;
    else if(key==2)
        return x*m;
    else
    {
        int tem=x%m;
        if(tem<0)
            tem=tem+m;
        return tem;
    }
}
void bfs()
{
    int tem;
    map <int,int> mp;
    mp[n]=1;
    queue< node >q;
    node in,out;
    in.num=n;
    in.step=0;
    in.road="";
    q.push(in);
    while(!q.empty())
    {
        out=q.front();q.pop();
        for(int i=0; i<4; i++)
        {
            tem=deal(out.num,i)%(k*m);
            if(mp[tem]==0)
            {
                in.num=tem;
                in.step=out.step+1;
                in.road=out.road+ch[i];
                mp[tem]=1;
                int x=tem%k;
                if(x<0)
                    x=x+k;
                if(x==fina)
                {
                    ans=in;
                    return;
                }
                else
                q.push(in);
            }
        }
    }
}
int main()
{

    while(1)
    {
        scanf("%d%d%d",&n,&k,&m);
        if(n==m&&m==k&&k==0)return 0;
            fina=(n+1)%k;
            if(fina<0)
                fina+=k;
            ans.step=0;
            bfs();
            printf("%d\n",ans.step);
            if(ans.step)
                cout<<ans.road<<endl;
    }
    return 0;
}
/* 下面是两个没能AC的代码, 已经向着AC代码做了改进, 然而还没AC, 等有时间再来找BUG吧*/
#include<iostream>
#include<cstdio>
using namespace std;
#include<queue>
#include<cstring>
int n, m, k;
int fina;
int flag = 1;
bool vis[2002];
const char d[4] = {'+', '*', '-', '%'};
struct node
{
    int num, step;
    string road;
};
queue<node>q;
node now;
node New;

int deal(int x, int i)
{
    if(i == 0)
        return x+m;
    else if(i == 1)
        return x*m;
    else if(i == 2)
        return x-m;
    else
    {
        int temp = x%m;
        if(temp < 0)
            temp += m;
        return temp;
    }
}
void bfs()
{
    memset(vis, false, sizeof(vis));
    now.step = 0;
    now.num = n;
    now.road = "";
    vis[now.num] = true;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int temp = deal(now.num, i) % k;
            if(temp < 0)
            temp += k;
            if(vis[temp])
                continue;
            New.num = temp;
            New.step = now.step + 1;
            New.road = now.road + d[i];
            vis[temp] = true;
            q.push(New);
            if(New.num == fina)
            {
                flag = 0;
                printf("%d\n", New.step);
                printf("%s\n", New.road);
                return;
            }

        }

    }
    if(flag)
        printf("0\n");
}
int main()
{
    while(scanf("%d%d%d", &n, &m, &k))
    {
        flag = 1;
        if(n == 0 && m == 0 && k == 0)
            return 0;
        while(!q.empty())
            q.pop();
        fina = (n+1)%k;
        if(fina < 0)
            fina += k;

        bfs();
    }
    return 0;
}
*/
#include<iostream>
#include<cstdio>
using namespace std;
#include<queue>
#include<cstring>
int n, m, k;
int fina;
bool vis[2002];
const char d[4] = {'+', '*', '-', '%'};
struct node
{
    int num, step;
    string road;
}ans;
queue<node>q;
node now;
node New;

int deal(int x, int i)
{
    if(i == 0)
        return x+m;
    else if(i == 1)
        return x*m;
    else if(i == 2)
        return x-m;
    else
    {
        int temp = x%m;
        if(temp < 0)
            temp += m;
        return temp;
    }
}
void bfs()
{
    memset(vis, false, sizeof(vis));
    now.step = 0;
    now.num = n;
    now.road = "";
    vis[now.num] = true;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int temp = deal(now.num, i) % k;
            if(temp < 0)
            temp += k;
            if(vis[temp])
                continue;
            New.num = temp;
            New.step = now.step + 1;
            New.road = now.road + d[i];
            vis[temp] = true;
            q.push(New);
            if(New.num == fina)
            {
                ans = New;
                return;
            }

        }

    }

}
int main()
{
    while(scanf("%d%d%d", &n, &m, &k))
    {
        if(n == 0 && m == 0 && k == 0)
            return 0;
        while(!q.empty())
            q.pop();
        fina = (n+1)%k;
        if(fina < 0)
            fina += k;
        ans.step = 0;
        bfs();
        cout << ans.step << endl;
        if(ans.step)
            cout << ans.road << endl;
    }
    return 0;
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值