hdu 1104 Remainder(BFS+数论)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1104

Remainder

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


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的区别:%出来的数有正有负,mod只能是正,即(a+mod)%mod,本题相关运算是指后者。关键词:最少的步骤,输出运算过程中的符号,相同运算次数的情况下符号序列有优先级之分。四项运算的过程中为了不产生溢出数所以进行了%km操作,还要注意每一步的%m,+m,%k,+k等操作。(WA了半天||-_-)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int md=1005;
int n,k,m,km;
bool vis[1000010];
struct node{
    int val,step,top;
    char path[1000];
    void init(){
        val=step=top=0;
        memset(path,0,sizeof(path));
    }
};
node que[md];
void bfs(){
    int q1=0,q2=0;
    node t;
    t.init();
    t.val=n;
    que[q2++]=t;
    vis[(t.val%k+k)%k]=1;
    while(q1!=q2){
        node cur=que[q1],temp;
        q1=(q1+1)%md;
        if(((n+1)%k+k)%k==(cur.val%k+k)%k){
            printf("%d\n%s\n",cur.step,cur.path);
            return ;
        }
        for(int i=0;i<4;i++){
            temp.init();
            strcpy(temp.path,cur.path);
            temp.top=cur.top;
            if(i==0){
                temp.val=(cur.val+m)%km;
                temp.path[temp.top++]='+';
            }
            else if(i==1){
                temp.val=(cur.val-m+km)%km;
                temp.path[temp.top++]='-';
            }
            else if(i==2){
                temp.val=cur.val*m%km;
                temp.path[temp.top++]='*';
            }
            else {
                temp.val=(cur.val%m+m)%m%km;
                temp.path[temp.top++]='%';
            }
            temp.step=cur.step+1;
            if(!vis[(temp.val%k+k)%k]){
                que[q2]=temp;
                q2=(q2+1)%md;
                vis[(temp.val%k+k)%k]=1;
            }
        }
    }
    printf("0\n");  //队列空了结束循环就是没有找到
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(cin>>n>>k>>m){
        if(n==0&&m==0&&k==0) break;
        km=k*m;
        memset(vis,0,sizeof(vis));
        bfs();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值