Remainder

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

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

/*
其他没什么好说的,数字太大需要%(k*m),这个可以证明就等于对n进行+、-m操作
不影响结果,属于正常操作
*/
#include<iostream>
#include<queue>
#include<string>
using namespace std;
bool vis[]; struct point
{
int val;
int step;
string s;
}p,t; void bfs(int n,int k,int m)
{
memset(vis,false,sizeof(vis));
queue<point> q;
int s=((n+)%k+k)%k;
t.val=n;
t.step=;
t.s="";
vis[(n%k+k)%k]=true;
q.push(t);
while(!q.empty())
{
t=q.front();
q.pop();
if(s==(t.val%k+k)%k)
{
cout<<t.step<<endl;
cout<<t.s<<endl;
return ;
}
for(int i=;i<;i++)
{
p=t;
p.step++;
if(i==)
{
p.val=(t.val+m)%(k*m);
p.s+='+';
}
else if(i==)
{
p.val=(t.val-m)%(k*m);
p.s+='-';
}
else if(i==)
{
p.val=(t.val*m)%(k*m);
p.s+='*';
}
else if(i==)
{
p.val=(t.val%m+m)%m%(k*m);
p.s+='%';
}
if(!vis[(p.val%k+k)%k])
{
q.push(p);
vis[(p.val%k+k)%k]=true;
}
}
}
cout<<<<endl;
}
int main()
{
int n,m,k;
while(cin>>n>>k>>m,k || m || n)
bfs(n,k,m);
return ;
}

hdu 1104 数论+bfs的更多相关文章

  1. hdu - 1104 Remainder (bfs + 数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...

  2. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  3. HDU 1104 Remainder (BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  5. HDU 1104 Remainder (BFS)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1104 题意:给你一个n.m.k,有四种操作n+m,n-m,n*m,n%m,问你最少经过多少步,使得最后 ...

  6. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

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

  7. HDU 1104 Remainder( BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. HDU 1104 Remainder (BFS求最小步数 打印路径)

    题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...

  9. HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...

随机推荐

  1. 如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)

    适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 到 11.2.0.2 [发行版 11.2]本文档所含信息适用于所有平台 用途 这篇文档提 ...

  2. CAD交互绘制圆形批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) { //自定义实体的GUID标识符 var sGui ...

  3. Java中集合类

    一.Collection Collection 接口用于表示任何对象或元素组.想要尽可能以常规方式处理一组元素时,就使用这一接口.Collection 在前面的大图也可以看出,它是List 和 Set ...

  4. 电商技术中企业数据总线ESB和注册服务管理的区别

    一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...

  5. git 不完全教程

    概念 工作目录:当前所见,Working directory 暂存区域:以后要提交到仓库的文件,称为Index或者staging area Git 仓库:持久化存储快照的地方,HEAD指针所指向的地方 ...

  6. 洛谷 P1483 序列变换

    https://www.luogu.org/problemnew/show/P1483 数据范围不是太大. 一个数组记录给k,记录每个数加了多少. 对于查询每个数的大小,那么就枚举每个数的因子,加上这 ...

  7. element使用心得

    Table Table 常用属性解释 数据过滤,filter过滤器 <el-table-column width="200" show-overflow-tooltip la ...

  8. perl5中锚位修饰符\A \z \Z 和perl4中^(开头)和$(结尾)的区别

    习惯使用perl4的开发者总是用^表示字符串开头锚位,用$表示字符串结尾锚位,比如\^https://\ 将会匹配所有以https://开头的字符串,同样,\.bmp$\将会匹配所有以.bmp结尾的字 ...

  9. perl:_DATA_ _LINE_ _FILE_

    这三个应该是句柄: _DATA_ _FILE_ _LINE_ 没有找到具体介绍...记录于此,已被后续补充.

  10. gpio/外设/控制器

    1.项目中所有的外设pad都是通过GPIO与控制器相连的.比如FSHC<=>gpio<=>flash 2.gpio类似多个 mux 集合. 3.对于与gpio相连的pad具体结 ...