hdu 1104 (数论+bfs)

Remainder

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2039    Accepted Submission(s): 436


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 *+
 
题意:(注意题目中的%是指mod)开始给了你n, k, m。。。。每次由+m, -m, *m, modm得到新的N,继续对N这样的操作,直到(n+1) mod k== N mod k时结束

(以下都是贴的别人的  其实我自己还没搞懂)
ps:%与mod的区别:%出来的数有正有负,符号取决于左操作数。。。而mod只能是正(因为a = b * q + r (q > 0 and 0 <= r < q), then we have a mod q = r    中r要大于等于0小于q)

所以要用%来计算mod的话就要用这样的公式:a mod b = (a % b + b) % b

括号里的目的是把左操作数转成正数

 由于新的N可以很大,所以我们每一步都要取%,而且最后要mod k,正常来说每步都%k就行了,但是由于其中的一个操作是N%m,所以我们每一步就不能%k了(%k%m混用会导致%出来的答案错误),而要%(k *m)(其实%(k,m的公倍数都行))
 
感想:这题难就难在数论  因为我完全不懂什么%%。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1000005
using namespace std;

int n,m,k,km,ans,ansstep,endval;
char dir[]="+-*%";
bool vis[maxn];
struct Node
{
    int val,step,fa;
    char c;
} cur,now,q[maxn];

bool bfs()
{
    int i,j,head=0,tail=-1,tt;
    int nstep,nval,tval;
    memset(vis,0,sizeof(vis));
    cur.val=n;
    cur.step=0;
    vis[(n%k+k)%k]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        nstep=now.step;
        nval=now.val;
        if((nval%k+k)%k==endval)
        {
            ans=head;
            ansstep=nstep;
            return true ;
        }
        for(i=0; i<4; i++)
        {
            switch(i)
            {
            case 0:
                tval=((nval+m)%km+km)%km;
                tt=(((nval+m)%k)+k)%k;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    cur.val=tval;
                    cur.fa=head;
                    cur.step=nstep+1;
                    cur.c=dir[i];
                    q[++tail]=cur;
                }
                break;
            case 1:
                tval=((nval-m)%km+km)%km;
                tt=(((nval-m)%k)+k)%k;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    cur.val=tval;
                    cur.fa=head;
                    cur.step=nstep+1;
                    cur.c=dir[i];
                    q[++tail]=cur;
                }
                break;
            case 2:
                tval=((nval*m)%km+km)%km;
                tt=(((nval*m)%k)+k)%k;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    cur.val=tval;
                    cur.fa=head;
                    cur.step=nstep+1;
                    cur.c=dir[i];
                    q[++tail]=cur;
                }
                break;
            case 3:
                tval=((nval%m+m)%m%km+km)%km;
                tt=(((nval%m)%k)+k)%k;
                if(!vis[tt])
                {
                    vis[tt]=1;
                    cur.val=tval;
                    cur.fa=head;
                    cur.step=nstep+1;
                    cur.c=dir[i];
                    q[++tail]=cur;
                }
                break;
            }
        }
        head++;
    }
    return false ;
}
void output(int t)
{
    if(t==0) return ;
    else
    {
        output(q[t].fa);
        printf("%c",q[t].c);
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&k,&m),n||k||m)
    {
        km=k*m;
        endval=((n+1)%k+k)%k;
        if(bfs())
        {
            printf("%d\n",ansstep);
            output(ans);
        }
        else printf("0");
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值