杭电1104(BFS+特别注意%与mod运算)

本文介绍了一个数学问题的解决方法,该问题是找到一系列操作使给定的整数N通过加、减、乘、取模操作达到特定条件。使用广度优先搜索算法来寻找最短的操作路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Remainder(难度:1)

Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

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
*+

思路:

题意:给出n,m,k,反复对n做+m,-m,*m,mod m的操作,使得 (初始的n+1) mod k = (现在的n) mod k。

注意:
(1)mod的结果均为正数,%的结果可正可负,取决于左操作数。
(2)因为存在%m的操作,所以不能直接% m % k,可能产生错误结果,要% ( k * m )

方法:
(1)广度优先搜索
(2)用%运算代替mod,即a mod b = ( a % b + b ) % b
(3)即 ( ( n + 1 ) % k + k ) % k == ( cur.num + m ) % ( k * m )

AC代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

#define maxn 1010
#define INF 0x3f3f3f3f
int vis[maxn*maxn];
int n,k,m,ans;

struct node{
    int num;//数值 
    string str;//运算符 
};

//广搜 
void bfs()
{
    node cur,cnt;//目前节点,下一个扩展节点 
    int km=k*m;
    ans=((n+1)%k+k)%k;//目标结果 
    memset(vis,0,sizeof(vis));
    queue<node>q;
    vis[(n%k+k)%k]=1;//初始化要遍历的点mod k,避免结果重复,并设置这点为已经访问 
    cur.num=n;//目前值为n 
    q.push(cur);//将目前状态放入队列 
    while(!q.empty())//当前队列仍有元素可扩展 
    {
        cur=q.front();//得到队头状态 
        q.pop();//从队列中弹出队头状态 
        for(int i=1;i<=4;i++)//扩展四个运算符 
        {
            if(i==1)//加法 
            {
                cnt.num=(cur.num+m)%km;
                cnt.str=cur.str+'+';
            }
            else if(i==2)//减法 
            {
                cnt.num=(cur.num-m)%km;
                cnt.str=cur.str+'-';
            }
            else if(i==3)//乘法 
            {
                cnt.num=(cur.num*m)%km;
                cnt.str=cur.str+'*';
            }
            else//取余 
            {
                cnt.num=((cur.num%m+m)%m)%km;
                cnt.str=cur.str+'%';
            }
            if((cnt.num%k+k)%k==ans)//得到目标结果 
            {
                cout<<cnt.str.length()<<endl;//输出运算符数目 
                cout<<cnt.str<<endl;//输出运算符 
                return;
            }
            if(!vis[(cnt.num%k+k)%k])//若还未访问 
            {
                vis[(cnt.num%k+k)%k]=1;//设为访问 
                q.push(cnt);//放入队列,继续for、while循环 
            }
        }
    }
    cout<<0<<endl;//没有,输出0 
}

int main()
{
    while(cin>>n>>k>>m)
    {
        if(n==0&&k==0&&m==0) break;
        bfs();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值