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. beta版本发布-团队

    一.β版本于α版本的不同 1.α版本先前只可电脑单机使用,β版本已成功解决联网问题,可以在不同电脑上正常使用 2.β版本相较于α版本修补了较多漏洞.进行了界面的优化且新增了学生个人信息维护功能.教师的 ...

  2. Jordan 标准型定理

    将学习到什么 就算两个矩阵有相同的特征多项式,它们也有可能不相似,那么如何判断两个矩阵是相似的?答案是它们有一样的 Jordan 标准型.   Jordan 标准型定理 这节目的:证明每个复矩阵都与一 ...

  3. No-12.函数进阶

    函数进阶 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数根据 有没有参数 以及 有没有返回值,可以 相互组合,一共有 4 种 组合形 ...

  4. Java常见对象Object类中的个别方法

    Java常见对象Object类 public int hashCode() : 返回该对象的哈希码值. 注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值.你可以理解成 ...

  5. EditorConfig文件

    EditorConfig .editorconfig文件 在很多开源项目中,会出现这个文件,这个文件有何作用? editorconfig 帮助开发者的(编辑器和IDEs)定义和维护编程风格. 有些编辑 ...

  6. 条款37:绝不重新定义继承而来的缺省参数值(Never redefine a function's inherited default parameter value)

    NOTE: 1.绝不重新定义一个继承而来的缺省参数值,因为缺省参数值都是静态绑定的,而virtual 函数-----你唯一应该覆盖的东西----却是动态绑定的.

  7. MySQL之单表查询、多表查询

    一.单表查询: 单个表的查询方法及语法顺序需要通过实际例子来熟悉 先将表数据创建下: mysql> create database singe_t1; # 建个数据库singe_t1 Query ...

  8. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

  9. Python 描述符(Descriptor) 附实例

    在 Python 众多原生特性中,描述符可能是最少被自定义的特性之一,但它在底层实现的方法和属性却无时不刻被使用着,它优雅的实现方式体现出 Python 简洁之美. 定义 一个描述符是一个有" ...

  10. 关于Powershell执行时的问题

    问题1: [问题描述] 使用Invoke-Command命令登录远程主机执行命令时,提示如下错误: [192.168.1.135] 连接到远程服务器失败,错误消息如下: WinRM 客户端无法处理该请 ...