{"id":1175,"date":"2024-09-07T09:00:00","date_gmt":"2024-09-07T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=1175"},"modified":"2024-08-20T22:08:29","modified_gmt":"2024-08-20T14:08:29","slug":"zerojudge-b003","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-b003\/","title":{"rendered":"ZeroJudge B003: Expression + \u2013 Symbol Setting Issue"},"content":{"rendered":"<p>Given an expression: \u00b1 1 \u00b1 2 \u00b1 3 \u00b1 \u2026 \u00b1 n = k, you must determine the operation symbol, either + or \u2013, in front of each number to find the smallest value of n such that this expression equals k.<\/p>\n\n\n\n<p>For example, if k=12, the expression would be:<\/p>\n\n\n\n<p>&#8211; 1 + 2 + 3 + 4 + 5 + 6 &#8211; 7 = 12<\/p>\n\n\n\n<p>In this case, the smallest value of n is 7.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Inputs\/Outputs<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Sample Input(s)<\/th><th>Sample Output(s)<\/th><\/tr><\/thead><tbody><tr><td>The input is EOF (End Of File). Each test case is on a single line and contains one integer k (where 0 \u2264 \u2223k\u2223 \u2264 1,000,000,000).<\/td><td>For each test case, output a single line with the smallest possible value of n (where 1 \u2264 n) such that the result of the expression equals the input value k.<\/td><\/tr><tr><td>12<br>-3646397<\/td><td>7<br>2701<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Thought Process<\/h2>\n\n\n\n<p>Sum the numbers from 1 to n. When the cumulative sum exceeds n, check if the difference between the cumulative sum and n is even. If it is even, then the current value of n is the answer.<\/p>\n\n\n\n<p>If n is negative, convert it to a positive number. Also, ensure that the variable used for summation is of type Long Long Int.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Code\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=b003\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge B003: Expression + \u2013 Symbol Setting Issue<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N;\n    while (cin &gt;&gt; N) {\n        N = abs(N);\n        long long int sum = 0;\n        for (int i = 1; i&lt;=1000000000; i++) {\n            sum += i;\n            if (sum == N) {\n                cout &lt;&lt; i &lt;&lt; &quot;\\n&quot;;\n                break;\n            }\n            if (sum &gt; N) {\n                if ((sum - N) % 2 == 0) {\n                    cout &lt;&lt; i &lt;&lt; &quot;\\n&quot;;\n                    break;\n                }\n            }\n        }\n    }\n}\n\n\/\/ZeroJudge B003\n\/\/Dr. SeanXD<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u7d66\u5b9a\u4e00\u500b\u904b\u7b97\u5f0f\uff1a \u00b1 1 \u00b1 2 \u00b1 3 \u00b1 &#8230; \u00b1 n = k\uff0c\u4f60\u5fc5\u9808\u6c7a\u5b9a\u6bcf\u500b\u6578\u5b57\u524d\u7684\u904b\u7b97\u7b26\u865f\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[29],"tags":[15,8,13,9],"class_list":["post-1175","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-long-long-int","tag-8","tag-13","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1177,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1177"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}