{"id":9656,"date":"2022-04-16T22:26:31","date_gmt":"2022-04-17T05:26:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9656"},"modified":"2022-04-16T22:27:43","modified_gmt":"2022-04-17T05:27:43","slug":"leetcode-2240-number-of-ways-to-buy-pens-and-pencils","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2240-number-of-ways-to-buy-pens-and-pencils\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2240. Number of Ways to Buy Pens and Pencils"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>total<\/code>&nbsp;indicating the amount of money you have. You are also given two integers&nbsp;<code>cost1<\/code>&nbsp;and&nbsp;<code>cost2<\/code>&nbsp;indicating the price of a pen and pencil respectively. You can spend&nbsp;<strong>part or all<\/strong>&nbsp;of your money to buy multiple quantities (or none) of each kind of writing utensil.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number of distinct ways<\/strong>&nbsp;you can buy some number of pens and pencils.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> total = 20, cost1 = 10, cost2 = 5\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> The price of a pen is 10 and the price of a pencil is 5.\n- If you buy 0 pens, you can buy 0, 1, 2, 3, or 4 pencils.\n- If you buy 1 pen, you can buy 0, 1, or 2 pencils.\n- If you buy 2 pens, you cannot buy any pencils.\nThe total number of ways to buy pens and pencils is 5 + 3 + 1 = 9.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> total = 5, cost1 = 10, cost2 = 10\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The price of both pens and pencils are 10, which cost more than total, so you cannot buy any writing utensils. Therefore, there is only 1 way: buy 0 pens and 0 pencils.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= total, cost1, cost2 &lt;= 10<sup>6<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Enumerate all possible ways to buy k pens, e.g. 0 pen, 1 pen, &#8230;, total \/ cost1.<br>The way to buy pencils are (total &#8211; k * cost1) \/ cost2 + 1.<br>ans = sum((total &#8211; k * cost1) \/ cost2 + 1)) for k = 0 to total \/ cost1.<\/p>\n\n\n\n<p>Time complexity: O(total \/ cost1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  long long waysToBuyPensPencils(int total, int cost1, int cost2) {\n    long long ans = 0;\n    for (long long k = 0; k <= total \/ cost1; ++k)\n      ans += (total - k * cost1) \/ cost2 + 1;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;total&nbsp;indicating the amount of money you have. You are also given two integers&nbsp;cost1&nbsp;and&nbsp;cost2&nbsp;indicating the price of a pen and pencil respectively.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31],"class_list":["post-9656","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9656"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9656\/revisions"}],"predecessor-version":[{"id":9658,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9656\/revisions\/9658"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}