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. 迅为4412开发板Linux设备树的镜像烧写和源码简单优化教程

    1 烧写:   烧写和4412默认镜像的烧写类似,使用fastboot. 先更新uboot,用4412默认uboot更新支持设备树的uboot 用支持设备树的uboot烧写. 进入支持设备树的uboo ...

  2. vue watch 监听

    1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...

  3. CAS (Compare and Swap)

    synchronized是悲观锁 注意:实现了CAS的有原子类(AtomicInteger,AtomicLong,等等原子类) CAS 是乐观锁,一种高效实现线程安全性的方法 1.支持原子更新操作,适 ...

  4. 四. python网络编程

    第八章.网络基础知识 1. TCP/IP协议介绍 1.TCP/IP概念 TCP/IP: Transmission Control Protocol/Internet Protocol的简写,中译名为传 ...

  5. Ukulele 常用和弦

  6. ios之自定义UINavigationBar

    ios5 自定义导航条问题 在ios5之前的系统中,可以通过定义导航条类别的方式自定义导航条: @implementation UINavigationBar (CustomImage)- (void ...

  7. Xcode及Mac快捷键

    1. 文件 CMD + N: 新文件CMD + SHIFT + N: 新项目CMD + O: 打开CMD + S: 保存CMD + SHIFT + S: 另存为CMD + W: 关闭窗口CMD + S ...

  8. Vuex基本概念

    Vuex基本概念 State Getter Mutation Action Module 简单的Store import Vue from 'vue'; import Vuex from 'vuex' ...

  9. 学习笔记(_huaji_)

    假如我没有见过太阳,我也许会忍受黑暗. 如果我知道自己会在哪里死去,我就永远都不去那儿.失败的经历,其实也有它的价值. 人的过失会带来错误,但要制造真正的灾难还得用计算机. 嘴角微微上扬已不复当年轻狂 ...

  10. 【DB_MySQL】 limit——取查询结果的子集

    语法:select * from student limit beginIndex,length; 这里结果集的下标同数组一样从0开始,beginIndex表示起始位置,length表示从beginI ...