{"id":4186,"date":"2018-12-27T22:34:58","date_gmt":"2018-12-27T19:34:58","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=4186"},"modified":"2018-12-27T23:00:57","modified_gmt":"2018-12-27T20:00:57","slug":"e-olymp-1868-%d1%84%d1%83%d0%bd%d0%ba%d1%86%d0%b8%d1%8f","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=4186","title":{"rendered":"e-olymp 1868. \u0424\u0443\u043d\u043a\u0446\u0438\u044f"},"content":{"rendered":"<p><strong>\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong><br \/>\n\u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<br \/>\n$$f(n)=\\begin{cases} 1, \\text{ \u0435\u0441\u043b\u0438 } n \\leq 2 \\\\ f(\\lfloor \\frac{6\\cdot n}{7} \\rfloor)+f(\\lfloor \\frac{2\\cdot n}{3} \\rfloor), \\text{ \u0435\u0441\u043b\u0438 } n \\mod \\; 2 = 1  \\\\ f(n-1)+f(n-3), \\text{ \u0435\u0441\u043b\u0438 } n \\mod \\; 2 = 0 \\end{cases}$$<br \/>\n<strong>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 <\/strong><br \/>\n\u041e\u0434\u043d\u043e \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e $n$ $(1 \\leq n \\leq 10^{12})$<br \/>\n<strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 <\/strong><br \/>\n\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 $f(n)$ , \u0432\u0437\u044f\u0442\u043e\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e $2^{32}$.<br \/>\n<strong>\u0422\u0435\u0441\u0442\u044b <\/strong><\/p>\n<table style=\"width: 297px\">\n<tbody>\n<tr>\n<th style=\"width: 142px\">\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/th>\n<th style=\"width: 155px\">\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/th>\n<\/tr>\n<tr>\n<td style=\"width: 142px\">\n<h5>10<\/h5>\n<\/td>\n<td style=\"width: 155px\">\n<h5>7<\/h5>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 142px\">\n<h5>123<\/h5>\n<\/td>\n<td style=\"width: 155px\">\n<h5>229966<\/h5>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 142px\">\n<h5>1<\/h5>\n<\/td>\n<td style=\"width: 155px\">\n<h5>1<\/h5>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 142px\">\n<h5>100<\/h5>\n<\/td>\n<td style=\"width: 155px\">\n<h5>136345<\/h5>\n<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 142px\">\n<h5>51<\/h5>\n<\/td>\n<td style=\"width: 155px\">\n<h5>5092<\/h5>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/strong><\/p>\n<pre class=\"lang:java decode:true \" title=\"e-olymp 1868. Function\">import java.io.BufferedReader;\r\nimport java.io.InputStreamReader;\r\nimport java.util.Map;\r\nimport java.util.HashMap;\r\nimport java.lang.Math;\r\n\r\nclass Main\r\n{\r\n\r\n    private static long mod = 1L &lt;&lt; 32;\r\n    public static Map&lt;Long, Long&gt; mp = new HashMap&lt;Long, Long&gt;();\r\n\r\n    public static long f(long n) {\r\n        if (n &lt;= 2) {\r\n            return 1;\r\n        }\r\n        if (mp.containsKey(n)) {\r\n            return mp.get(n);\r\n        }\r\n        if (n % 2 == 1) {\r\n            long d = (f(6 * n \/ 7) % mod + f(2 * n \/ 3) % mod) % mod;\r\n            mp.put(n, d);\r\n            return mp.get(n);\r\n        }\r\n        long o = (f(n - 1) % mod + f(n - 3) % mod) % mod;\r\n        mp.put(n, o);\r\n        return mp.get(n);\r\n    }\r\n\r\n\r\n    public static void main (String[] args) throws java.lang.Exception\r\n    {\r\n        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));\r\n        String[] params1 = bufferedReader.readLine().split(\" \");\r\n        long n;\r\n        n = Long.parseLong(params1[0]);\r\n        System.out.print(f(n));\r\n    }\r\n}<\/pre>\n<p><strong>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/strong><br \/>\n\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0442\u043e\u0438\u0442 \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438. \u0422\u0430\u043a \u043a\u0430\u043a \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0438 \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u0438 \u0438 \u0442\u0435 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u043b\u0433\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u0437\u0430\u0434\u0430\u0447\u0443. \u0422\u043e \u0435\u0441\u0442\u044c, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u043d\u0430 5 \u0438 20 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c, \u0447\u0442\u043e \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0435\u0449\u0435 \u0440\u0430\u0437, \u0430 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442, \u0447\u0435\u043c\u0443 \u0440\u0430\u0432\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438.\u0414\u0430\u043b\u0435\u0435, \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e, \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043d\u0430\u0448\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<ul>\n<li>\u0415\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0432\u0435\u043b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u043c\u0435\u043d\u044c\u0448\u0435 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u043d\u043e \u0434\u0432\u0443\u043c, \u0442\u043e \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u0438\u043d.<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441 \u0442\u0430\u043a\u0438\u043c \u0436\u0435 \u043a\u043b\u044e\u0447\u043e\u043c, \u0442\u043e \u043e\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u0432\u0435\u043b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c, \u0434\u0430\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043e\u0442 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0434\u0438\u043d, \u0442\u043e \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 $f(\\lfloor \\frac{6\\cdot n}{7} \\rfloor)+f(\\lfloor \\frac{2\\cdot n}{3} \\rfloor)$.<\/li>\n<li>\u0412\u043e \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043d\u0430\u0448\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 $f(n-1)+f(n-3)$.<\/li>\n<\/ul>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0438<\/strong><br \/>\n\u0417\u0430\u0434\u0430\u0447\u0430 \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 <a href=\"https:\/\/www.e-olymp.com\/ru\/problems\/1868\">e-olymp<\/a><br \/>\n\u041a\u043e\u0434 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 <a href=\"https:\/\/ideone.com\/g7Fa75\">Ideone<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e: $$f(n)=\\begin{cases} 1, \\text{ \u0435\u0441\u043b\u0438 } n \\leq 2 \\\\ f(\\lfloor \\frac{6\\cdot n}{7} \\rfloor)+f(\\lfloor \\frac{2\\cdot n}{3} \\rfloor), \\text{ \u0435\u0441\u043b\u0438 } n \\mod \\; 2 = 1 \\\\ f(n-1)+f(n-3), \\text{ \u0435\u0441\u043b\u0438 } n \\mod \\; 2 = 0 \\end{cases}$$ \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u041e\u0434\u043d\u043e \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e $n$ $(1 \\leq n \\leq 10^{12})$ \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=4186\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":112,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,107],"tags":[617,71],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/4186"}],"collection":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4186"}],"version-history":[{"count":4,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/4186\/revisions"}],"predecessor-version":[{"id":4744,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/4186\/revisions\/4744"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}