标签:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3036 Accepted Submission(s): 679
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
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4479860.html