hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))
Remainder
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3036 Accepted Submission(s): 679
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.
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.
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
*+
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<queue> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 int n , k , m , ini , km ; 7 int en ; 8 bool vis[1000010] ; 9 struct node 10 { 11 int w ; 12 int dir , nxt , step ; 13 }e[1000001]; 14 int l , r ; 15 /* 16 bool cmp (const node &a , const node &b) 17 { 18 if (a.step < b.step ) return true ; 19 if (a.step == b.step ) return a.dir < b.dir ; 20 return false ; 21 }*/ 22 23 int calc (int u , int id) 24 { 25 if (id == 0) return (u + m) % km; 26 else if (id == 1) return (u - m) % km ; 27 else if (id == 2) return (u * m) % km ; 28 else return (u % m + m) % m % km; 29 } 30 31 bool bfs () 32 { 33 // printf ("ini=%d\n" , ini ) ; 34 node tmp , ans ; 35 l = 0 , r = 1 ; 36 vis[ (n % k + k) % k] = 1 ; 37 e[l].w = n , e[l].dir = -1 , e[l].nxt = -1 , e[l].step = 0 ; 38 while ( l != r) { 39 // std::sort (e + l , e + r , cmp ) ; 40 ans = e[l] ; 41 // printf ("S---%d = %d\n" , ans.w , ans.step ) ; 42 for (int i = 0 ; i < 4 ; i ++) { 43 tmp = ans ; 44 tmp.w = calc (tmp.w , i) ; 45 if (vis[(tmp.w % k + k) % k]) continue ; vis[ (tmp.w % k + k) % k] = 1 ; 46 tmp.dir = i ; tmp.nxt = l ; tmp.step ++ ; 47 e[r ++] = tmp ; 48 if ( ((tmp.w % k + k) % k ) == ini) { 49 // printf ("final : %d\n" , tmp.step ) ; 50 // printf ("answer:%d\n" , tmp.w ) ; 51 return true ; 52 } 53 // printf ("%d = %d\n" , tmp.w , tmp.step ) ; 54 } 55 l ++ ; 56 } 57 return false ; 58 } 59 60 void dfs (int id , int deep) 61 { 62 if (e[id].nxt == -1) { 63 printf ("%d\n" , deep ) ; 64 return ; 65 } 66 // printf ("ID=%d , %d \n" , id , e[id].dir ) ; 67 dfs (e[id].nxt , deep + 1) ; 68 int t = e[id].dir ; 69 // printf ("t=%d\n" , t ) ; 70 if (t == 0) printf ("+") ; 71 else if (t == 1) printf ("-") ; 72 else if (t == 2) printf ("*") ; 73 else if (t == 3) printf ("%%") ; 74 } 75 76 int main () 77 { 78 // freopen ("a.txt" , "r" , stdin ) ; 79 while (~ scanf ("%d%d%d" , &n , &k , &m )) { 80 if (n == 0 && k == 0 && m == 0) break ; 81 memset (vis , 0 , sizeof(vis)) ; 82 ini = ((n+1)%k + k) % k ; 83 /* if (bfs () ) {puts ("yes") ; printf ("l=%d\n" , l ) ; } 84 else puts ("no") ;*/ 85 km = k * m ; 86 if (bfs ()) dfs (r - 1, 0) ; 87 else printf ("0") ; 88 puts ("") ; //puts ("") ; 89 } 90 return 0 ; 91 }
wa到死。
一个个坑等你跳,比如说printf ("%%") ;
% (k * m) ;
mod : a mod b = (a % b + b) % b ;
http://www.cnblogs.com/qiufeihai/archive/2012/08/28/2660272.html