HDU 1104 Remainder( BFS(廣度優先搜尋))

來源:互聯網
上載者:User
Remainder

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

Total Submission(s): 2255 Accepted Submission(s): 479Problem DescriptionCoco 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.

InputThere 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.

OutputFor 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 100 0 0

Sample Output

02*+

題意:(注意題目中的%是指mod)開始給了你n, k, m。。。。每次由+m, -m, *m, modm得到新的N,繼續對N這樣的操作,直到(n+1) mod k== N mod k時結束。。。並且列印路徑

%與mod的區別:%出來的數有正有負,符號取決於左運算元。。。而mod只能是正(因為a = b * q + r (q > 0 and 0 <= r < q), then we have a mod q = r 中r要大於等於0小於q)。。。。。

所以要用%來計算mod的話就要用這樣的公式:a mod b = (a % b + b) % b

括弧裡的目的是把左運算元轉成正數

由於新的N可以很大,所以我們每一步都要取%,而且最後要mod k,正常來說每步都%k就行了,但是由於其中的一個操作是N%m,所以我們每一步就不能%k了(%k%m混用會導致%出來的答案錯誤),而要%(k *m);

 

 

%與mod的區別 :http://blog.csdn.net/deng_hui_long/article/details/9966197

 

思路: 用BFS(廣度優先搜尋)

import java.io.*;import java.util.*;/* * @author denghuilong  *   * 2013-8-14下午5:08:37 **/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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.