Remainder

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2260 Accepted Submission(s): 481
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
*+

BFS(广度优先搜索)

 
import java.io.*;
import java.util.*; public class Main {
public String str="+-*%";
public int n,m,k,sum,km;
public boolean boo[]=new boolean[1000*1000*10+1];
public Queue<Node1> list=new LinkedList<Node1>();
public static void main(String[] args) { new Main().work();
}
public void work(){
Scanner sc=new Scanner(new BufferedInputStream(System.in));
while(sc.hasNext()){
list.clear();
Arrays.fill(boo,false);
n=sc.nextInt();
k=sc.nextInt();
m=sc.nextInt();
if(n==0&&k==0&&m==0)
System.exit(0);
Node1 node=new Node1();
node.n=n;
node.s="";
sum=getMode(n+1,k);
km=m*k;
boo[getMode(n,km)]=true;
list.add(node);
BFS();
}
}
public void BFS(){
while(!list.isEmpty()){
Node1 node=list.poll();
if(getMode(node.n,k)==sum){
System.out.println(node.s.length());
System.out.println(node.s);
return;
}
for(int i=0;i<str.length();i++){
int temp=0;
if(str.charAt(i)=='+'){
temp=getMode(node.n+m,km);
}
else if(str.charAt(i)=='-'){
temp=getMode(node.n-m,km);
}
else if(str.charAt(i)=='*'){
temp=getMode(node.n*m,km);
}
else if(str.charAt(i)=='%'){
temp=getMode(getMode(node.n,m),km);
}
if(!boo[temp]){
boo[temp]=true;
Node1 t=node.getNode();
t.n=temp;
t.s=t.s+str.charAt(i);
list.add(t);
}
}
}
System.out.println(0);
}
public int getMode(int a,int b){
return (a%b+b)%b;
}
}
class Node1{
int n;
String s;
Node1(){
n=0;
s="";
}
public Node1 getNode(){
Node1 node=new Node1();
node.n=0;
node.s=s;
return node;
}
}

 

最新文章

  1. 读取xml数据装配到字典中之应用场景
  2. javascript的几种继承
  3. 二十一、Java基础--------IO流之综合案例分析
  4. IOS 二维码的实现
  5. [转]PLSQL Developer备份恢复oracle数据
  6. Unicode与UTF8相互转化(使用MultiByteToWideChar)
  7. 51nod1239 欧拉函数之和
  8. 中国海洋大学第四届朗讯杯高级组 Cash Cow(模拟)
  9. Know Thy Complexities!
  10. VS上的WebService入门贴
  11. lsdslam代码笔记
  12. 移动端tab滑动和上下拉刷新加载
  13. 光盘安装win7系统教程
  14. Oracle使用触发器和mysql中使用触发器的比较
  15. linux之awk命令获取最后一列
  16. 案例一(haproxy+keepalived高可用负载均衡系统)【转】
  17. 【BZOJ4826】【HNOI2017】影魔(扫描线,单调栈)
  18. 抗性基因数据库CARD介绍
  19. 字符串、字节数组、流之间的相互转换以及文件MD5的计算
  20. 1.SpringBoot之Helloword 快速搭建一个web项目

热门文章

  1. 在 Bootstraptable 插件基础上新增可编辑行
  2. 转:GitHub 万星推荐成长技术清单
  3. Maven实用总结
  4. 在sublime Text 3上编写并运行java程序
  5. Codeforces 500 E. New Year Domino
  6. BZOJ 2286 [Sdoi2011]消耗战(虚树+树形DP)
  7. 【最小割】BZOJ3438-小M的作物(Rank 2???!!!)(含新款Dinic模板)
  8. 【期望DP+高斯消元】BZOJ3270-博物馆
  9. (疯狂java)第三课
  10. Redis 真得那么好用吗?