Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2927 Accepted Submission(s): 939
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<queue> 5 int T , mod , c , n ; 6 char a[500 + 10] ; 7 bool vis[5100 + 10] ; 8 int num[20] ; 9 struct node 10 { 11 int step , ret ; 12 node () {} 13 node (int step , int ret) : step (step) , ret (ret) {} 14 }; 15 struct link 16 { 17 int v , nxt ; 18 }e[200000 + 10]; 19 void echo (int an) 20 { 21 int k = 0 ; 22 for (int i = an ; ~ i ; i = e[i].nxt) { 23 if (e[i].v < 10) a[k ++] = ‘0‘ + e[i].v ; 24 else if (e[i].v >= 10) a[k ++] = e[i].v - 10 + ‘A‘ ; 25 } 26 // printf ("\nk=%d\n" , k) ; 27 for (int i = k - 1 ; i >= 0 ; i --) { 28 printf ("%c" , a[i]) ; 29 } 30 printf ("\n") ; 31 } 32 33 bool bfs () 34 { 35 int E = -1 ; 36 std::queue<node> q ; 37 while (!q.empty ()) q.pop () ; 38 memset (vis , 0 , sizeof(vis)) ; 39 node ans , tmp ; 40 int cnt = E + 1 ; 41 for (int i = 0 ; i < n ; i ++) { 42 if (num[i] != 0 && !vis[num[i]%mod]) { 43 ans = node (1 , num[i]%mod) ; 44 e[cnt].v = num[i] ; e[cnt].nxt = E ; cnt ++ ; 45 vis[num[i]%mod] ; 46 if (num[i]%mod == 0) {echo (cnt - 1) ; return true ;} 47 q.push (ans) ; 48 } 49 } 50 // for (int i = 0 ; i < n ; i++) printf ("%d " , num[i]) ; printf ("\n") ; 51 while (!q.empty ()) { 52 ans = q.front () ; q.pop () ; 53 E ++ ; 54 // printf ("step=%d , ret=%d\n" , ans.step , ans.ret) ; 55 for (int i = 0 ; i < n ; i++) { 56 tmp.ret = (ans.ret * c + num[i]) % mod ; 57 tmp.step = ans.step + 1 ; 58 // printf ("+%d=(%d)\n" , num[i],tmp.ret) ; 59 if (tmp.step > 500) continue ; 60 if (vis[tmp.ret]) continue ; 61 e[cnt].v = num[i] ; e[cnt].nxt = E ; cnt ++ ; 62 // printf ("(%d)ans.ret=%d\n" , num[i] , ans.ret) ; 63 if (tmp.ret == 0) {echo (cnt - 1) ; return true ;} 64 vis[tmp.ret] = 1 ; 65 q.push (tmp) ; 66 } 67 } 68 return false ; 69 } 70 71 int main () 72 { 73 //freopen ("a.txt" , "r" , stdin ) ; 74 scanf ("%d" , &T) ; 75 while (T--) { 76 scanf ("%d%d%d" , &mod , &c , &n) ; 77 // printf ("mod=%d,c=%d,n=%d\n" , mod , c , n ) ; 78 // printf ("hehe :%s\n" , a ) ; 79 for (int i = 0; i < n; i++){ 80 scanf("%s", a); 81 if (a[0] <= ‘9‘ && a[0] >= ‘0‘) num[i] = a[0] - ‘0‘; 82 else if (a[0] <= ‘F‘ && a[0] >= ‘A‘) num[i] = a[0] - ‘A‘ + 10; 83 } 84 std::sort (num , num + n) ; 85 if (mod == 0) { 86 if (num[0] == 0) puts ("0") ; 87 else puts ("give me the bomb please") ; 88 continue ; 89 } 90 if ( !bfs () ) puts ("give me the bomb please") ; 91 } 92 return 0 ; 93 }
bfs能用来生成数,给赞啊。规避了大数的做法也很6.
原文:http://www.cnblogs.com/get-an-AC-everyday/p/4441264.html