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.
 
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
*+

  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[] ;
  9. struct node
  10. {
  11. int w ;
  12. int dir , nxt , step ;
  13. }e[];
  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 == ) return (u + m) % km;
  26. else if (id == ) return (u - m) % km ;
  27. else if (id == ) 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 = , r = ;
  36. vis[ (n % k + k) % k] = ;
  37. e[l].w = n , e[l].dir = - , e[l].nxt = - , e[l].step = ;
  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 = ; i < ; 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] = ;
  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 == -) {
  63. printf ("%d\n" , deep ) ;
  64. return ;
  65. }
  66. // printf ("ID=%d , %d \n" , id , e[id].dir ) ;
  67. dfs (e[id].nxt , deep + ) ;
  68. int t = e[id].dir ;
  69. // printf ("t=%d\n" , t ) ;
  70. if (t == ) printf ("+") ;
  71. else if (t == ) printf ("-") ;
  72. else if (t == ) printf ("*") ;
  73. else if (t == ) 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 == && k == && m == ) break ;
  81. memset (vis , , sizeof(vis)) ;
  82. ini = ((n+)%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 - , ) ;
  87. else printf ("") ;
  88. puts ("") ; //puts ("") ;
  89. }
  90. return ;
  91. }

wa到死。
一个个坑等你跳,比如说printf ("%%") ;

% (k * m) ;

mod : a mod b = (a % b + b) % b ;

http://www.cnblogs.com/qiufeihai/archive/2012/08/28/2660272.html

hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))的更多相关文章

  1. HDU 1104 Remainder( BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  2. hdu - 1104 Remainder (bfs + 数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=1104 注意这里定义的取模运算和计算机的%是不一样的,这里的取模只会得到非负数. 而%可以得到正数和负数. 所以需 ...

  3. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  4. HDU 1104 Remainder (BFS)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1104 题意:给你一个n.m.k,有四种操作n+m,n-m,n*m,n%m,问你最少经过多少步,使得最后 ...

  5. HDU 1104 Remainder

    与前一题类似,也是BFS+记录路径, 但是有很多BUG点, 第一MOD操作与%不同i,其实我做的时候注意到了我们可以这样做(N%K+K)%K就可以化为正数,但是有一点要注意 N%K%M!=N%M%K; ...

  6. HDU 1104 Remainder (BFS求最小步数 打印路径)

    题目链接 题意 : 给你N,K,M,N可以+,- ,*,% M,然后变为新的N,问你最少几次操作能使(原来的N+1)%K与(新的N)%k相等.并输出相应的操作. 思路 : 首先要注意题中给的%,是要将 ...

  7. HDU 1104 Remainder (BFS(广度优先搜索))

    Remainder Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  8. hdu 1104 数论+bfs

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

  9. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

随机推荐

  1. UVA12034Race(DP)

    题目链接 题意:A.B两人赛马,最终名次有三种可能:并列第一:A第一,B第二:B第一,A第二.输入n,求 n 人赛马时最终名次的可能,结果对10056取余 分析:对于 i 匹马来说,有 i 中可能,设 ...

  2. 理解和使用 JavaScript 中的回调函数

    理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报  分类: JavaScript(4)    目录( ...

  3. gnuplot配置HOME目录

    http://blog.csdn.net/jspenliany/article/details/39828261 本人使用gnuplot绘图,使用console version的来进行处理的时候,经常 ...

  4. css属性设置

    css在线编辑工具地址:http://tool.chinaz.com/Tools/CssDesigner.aspx 案例详情: http://dongtianee.sinaapp.com/index. ...

  5. JSP简单标签开发

    一.继承自SimpleTag接口的自定义标签实现类称为简单标签,接口中5个方法 1.setJspContext方法 用于把JSP页面的PageContext对象传递给标签处理器对象 2.setPare ...

  6. Java——标签组件:JLabel

    使用一个标签 import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import javax.swing.J ...

  7. OpenGL Driver Architecture[转]

    http://www.cnblogs.com/cgwolver/archive/2009/01/04/1368350.html

  8. Could not load file or assembly Microsoft.Web.Infrastructure

    Error info:Could not load file or assembly 'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=n ...

  9. AutoMocker单元测试

    /// <summary> /// 测试获取所有物流 /// </summary> [TestMethod] public void TestExpressController ...

  10. IIS7/IIS7.5中目录执行权限的设置方法

    我们在建站的时候,通常有些目录必须给写入权限,这个时候这些目录就很可能被人写入脚本文件,为了将安全性维护得更好,我们可以关闭这些有写入权限的目录的脚本执行权限.IIS6的时候,我们很容易找到关闭的地方 ...