{"id":65,"date":"2011-03-29T07:40:27","date_gmt":"2011-03-28T23:40:27","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=65"},"modified":"2012-03-03T07:43:36","modified_gmt":"2012-03-02T23:43:36","slug":"uva-756-biorhythms","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/uva-756-biorhythms\/","title":{"rendered":"UVa 756. Biorhythms"},"content":{"rendered":"<h3>Breif description :<\/h3>\n<p>Give you a system of modular arithmetic equation ..<br \/>\nYour task is to calculate the minimum solution.<br \/>\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>Solve the system of the modular arithmetic equation is a fundament in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Number_theory\">Number Theroy<\/a>, There is a method called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chinese_remainder_theorem\">Chinese Remainder Theorem<\/a> could give us a good answer.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <cstdio>\r\nusing namespace std;\r\n\r\nconst int M1 = 23 , M2 = 28, M3 = 33, M = M1 * M2 * M3;\r\nint m1, m2, m3, mm1, mm2, mm3, c1, c2, c3;\r\nint r1, r2, r3, x0;\r\nint n, a;\r\n\r\n\r\ninline void _M(int &a){\r\n\tif (a < 0) a += M;\r\n\ta %= M;\r\n}\r\n\r\ninline int _M(const int&#038; a, const int&#038; n){\r\n\tif (a < 0) return a + n;\r\n\treturn a;\r\n}\r\n\r\ninline int _I(int b, const int&#038; n){\r\n\tint a = n;\r\n\tint x1 = 0, x2 = 1, q;\r\n\twhile (true){\r\n\t\tq = a \/ b, a %= b;\r\n\t\tif (!a) return _M(x2, n);\r\n\t\tx1 -= q * x2;\r\n\t\tq = b \/ a, b %= a;\r\n\t\tif (!b) return _M(x1, n);\r\n\t\tx2 -= q * x1;\r\n\t}\r\n}\r\n\r\ninline int _CRT(){\r\n\tint res = r1 * c1; _M(res);\r\n\tres += r2 * c2, _M(res);\r\n\tres += r3 * c3, _M(res);\r\n\tres -= x0, _M(res);\r\n\treturn res;\r\n}\r\n\r\n\r\n\r\n\r\nint main(){\r\n\tm1 = M \/ M1, m2 = M \/ M2, m3 = M \/ M3;\r\n\tmm1 = _I(m1, M1), mm2 = _I(m2, M2), mm3 =  _I(m3, M3);\r\n\tc1 = _M(m1 * mm1, M), c2 = _M(m2 * mm2, M), c3 = _M(m3 * mm3, M);\r\n\t\r\n\tint T = 0;\r\n\twhile (cin >> n && n!=0){\r\n\t\tprintf(\"Case %d: the next triple peak occurs in %d days.\\n\", ++T, _CRT() ? _CRT() : M);\r\n\t}\r\n}\r\n<\/pre>\n<p><\/bk><\/bk><\/p>\n<h3>Further discussion :<\/h3>\n<p>This problem has fixed modulus, which makes it more appropriate to do so, but &#8230; Implement the algorithm naively according to the original CRT has a few restriction.[nbnote ]<br \/>\n<a href=\"http:\/\/hi.baidu.com\/aekdycoin\/blog\/item\/71d7a842b93f611b73f05da4.html\"><br \/>\nHere have another method called Merge-Equation Could extend its scope, which is equivalence on principle, different on motivation  &#8230;<\/a>[\/nbnote] <\/p>\n<p>Here we give another iteration algorithm :<\/p>\n<pre> int x = r[0], step = m[0];\r\n for (int i = 1; i < n; i ++){\r\n\twhile ((x - a[i]) % m[i] != r[i])\r\n\t\tx += step;\r\n\tstep = lcm(step, m[i]);\r\n }<\/pre>\n<p> This algorithm is base on the brute force enumerate method, The inner while loop actually is to solve the following equation:<\/p>\n<p> x + ? step = r[i] (mod m[i])<\/p>\n<p> ... And this step could work out with a exEuclid algorithm which is similar to the previous one... <\/p>\n<p>The method maybe more straight and powerful (faster?), to taste those subtle distinction, try <a href=\"http:\/\/uva.onlinejudge.org\/index.php?option=com_onlinejudge&#038;Itemid=8&#038;category=9&#038;page=show_problem&#038;problem=641\">UVa 700. Date Bugs<\/a> ... (or Poj ???)<\/p>\n<h3>External link :<\/h3>\n<ul>\n<p><a href=\"http:\/\/uva.onlinejudge.org\/index.php?option=com_onlinejudge&#038;Itemid=8&#038;category=9&#038;page=show_problem&#038;problem=697\"><br \/>\nhttp:\/\/uva.onlinejudge.org\/index.php?option=com_onlinejudge&Itemid=8&category=9&page=show_problem&problem=697<\/a><br \/>\n<a href=\" http:\/\/hi.baidu.com\/aekdycoin\/blog\/item\/42b0cb109e728074ca80c4cb.html\"><br \/>\nhttp:\/\/hi.baidu.com\/aekdycoin\/blog\/item\/42b0cb109e728074ca80c4cb.html<\/a><\/ul>\n<p>[nbnote print=\"true\" ]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Breif description : Give you a system of modular arithmetic equation .. Your task is to calculate the minimum solution.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[31],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-uva"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-13","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}