id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\nbool isDivisible(string num, int k)\n{\n int len = num.length();\n if (len == 1)\n {\n return (num[0] - '0') % k == 0;\n }\n if (k == 1)\n {\n return true;\n }\n if (k == 2)\n {\n int x = (num[len - 1] - '0');\n return x % 2 == 0;\n }\n if (k == 3 || k == 9)\n {\n int sum = 0;\n for (int i = 0; i < len; i++)\n {\n int x = num[i] - '0';\n sum += x;\n }\n\n return sum % k == 0;\n }\n\n if (k == 4)\n {\n int sum = 0;\n sum += (num[len - 1] - '0') + 10 * (num[len - 2] - '0');\n return sum % 4 == 0;\n }\n if (k == 5)\n {\n return num[len - 1] == '0' || num[len - 1] == '5';\n }\n\n if (k == 6)\n {\n int x = (num[len - 1] - '0');\n int sum = 0;\n for (int i = 0; i < len; i++)\n {\n int x = num[i] - '0';\n sum += x;\n }\n\n return x % 2 == 0 && sum % 3 == 0;\n }\n if (k == 7)\n {\n long long sum = 0;\n long long multiplier = 1;\n for (int i = len - 1; i >= 0; i--)\n {\n sum += (num[i] - '0') * multiplier;\n multiplier *= 10;\n }\n\n return sum % k == 0;\n }\n\n if (k == 8)\n {\n int sum = 0;\n int m = 1;\n for (int i = len - 1; i >= 0 && i >= len - 3; i--)\n {\n sum += m * (num[i] - '0');\n m *= 10;\n }\n\n return sum % k == 0;\n }\n\n return true;\n}\n\nunsigned long long factorial(int num)\n{\n if (num == 0 || num == 1)\n return 1;\n unsigned long long result = 1;\n for (int i = 2; i <= num; ++i)\n {\n result *= i;\n }\n return result;\n}\n\nunsigned long long findRearrangements(string num)\n{\n std::vector<int> frequencies(10, 0);\n int len = num.length();\n\n // Count frequencies of each digit\n for (int i = 0; i < len; i++)\n {\n frequencies[num[i] - '0']++;\n }\n\n // If there are leading zeros\n unsigned long long totalCombinations = 0;\n if (frequencies[0])\n {\n // Calculate permutations where leading zero is not allowed\n // We need to find permutations of len-1 digits when the first digit is non-zero\n for (int firstDigit = 1; firstDigit < 10; ++firstDigit)\n {\n if (frequencies[firstDigit] > 0)\n {\n frequencies[firstDigit]--;\n unsigned long long numerator = factorial(len - 1);\n unsigned long long denominator = 1;\n\n for (int i = 0; i < 10; ++i)\n {\n denominator *= factorial(frequencies[i]);\n }\n\n totalCombinations += numerator / denominator;\n frequencies[firstDigit]++;\n }\n }\n }\n else\n {\n // Calculate permutations with no leading zero restriction\n unsigned long long numerator = factorial(len);\n unsigned long long denominator = 1;\n\n for (int i = 0; i < 10; ++i)\n {\n denominator *= factorial(frequencies[i]);\n }\n\n totalCombinations = numerator / denominator;\n }\n\n return totalCombinations;\n}\n\nvector<string> palindromes(int n)\n{\n\n if (n == 1)\n {\n return {\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"};\n }\n if (n == 2)\n {\n return {\"00\", \"11\", \"22\", \"33\", \"44\", \"55\", \"66\", \"77\", \"88\", \"99\"};\n }\n\n vector<string> arr = palindromes(n - 2);\n vector<string> returnArr;\n for (int i = 0; i < arr.size(); i++)\n {\n string s = arr[i];\n for (int j = 0; j <= 9; j++)\n {\n char c = j + 48;\n returnArr.push_back(c + s + c);\n }\n }\n\n return returnArr;\n}\n\nlong long countGoodIntegers(int n, int k)\n{\n vector<string> goodStrings = palindromes(n);\n\n long long count = 0;\n unordered_set<string> mySet;\n\n for (string str : goodStrings)\n {\n if (str[0] == '0')\n {\n continue;\n }\n bool divisible = isDivisible(str, k);\n sort(str.begin(), str.end());\n if (divisible && mySet.find(str) == mySet.end())\n {\n mySet.insert(str);\n count += findRearrangements(str);\n }\n }\n\n return count;\n}\n};",
"memory": "214579"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\nbool isDivisible(string num, int k)\n{\n int len = num.length();\n if (len == 1)\n {\n return (num[0] - '0') % k == 0;\n }\n if (k == 1)\n {\n return true;\n }\n if (k == 2)\n {\n int x = (num[len - 1] - '0');\n return x % 2 == 0;\n }\n if (k == 3 || k == 9)\n {\n int sum = 0;\n for (int i = 0; i < len; i++)\n {\n int x = num[i] - '0';\n sum += x;\n }\n\n return sum % k == 0;\n }\n\n if (k == 4)\n {\n int sum = 0;\n sum += (num[len - 1] - '0') + 10 * (num[len - 2] - '0');\n return sum % 4 == 0;\n }\n if (k == 5)\n {\n return num[len - 1] == '0' || num[len - 1] == '5';\n }\n\n if (k == 6)\n {\n int x = (num[len - 1] - '0');\n int sum = 0;\n for (int i = 0; i < len; i++)\n {\n int x = num[i] - '0';\n sum += x;\n }\n\n return x % 2 == 0 && sum % 3 == 0;\n }\n if (k == 7)\n {\n long long sum = 0;\n long long multiplier = 1;\n for (int i = len - 1; i >= 0; i--)\n {\n sum += (num[i] - '0') * multiplier;\n multiplier *= 10;\n }\n\n return sum % k == 0;\n }\n\n if (k == 8)\n {\n int sum = 0;\n int m = 1;\n for (int i = len - 1; i >= 0 && i >= len - 3; i--)\n {\n sum += m * (num[i] - '0');\n m *= 10;\n }\n\n return sum % k == 0;\n }\n\n return true;\n}\n\nunsigned long long factorial(int num)\n{\n if (num == 0 || num == 1)\n return 1;\n unsigned long long result = 1;\n for (int i = 2; i <= num; ++i)\n {\n result *= i;\n }\n return result;\n}\n\nunsigned long long findRearrangements(string num)\n{\n std::vector<int> frequencies(10, 0);\n int len = num.length();\n\n // Count frequencies of each digit\n for (int i = 0; i < len; i++)\n {\n frequencies[num[i] - '0']++;\n }\n\n // If there are leading zeros\n unsigned long long totalCombinations = 0;\n if (frequencies[0])\n {\n // Calculate permutations where leading zero is not allowed\n // We need to find permutations of len-1 digits when the first digit is non-zero\n for (int firstDigit = 1; firstDigit < 10; ++firstDigit)\n {\n if (frequencies[firstDigit] > 0)\n {\n frequencies[firstDigit]--;\n unsigned long long numerator = factorial(len - 1);\n unsigned long long denominator = 1;\n\n for (int i = 0; i < 10; ++i)\n {\n denominator *= factorial(frequencies[i]);\n }\n\n totalCombinations += numerator / denominator;\n frequencies[firstDigit]++;\n }\n }\n }\n else\n {\n // Calculate permutations with no leading zero restriction\n unsigned long long numerator = factorial(len);\n unsigned long long denominator = 1;\n\n for (int i = 0; i < 10; ++i)\n {\n denominator *= factorial(frequencies[i]);\n }\n\n totalCombinations = numerator / denominator;\n }\n\n return totalCombinations;\n}\n\nvector<string> palindromes(int n)\n{\n\n if (n == 1)\n {\n return {\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"};\n }\n if (n == 2)\n {\n return {\"00\", \"11\", \"22\", \"33\", \"44\", \"55\", \"66\", \"77\", \"88\", \"99\"};\n }\n\n vector<string> arr = palindromes(n - 2);\n vector<string> returnArr;\n for (int i = 0; i < arr.size(); i++)\n {\n string s = arr[i];\n for (int j = 0; j <= 9; j++)\n {\n char c = j + 48;\n returnArr.push_back(c + s + c);\n }\n }\n\n return returnArr;\n}\n\nlong long countGoodIntegers(int n, int k)\n{\n vector<string> goodStrings = palindromes(n);\n\n long long count = 0;\n unordered_set<string> mySet;\n\n for (string str : goodStrings)\n {\n if (str[0] == '0')\n {\n continue;\n }\n bool divisible = isDivisible(str, k);\n sort(str.begin(), str.end());\n if (divisible && mySet.find(str) == mySet.end())\n {\n mySet.insert(str);\n count += findRearrangements(str);\n }\n }\n\n return count;\n}\n};",
"memory": "214579"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> generate(long long min,long long max,int digits){\n vector<string> answer;\n for(long long i = min;i <= max;i++){\n string temp;\n if(!(digits%2)){\n temp = to_string(i);\n reverse(temp.begin(),temp.end());\n string result = to_string(i) + temp;\n answer.push_back(result);\n }\n\n else{\n temp = to_string(i).substr(0,to_string(i).size() - 1);\n reverse(temp.begin(),temp.end());\n string result = to_string(i) + temp;\n answer.push_back(result);\n }\n }\n return answer;\n }\n\n int count_zeros(string number){\n int zero_count = 0;\n for(auto i : number){\n if(i == '0')zero_count++;\n }\n return zero_count;\n }\n\n\n long long factorial(int n) \n { \n \n return (n==1 || n==0) ? 1: n * factorial(n - 1); \n } \n\n\n long long same_numbers(string number){\n map<char,int> freq;\n for(auto i: number)freq[i]++;\n\n long long combs = 1;\n\n for(auto i: freq){\n combs *= factorial(i.second);\n }\n\n return combs;\n }\n\n\n\n long long countGoodIntegers(int n, int k) {\n \n int digits = ceil((float)n/2.0);\n long long min = pow(10,digits - 1);\n long long max = pow(10,digits) - 1;\n \n vector<string> answers = generate(min,max,n);\n\n long long final_answer = 0;\n\n unordered_set<string> myset;\n\n for(auto i: answers){\n string temp = i;\n sort(temp.begin(),temp.end());\n if(stoll(i)%k == 0 && !(myset.find(temp) != myset.end())){\n cout<<i<<endl;\n final_answer += ((n - count_zeros(i))*(factorial(n - 1)))/same_numbers(i);\n cout<<((n - count_zeros(i))*(factorial(n - 1)))/same_numbers(i)<<endl;\n sort(i.begin(),i.end());\n myset.insert(i);\n }\n }\n\n return final_answer;\n \n }\n};",
"memory": "218455"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> generate(long long min,long long max,int digits){\n vector<string> answer;\n for(long long i = min;i <= max;i++){\n string temp;\n if(!(digits%2)){\n temp = to_string(i);\n reverse(temp.begin(),temp.end());\n string result = to_string(i) + temp;\n answer.push_back(result);\n }\n\n else{\n temp = to_string(i).substr(0,to_string(i).size() - 1);\n reverse(temp.begin(),temp.end());\n string result = to_string(i) + temp;\n answer.push_back(result);\n }\n }\n return answer;\n }\n\n int count_zeros(string number){\n int zero_count = 0;\n for(auto i : number){\n if(i == '0')zero_count++;\n }\n return zero_count;\n }\n\n\n long long factorial(int n) \n { \n \n return (n==1 || n==0) ? 1: n * factorial(n - 1); \n } \n\n\n long long same_numbers(string number){\n map<char,int> freq;\n for(auto i: number)freq[i]++;\n\n long long combs = 1;\n\n for(auto i: freq){\n combs *= factorial(i.second);\n }\n\n return combs;\n }\n\n\n\n long long countGoodIntegers(int n, int k) {\n \n int digits = ceil((float)n/2.0);\n long long min = pow(10,digits - 1);\n long long max = pow(10,digits) - 1;\n \n vector<string> answers = generate(min,max,n);\n\n long long final_answer = 0;\n\n unordered_set<string> myset;\n\n for(auto i: answers){\n string temp = i;\n sort(temp.begin(),temp.end());\n if(stoll(i)%k == 0 && !(myset.find(temp) != myset.end())){\n cout<<i<<endl;\n final_answer += ((n - count_zeros(i))*(factorial(n - 1)))/same_numbers(i);\n cout<<((n - count_zeros(i))*(factorial(n - 1)))/same_numbers(i)<<endl;\n sort(i.begin(),i.end());\n myset.insert(i);\n }\n }\n\n return final_answer;\n \n }\n};",
"memory": "218455"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> ps; \n void generatePalindromes(string & curr, int ind, bool flg = true) {\n int n = curr.size(); \n\n if (ind >= (n + 1) / 2) {\n ps.push_back(curr); \n return; \n }\n\n char start = flg ? '1' : '0'; \n\n for (char c = start; c <= '9'; c++) {\n curr[ind] = curr[n - 1 - ind] = c; \n generatePalindromes(curr, ind + 1, false); \n }\n\n return; \n }\n\n long long factorial(int n) {\n long long result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n }\n \n long long cntPermutation(const string &s) {\n int n = s.size(); \n unordered_map<char, int> freq;\n for (char c : s) freq[c]++;\n \n long long res = factorial(n);\n \n for (char c = '0'; c <= '9'; c++) \n res /= factorial(freq[c]);\n \n if (freq['0'] != 0) {\n int lres = factorial(n - 1); \n \n freq['0']--; \n \n for (char c = '0'; c <= '9'; c++) {\n lres /= factorial(freq[c]);\n }\n \n return res - lres; \n }\n \n return res; \n }\n \n\n long long countGoodIntegers(int n, int k) {\n string curr(n, ' '); \n generatePalindromes(curr, 0); \n \n long long res = 0; \n set<string> st; \n \n for (auto& x : ps) {\n string l = x; \n sort(l.begin(), l.end()); \n // cout << x << endl; \n if (stoll(x) % k == 0 && !st.count(l)) {\n long long value = cntPermutation(x); \n res += value; \n // cout << x << ' ' << value << endl; \n st.insert(l); \n }\n }\n \n return res; \n }\n};\n",
"memory": "222331"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> ps; \n void generatePalindromes(string & curr, int ind, bool flg = true) {\n int n = curr.size(); \n\n if (ind >= (n + 1) / 2) {\n ps.push_back(curr); \n return; \n }\n\n char start = flg ? '1' : '0'; \n\n for (char c = start; c <= '9'; c++) {\n curr[ind] = curr[n - 1 - ind] = c; \n generatePalindromes(curr, ind + 1, false); \n }\n\n return; \n }\n\n long long factorial(int n) {\n long long result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n }\n \n long long cntPermutation(const string &s) {\n int n = s.size(); \n unordered_map<char, int> freq;\n for (char c : s) freq[c]++;\n \n long long res = factorial(n);\n \n for (char c = '0'; c <= '9'; c++) \n res /= factorial(freq[c]);\n \n if (freq['0'] != 0) {\n int lres = factorial(n - 1); \n \n freq['0']--; \n \n for (char c = '0'; c <= '9'; c++) {\n lres /= factorial(freq[c]);\n }\n \n return res - lres; \n }\n \n return res; \n }\n \n\n long long countGoodIntegers(int n, int k) {\n string curr(n, ' '); \n generatePalindromes(curr, 0); \n \n long long res = 0; \n set<string> st; \n \n for (auto& x : ps) {\n string l = x; \n sort(l.begin(), l.end()); \n if (stoll(x) % k == 0 && !st.count(l)) {\n long long value = cntPermutation(x); \n res += value; \n st.insert(l); \n }\n }\n \n return res; \n }\n};\n",
"memory": "222331"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n\nll vectorToNumber(vector<int>& num) {\n ll ans = 0;\n for (int digit : num) {\n ans = ans * 10 + digit;\n }\n return ans;\n }\n\n ll fact(int n){\n ll ans = 1;\n for(int i=2 ; i<=n ; i++) ans *= i;\n return ans;\n }\n\n ll Total_Permutations(map<int, int> &freq, int n) {\n ll totalPermutations = fact(n);\n\n for (auto i : freq) {\n totalPermutations /= fact(i.second);\n }\n return totalPermutations;\n }\n\n // Function to calculate permutations starting with zero\n ll PermutationsStartingWithZero(map<int, int> freq, int n) {\n if (freq.find(0) == freq.end() || freq[0] == 0) { // When there's no zero \n return 0;\n }\n\n freq[0]--; // Fix one zero as the first digit\n ll permutationsWithZero = fact(n-1);\n\n for (auto& i : freq) {\n permutationsWithZero /= fact(i.second);\n }\n return permutationsWithZero;\n }\n\n ll calc(map<int,int> &freq, int n){\n ll a = Total_Permutations(freq,n), b = PermutationsStartingWithZero(freq,n);\n return a - b ; // Permutations that doesnt start with 0\n }\n set<map<int,int>> vis;\n long long help(int n, int k, int i, string ans) {\n if (i == (n / 2)) {\n ll count = 0;\n string l = ans;\n\n if (n % 2 == 1) { // Odd length\n for (int x = 0; x <= 9; ++x) {\n string palin = l + to_string(x);\n string rev_palin = l;\n reverse(rev_palin.begin(), rev_palin.end());\n palin += rev_palin;\n\n if (palin[0] == '0') continue; \n if (stoll(palin) % k == 0) {\n map<int,int> m;\n ll lkl=stoll(palin);\n while(lkl)\n {\n m[lkl%10]++;\n lkl/=10;\n }\n if(vis.find(m)!=vis.end())\n {\n continue;\n }\n ll xl=calc(m,n);\n count +=xl; \n vis.insert(m);\n }\n }\n } else { // Even length\n string palin = l;\n string rev_palin = l;\n reverse(rev_palin.begin(), rev_palin.end());\n palin += rev_palin;\n\n if (palin[0] == '0') return 0; // Skip invalid palindromes with leading zeros\n\n if (stoll(palin) % k == 0) {\n map<int,int> m;\n ll lkl=stoll(palin);\n while(lkl)\n {\n m[lkl%10]++;\n lkl/=10;\n }\n if(vis.find(m)==vis.end())\n {\n ll xl=calc(m,n);\n count +=xl; \n vis.insert(m);\n }\n }\n }\n return count;\n }\n\n long long result = 0;\n for (int x = (i == 0 ? 1 : 0); x <= 9; ++x) {\n result += help(n, k, i + 1, ans + to_string(x));\n }\n return result;\n }\n\n long long countGoodIntegers(int n, int k) {\n return help(n, k, 0, \"\");\n }\n};\n",
"memory": "226208"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long findPermutations(map<int,int>& hash, int n, vector<int>& factorial) {\n long long permutations = factorial[n];\n for (auto itr : hash) {\n permutations /= factorial[itr.second];\n }\n return permutations;\n }\n\n long long vectorToInteger(vector<int>& nums) {\n long long number = 0;\n for (int x : nums) {\n number = (number * 10) + x;\n }\n return number;\n }\n\n void generatePalindromes(int start, int end, vector<int>& nums, set<map<int,int>>& st, int k) {\n if (start > end) {\n long long number = vectorToInteger(nums);\n if (number % k != 0) return;\n\n map<int,int> mp;\n for (int x : nums) ++mp[x];\n st.insert(mp); // Insert the frequency map for uniqueness\n return;\n }\n\n for (int i = 0; i <= 9; ++i) {\n if (start == 0 && i == 0) continue; // Skip leading zero\n nums[start] = i;\n nums[end] = i;\n generatePalindromes(start + 1, end - 1, nums, st, k);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n // Generate palindrome numbers\n vector<int> nums(n, 0);\n set<map<int,int>> st;\n generatePalindromes(0, n - 1, nums, st, k);\n\n // Precompute factorials\n vector<int> factorial(n + 1, 1);\n for (int i = 1; i <= n; ++i) {\n factorial[i] = i * factorial[i - 1];\n }\n\n // Count valid distinct integers\n long long ans = 0;\n for (auto mp : st) {\n ans += findPermutations(mp, n, factorial);\n\n // Handle cases with leading zeros\n if (mp[0] > 0) {\n --mp[0];\n ans -= findPermutations(mp, n - 1, factorial);\n }\n }\n\n return ans;\n }\n};\n",
"memory": "226208"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <vector>\n#include <unordered_map>\n#include <string>\nusing namespace std;\n\ntypedef long long ll;\n\nclass Solution {\nprivate:\n // Helper function to generate palindromic numbers of given length\n \n\n // Function to convert string to number\n ll convert(const string &s) {\n ll num = 0;\n for (char ch : s) {\n num = num * 10 + (ch - '0');\n }\n return num;\n }\n \n // Function to calculate factorial of a number\n ll factorial(int n) {\n ll fact = 1;\n for (int i = 2; i <= n; ++i) {\n fact *= i;\n }\n return fact;\n }\n\n // Function to count valid permutations of the string\n ll combinations(const string &num) {\n unordered_map<char, int> freq;\n\n // Count frequency of each digit\n for (char digit : num) {\n freq[digit]++;\n }\n\n // Calculate total permutations of the digits\n ll totalPermutations = factorial(num.size());\n\n // Divide by factorial of the frequencies to handle duplicates\n for (const auto &[digit, count] : freq) {\n totalPermutations /= factorial(count);\n }\n\n // Handle invalid permutations with leading zeros\n if (freq['0'] > 0) {\n // Calculate permutations with leading zero\n freq['0']--;\n ll invalidPermutations = factorial(num.size() - 1);\n\n for (const auto &[digit, count] : freq) {\n invalidPermutations /= factorial(count);\n }\n\n // Subtract invalid permutations (leading zero cases)\n totalPermutations -= invalidPermutations;\n }\n\n return totalPermutations;\n }\n\n\n ll ans = 0;\n set<map<int,int>> vis;\n\n void generatePalindromicNumbers(int n, string ¤t, int left, int right,int k) {\n // Base case: when the left index crosses the right, we've formed a valid palindrome\n if (left > right) {\n ll pali = convert(current);\n\n if (pali % k == 0 ) {\n map<int,int>m;\n\n while(pali){\n m[pali%10]++;\n pali /= 10;\n }\n \n if(vis.find(m) == vis.end()){\n ans += combinations(current);\n vis.insert(m);\n }\n }\n return;\n }\n\n // Start digits from '1' for the first position to avoid leading zeros\n char start = (left == 0) ? '1' : '0';\n\n // Try each digit from start to '9'\n for (char ch = start; ch <= '9'; ++ch) {\n current[left] = ch;\n current[right] = ch; // Ensure symmetry\n generatePalindromicNumbers(n, current, left + 1, right - 1,k);\n }\n }\n\n // Function to return a vector of palindromic numbers of length n in string form\n void getPalindromicNumbers(int n,int k) {\n string current(n, '0'); // Initialize an empty string of length n filled with '0'\n generatePalindromicNumbers(n, current, 0, n - 1,k);\n }\n\npublic:\n // Main function to count good integers\n ll countGoodIntegers(int n, int k) {\n\n getPalindromicNumbers(n,k);\n return ans;\n }\n};\n\n",
"memory": "230084"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<string> palin;\n long long int getInt(string s){\n long long int ans=0;\n for(int i=0;i<s.length();i++){\n ans=(ans*10)+(s[i]-'0');\n }\n return ans;\n }\n map<long long int,long long int> fact;\n void generatePalin(int i,int n,string curr){\n if(i==((n)/2)){\n string res=curr;\n reverse(res.begin(),res.end());\n if((n%2)==0){\n palin.push_back(curr+res);\n }\n else{\n for(int j=0;j<=9;j++)\n palin.push_back(curr+to_string(j)+res);\n }\n return;\n }\n if(i==0) for(int j=1;j<=9;j++) generatePalin(i+1,n,curr+to_string(j));\n else for(int j=0;j<=9;j++) generatePalin(i+1,n,curr+to_string(j));\n }\n long long countGoodIntegers(int n, int k) {\n fact[0]=1;\n fact[1]=1;\n fact[2]=2;\n fact[3]=6;\n fact[4]=24;\n fact[5]=120;\n fact[6]=720;\n fact[7]=5040;\n fact[8]=40320;\n fact[9]=362880;\n generatePalin(0,n,\"\");\n\n set<string> st;\n for(auto it:palin){ \n if((getInt(it)%k==0)){\n string temp=it;\n sort(temp.begin(),temp.end());\n reverse(temp.begin(),temp.end());\n st.insert(temp);\n }}\n // long long int finalRes=0;\n long long int cnt=0;\n for(auto it:st){\n map<int,int> mp;\n cout<<it<<\" : \";\n for(auto ch:it){\n mp[ch-'0']++;\n }\n for(int i=1;i<=9;i++){\n long long int ans=0;\n if(mp[i]>0)\n { mp[i]--;\n ans=fact[n-1];\n for(int j=0;j<=9;j++) ans/=fact[mp[j]];\n mp[i]++;\n cnt+=ans;}\n }\n }\n\n return cnt;\n }\n};",
"memory": "230084"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "using ll = long long;\nll fact[11];\n\nclass Solution {\npublic:\n\n int tot;\n ll calc(vector<int> f)\n {\n ll ans = 0;\n // fix first pos\n for (int i=1; i<=9; i++)\n {\n if (f[i])\n {\n f[i]--;\n ll cur = fact[tot-1];\n for (int j=0; j<=9; j++) cur /= fact[f[j]];\n f[i]++;\n ans += cur;\n }\n }\n return ans;\n }\n\n vector<string> palin;\n int N;\n void rec(int i, string cur)\n {\n if (i==N) \n {\n palin.push_back(cur);\n return;\n }\n for (int d=0; d<=9; d++)\n {\n string next = cur;\n if (d==0)\n {\n if (i!=0) next += to_string(d);\n else continue;\n }\n else next += to_string(d);\n rec(i+1, next);\n }\n }\n\n\n long long countGoodIntegers(int n, int k) {\n if (n==1) return 9/k;\n\n tot = n;\n fact[0] = fact[1] = 1;\n for (int i=2; i<11; i++) fact[i] = fact[i-1]*i;\n\n set<vector<int>> vis;\n N = (n+1)/2; palin.clear();\n rec(0, \"\");\n \n ll ans = 0;\n for (auto &p:palin)\n {\n // if current palindrome of length n is divisible by k, then\n // all of the distinct numbers of length n which are permutations\n // of current palindrome are also valid\n\n // we'll only take permutations with distinct frequency set\n // to avoid overcounting\n\n string rev = p; if (n%2) rev.pop_back();\n reverse(rev.begin(), rev.end());\n string cur = p+rev;\n // check if cur is divisible by k\n ll num = stol(cur);\n if (num%k) continue;\n vector<int> f(10,0);\n for (auto &e:cur) f[e-'0']++;\n if (vis.find(f) != vis.end()) continue;\n vis.insert(f);\n ans += calc(f);\n }\n return ans;\n }\n};",
"memory": "233960"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int order(int n, int a){\n vector<int> aDigs(n, 0);\n for(int i=0; i<(n+1)/2; ++i){\n aDigs[i] = a%10;\n aDigs[n-1-i] = a%10;\n a /= 10;\n }\n sort(aDigs.begin(), aDigs.end());\n long long ans = 0;\n for(int i=0; i<aDigs.size(); ++i){\n ans *= 10;\n ans += aDigs[i];\n }\n return ans;\n }\n\n bool isDiv(int n, int a, int k){\n vector<int> aDigs(n, 0);\n for(int i=0; i<(n+1)/2; ++i){\n aDigs[i] = a%10;\n aDigs[n-1-i] = a%10;\n a /= 10;\n }\n long long aNew = 0;\n for(int i=0; i<n; ++i){\n aNew *= 10;\n aNew += aDigs[i];\n }\n return (aNew%k)==0;\n }\n\n int fact(int a){\n if(a==0){\n return 1;\n }\n int ans = 1;\n for(int i=1; i<=a; ++i){\n ans *= i;\n }\n return ans;\n }\n\n long long calcPerm(int n, int a){\n int b = a;\n vector<int> aHist(10, 0);\n for(int i=0; i<n/2; ++i){\n aHist[a%10] += 2;\n a /= 10;\n }\n if((n%2)==1){\n aHist[a] += 1;\n }\n\n // if(b==104){\n // for(int i=0; i<10; ++i){\n // cout << aHist[i] << \" \";\n // }\n // cout << endl;\n // }\n\n\n long long ans = fact(n);\n\n for(int i=0; i<10; ++i){\n ans /= fact(aHist[i]);\n }\n\n // if(b==104){\n // cout << ans << endl;\n // }\n\n if(aHist[0]!=0){\n long long rem = fact(n-1);\n rem /= fact(aHist[0]-1);\n for(int i=1; i<10; ++i){\n rem /= fact(aHist[i]);\n }\n ans -= rem;\n }\n\n // if(b==104){\n // cout << ans << endl;\n // }\n\n return ans;\n }\n\n long long countGoodIntegers(int n, int k) {\n unordered_map<long long,int> nums;\n long long ans =0;\n if(n==1){\n for(int i=1; i<10; ++i){\n if((i%k)==0){\n ++ans;\n }\n }\n return ans;\n }\n int a = (n+1)/2;\n int endPoint = pow(10,a);\n for(int i=1; i<endPoint; ++i){\n if((i%10)==0){\n continue;\n }\n int aOrd = order(n, i);\n if(nums[aOrd]==0){\n if(isDiv(n, i, k)){\n nums[aOrd] = 1;\n cout << i << \" \";\n ans += calcPerm(n, i);\n cout << ans << endl;\n }\n }\n }\n return ans;\n }\n};",
"memory": "233960"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\n set<map<int,int>>visited;\n vector<int>factorial;\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n void generateFactorial(){\n factorial.resize(11,1);\n int prod = 1;\n for(int i = 2;i<11;i++){\n prod *= i;\n factorial[i] = prod;\n }\n }\n void increaseCount(string &str,int n,long long&count){\n map<int,int>countFrequency;\n for(char ch : str){\n countFrequency[ch-'0']++;\n }\n if(visited.find(countFrequency) != visited.end()){\n return;\n }\n visited.insert(countFrequency);\n int nFact = factorial[n];\n for(int i = 0;i<=9;i++){\n nFact/=factorial[countFrequency[i]];\n }\n if(countFrequency[0] > 0){\n int remove0 = factorial[n-1];\n countFrequency[0]--;\n for(int i = 0;i<=9;i++){\n remove0/=factorial[countFrequency[i]];\n }\n nFact -= remove0;\n }\n count += nFact; \n }\n void solve(string& str, int index, int n, int k, long long& count) {\n if (index == ceil((1.0 * n) / 2)) {\n if (stoll(str) % k == 0) {\n increaseCount(str, n,count);\n }\n return;\n }\n for (char ch = (index == 0 ? '1' : '0'); ch <= '9'; ch++) {\n str[index] = ch;\n str[n - index - 1] = ch;\n solve(str, index + 1, n, k, count);\n str[index] = ' ';\n str[n - index - 1] = ' ';\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n long long count = 0;\n generateFactorial();\n string str(n, ' ');\n solve(str, 0, n, k, count);\n return count;\n }\n};\n",
"memory": "237836"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n #define X first\n #define Y second\n bool isdivisible(string &s, int k){\n long long mod = 0;\n for (char ch : s) {\n mod = (mod * 10 + (ch - '0')) % k;\n }\n return mod == 0;\n }\n \n \nlong long factorial(int n) {\n long long result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n}\n\n// Function to count permutations without leading zero\nset<vector<int>> aa;\nint allperm(string& numStr) {\n std::vector<int> digitCount(10, 0); // Count of each digit from 0 to 9\n\n // Count the occurrences of each digit\n for (char ch : numStr) {\n digitCount[ch - '0']++;\n }\nif(aa.find(digitCount) != aa.end()){\n return 0;\n }\n \n aa.insert(digitCount);\n\n // Total length of the number\n int totalLength = numStr.length();\n \n // Calculate total permutations\n long long totalPermutations = factorial(totalLength);\n \n // Divide by the factorial of the counts of each digit\n for (int count : digitCount) {\n if (count > 1) {\n totalPermutations /= factorial(count);\n }\n }\n\n // If the first digit is '0', we need to subtract those permutations\n if (digitCount[0] > 0) {\n // Calculate permutations with leading zero\n long long leadingZeroPermutations = factorial(totalLength - 1);\n // Reduce the count of the leading zero\n digitCount[0]--;\n \n // Divide by the factorial of the counts of each digit again\n for (int count : digitCount) {\n if (count > 1) {\n leadingZeroPermutations /= factorial(count);\n }\n }\n \n // Subtract leading zero permutations from total permutations\n totalPermutations -= leadingZeroPermutations;\n }\n\n return totalPermutations;\n}\n \n long long countGoodIntegers(int n, int k) {\n \n vector<string> v;\n \n queue<pair<string,pair<int,int>>> q;\n \n string st = \"\";\n for(int i = 0;i < n;i++){\n st += '$';\n }\n q.push({st,{0,n-1}});\n \n while(!q.empty()){\n string s = q.front().X;\n int l = q.front().Y.X;\n int r = q.front().Y.Y;\n q.pop();\n \n if(l > r){\n if(isdivisible(s,k))\n v.push_back(s);\n } \n \n for(int i = (l > 0 ? 0 : 1);i <= 9;i++){\n if(l <= r){\n s[l] = i + '0';\n s[r] = i + '0';\n q.push({s,{l+1,r-1}});\n } \n }\n }\n \n long long ans = 0;\n // cout<<v.size()<<endl;\n for(auto i:v){\n // cout<<i<<\" \";\n ans += allperm(i);\n } \n // cout<<endl;\n return ans;\n }\n};",
"memory": "237836"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\n long long vectorToNumber(const vector<int>& num) {\n long long ans = 0;\n for (int digit : num) {\n ans = ans * 10 + digit;\n }\n return ans;\n }\n\n long long fact(int n){\n long long ans = 1;\n for(int i=2 ; i<=n ; i++) ans *= i;\n return ans;\n }\n\n long long calculatePermutations(map<int, int>& count) {\n int n = 0;\n for (auto& entry : count) {\n n += entry.second;\n }\n\n long long totalPermutations = fact(n);\n\n for (auto& entry : count) {\n totalPermutations /= fact(entry.second);\n }\n\n return totalPermutations;\n }\n\n // Function to calculate permutations starting with zero\n long long calculatePermutationsStartingWithZero(map<int, int> count) {\n if (count.find(0) == count.end() || count[0] == 0) {\n return 0;\n }\n\n count[0]--; // Fix one zero as the first digit\n\n int totalDigits = 0;\n for (auto& entry : count) {\n totalDigits += entry.second;\n }\n\n long long permutationsWithZero = fact(totalDigits);\n\n for (auto& entry : count) {\n permutationsWithZero /= fact(entry.second);\n }\n return permutationsWithZero;\n }\n\npublic:\n set<map<int,int>>m;\n void generatePalindrome(vector<int>& num, int left, int right, int k) {\n if (left > right) {\n long long palindrome = vectorToNumber(num);\n\n if (palindrome % k == 0) {\n map<int,int>temp;\n while(palindrome){\n temp[palindrome%10]++;\n palindrome /= 10;\n }\n m.insert(temp);\n }\n return;\n }\n\n // Set the current digit to all possible values from 0 to 9\n for (int digit = (left == 0) ? 1 : 0; digit <= 9; ++digit) {\n num[left] = num[right] = digit;\n generatePalindrome(num, left + 1, right - 1, k);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n vector<int> num(n);\n generatePalindrome(num, 0, n-1, k);\n long long ans = 0;\n\n for(auto i : m){\n long long totalPermutations = calculatePermutations(i),\n permutationsStartingWithZero = calculatePermutationsStartingWithZero(i),\n permutationsNotStartingWithZero = totalPermutations - permutationsStartingWithZero;\n \n ans += permutationsNotStartingWithZero;\n }\n return ans;\n }\n};",
"memory": "241713"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long ans=0;\n set<map<int,int>> vis;\n long long con(vector<int>&nums){\n long long b=0;\n for(int i=0;i<nums.size();i++){\n b=b*10+nums[i];\n }\n return b;\n }\n long long fact(int x){\n long long f=1;\n for(int i=1;i<=x;i++){\n f=f*i;\n }\n return f;\n }\n long long totp(map<int,int> mp,int n){\n long long t=fact(n);\n for(auto &y : mp){\n t=t/fact(y.second);\n }\n return t;\n\n }\n long long permwz(map<int,int> mp , int n){\n if(mp.find(0)==mp.end() || mp[0]==0){\n return 0;\n }\n mp[0]--;\n long long t=fact(n-1);\n for(auto &y : mp){\n t=t/fact(y.second);\n }\n return t;\n }\n long long cal(map<int,int>& mp ,int n){\n long long a=totp(mp,n);\n long long b=permwz(mp,n);\n return a-b;\n }\n void genp(vector<int>& nums, int l,int r, int k, int n){\n if(l>r){\n long long p=con(nums);\n if(p%k==0){\n map<int,int> mp;\n while(p){\n mp[p%10]++;\n p=p/10;\n }\n if(vis.find(mp)==vis.end()){\n ans=ans+cal(mp,n);\n vis.insert(mp);\n }\n }\n return;\n\n }\n for(int i=(l==0) ? 1: 0 ; i<=9;i++){\n nums[l]=nums[r]=i;\n genp(nums,l+1,r-1,k,n);\n }\n }\n long long countGoodIntegers(int n, int k) {\n vector<int> nums(n);\n genp(nums,0,n-1,k,n);\n return ans;\n\n }\n};",
"memory": "241713"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long fact(int n){\n long long ans=1;\n for(int i=1;i<=n;i++) ans=ans*i;\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n vector<int> num;\n long long fac[12]={0};\n for(int i=0;i<12;i++) fac[i]=fact(i);\n for(int i=1;i<=9;i++) num.push_back(i);\n int req=(n+1)/2;\n for(int i=2;i<=req;i++){\n vector<int> temp;\n for(auto it:num){\n for(int x=0;x<=9;x++){\n temp.push_back(it*10+x);\n }\n }\n num=temp;\n }\n map<int,int> mp;\n long long ans=0;\n // cout<<num.size()<<endl;\n for(auto &it:num){\n long long re=it,temp=it;\n if(n&1) temp/=10;\n while(temp>0){\n re=(re*10)+(temp%10);\n temp/=10;\n }\n if(re%k==0){\n vector<int> freq(11,0);\n vector<int> xr;\n long long x=re,mn=0;\n while(x>0){\n freq[x%10]++;\n xr.push_back(x%10);\n x/=10;\n }\n sort(xr.begin(),xr.end());\n for(auto it:xr) mn=mn*10+it;\n if(mp.find(mn)!=mp.end()) continue;\n mp[mn]++;\n long long sum=0;\n for(int i=1;i<=9;i++) sum+=freq[i];\n long long curr=sum*fac[n-1];\n for(int i=0;i<=9;i++) curr/=fac[freq[i]];\n ans=ans+curr;\n }\n }\n return ans;\n }\n};",
"memory": "245589"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long fact(int n){\n long long ans=1;\n for(int i=1;i<=n;i++) ans=ans*i;\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n vector<int> num;\n for(int i=1;i<=9;i++) num.push_back(i);\n int req=(n+1)/2;\n for(int i=2;i<=req;i++){\n vector<int> temp;\n for(auto it:num){\n for(int x=0;x<=9;x++){\n temp.push_back(it*10+x);\n }\n }\n num=temp;\n }\n map<int,int> mp;\n long long ans=0;\n // cout<<num.size()<<endl;\n for(auto &it:num){\n long long re=it,temp=it;\n if(n&1) temp/=10;\n while(temp>0){\n re=(re*10)+(temp%10);\n temp/=10;\n }\n if(re%k==0){\n vector<int> freq(10,0);\n vector<int> xr;\n long long x=re,mn=0;\n while(x>0){\n freq[x%10]++;\n xr.push_back(x%10);\n x/=10;\n }\n sort(xr.begin(),xr.end());\n for(auto it:xr) mn=mn*10+it;\n if(mp.find(mn)!=mp.end()) continue;\n mp[mn]++;\n long long sum=0;\n for(int i=1;i<=9;i++) sum+=freq[i];\n long long curr=sum*fact(n-1);\n for(int i=0;i<=9;i++) curr/=fact(freq[i]);\n ans=ans+curr;\n }\n }\n return ans;\n }\n};",
"memory": "245589"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<string> perms = generatePemutations(n, true);\n\n long long answer = 0;\n\n std::unordered_set<std::vector<int>, VectorHash> st;\n\n for (string perm: perms) {\n long long p = stoll(perm);\n\n if (p == 0 || p % k != 0)\n continue;\n\n // cout << \"perm=\" << perm << endl;\n\n vector<int> counts(10, 0);\n for (char i: perm)\n ++counts[i - '0'];\n\n if (st.count(counts))\n continue;\n st.insert(counts);\n\n int curr = factorials[n];\n for (int count: counts)\n if (count > 1)\n curr /= factorials[count];\n\n if (counts[0] > 0) {\n int fact = factorials[n - 1];\n --counts[0];\n for (int count: counts)\n if (count > 1)\n fact /= factorials[count];\n\n // curr -= factorials[n - 1];\n curr -= fact;\n }\n\n answer += curr;\n }\n\n return answer;\n }\n\nprivate:\n vector<string> generatePemutations(int n, bool first = false) {\n if (n == 0)\n return {\"\"};\n if (n == 1)\n return nums;\n\n vector<string> subPerms = generatePemutations(n - 2);\n\n vector<string> permutations;\n for (string num: nums) {\n if (num == \"0\" && first)\n continue;\n\n for (string& str: subPerms)\n permutations.push_back(num + str + num);\n }\n\n return std::move(permutations);\n }\n\n struct VectorHash {\n std::size_t operator()(const std::vector<int>& vec) const {\n int sum;\n for (int i = 0; i < 10; ++i)\n sum += 10 * i * vec[i];\n return hash<int>()(sum);\n }\n };\n\n vector<string> nums = {\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"};\n vector<int> factorials = { 0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};\n};",
"memory": "249465"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\n#define append push_back\npublic:\nlong long fact(long long n){\n if (n == 0 || n == 1)\n return 1;\n return n * fact(n-1);\n}\n void solve(int index, int n, vector<string> &v, string &s){\n if (index==n) {v.append(s); return;}\n for(int i=0; i<10; i++){\n if (i==0 and index==0) continue;\n s+=to_string(i);\n solve(index+1, n, v, s);\n s.pop_back();\n }\n }\n long long countGoodIntegers(int n, int k) {\n vector<string> vv;\n char* end;\n string s=\"\";\n solve(0, n/2, vv, s);\n vector<string> v;\n if (n%2==1){\n for(auto val: vv){\n for(int i=0; i<10; i++) v.append(val+to_string(i));\n }\n }\n else v=vv;\n long long cnt=0;\n unordered_set<string> st;\n for(auto val: v){\n // cout<<val<<endl;\n if (n%2){\n string s(val.begin(), val.begin()+n/2);\n reverse(s.begin(), s.end());\n string str = val + s;\n auto num=stol(str);\n sort(str.begin(), str.end());\n if (num%k==0 and st.find(str)==st.end()) {\n unordered_map<char, long long> mp;\n for(auto val: str) mp[val]++;\n long long f = fact(n);\n for(auto pr: mp) f /= fact(pr.second);\n cnt+=f;\n st.insert(str);\n if (mp.find('0')!=mp.end()){\n long long f = fact(n-1);\n mp['0']--;\n for(auto pr: mp)f /= fact(pr.second);\n cnt-=f;\n st.insert(str);\n }\n }\n }\n else{\n string str = val;\n reverse(val.begin(), val.end());\n str+=val;\n auto num=stol(str);\n sort(str.begin(), str.end());\n\n if (num%k==0 and st.find(str)==st.end()) {\n unordered_map<char, long long> mp; \n for(auto val: str) mp[val]++;\n long long f = fact(n*1LL);\n for(auto pr: mp) f /= fact(pr.second);\n cnt+=f;\n st.insert(str);\n if (mp.find('0')!=mp.end()){\n long long f = fact(n-1);\n mp['0']--;\n for(auto pr: mp)f /= fact(pr.second);\n cnt-=f;\n }\n }\n }\n }\n // cout<<v.size()<<endl;\n return cnt;\n }\n};",
"memory": "249465"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n long long vectorToNumber(const vector<int>& num) {\n long long number = 0;\n for (int digit : num) {\n number = number * 10 + digit;\n }\n return number;\n }\n\n long long fact(int n){\n long long ans = 1;\n for(int i=2 ; i<=n ; i++) ans *= i;\n return ans;\n }\n\n long long calculatePermutations(const map<int, int>& count) {\n int totalDigits = 0;\n for (const auto& entry : count) {\n totalDigits += entry.second;\n }\n\n long long totalPermutations = fact(totalDigits);\n\n for (const auto& entry : count) {\n totalPermutations /= fact(entry.second);\n }\n\n return totalPermutations;\n }\n\n // Function to calculate permutations starting with zero\n long long calculatePermutationsStartingWithZero(map<int, int> count) {\n if (count.find(0) == count.end() || count[0] == 0) {\n return 0;\n }\n\n count[0]--; // Fix one zero as the first digit\n\n int totalDigits = 0;\n for (const auto& entry : count) {\n totalDigits += entry.second;\n }\n\n long long permutationsWithZero = fact(totalDigits);\n\n for (const auto& entry : count) {\n permutationsWithZero /= fact(entry.second);\n }\n\n return permutationsWithZero;\n }\npublic:\n vector<long long> pali;\n set<map<int,int>>m;\n\n void generatePalindrome(vector<int>& num, int left, int right, int k) {\n if (left > right) {\n long long palindrome = vectorToNumber(num);\n if (palindrome % k == 0 && palindrome) {\n pali.push_back(palindrome);\n cout<<palindrome<<endl;\n }\n return;\n }\n\n // Set the current digit to all possible values from 0 to 9\n for (int digit = (left == 0) ? 1 : 0; digit <= 9; ++digit) {\n num[left] = num[right] = digit;\n generatePalindrome(num, left + 1, right - 1, k);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n // if(k == 1)\n // return 9 * powl(10,(n+1)/2);\n \n // if (n == 1) {\n // int ans = 0;\n // for(int i = 1; i <= 9; ++i) {\n // if(i % k == 0) {\n // ans++;\n // }\n // }\n // return ans;\n // }\n\n vector<int> num(n);\n generatePalindrome(num, 0, n-1, k);\n long long ans = 0;\n\n for(auto i:pali){\n map<int,int>temp;\n while(i){\n temp[i%10]++;\n i /= 10;\n }\n m.insert(temp);\n }\n\n for(auto i : m){\n // Calculate total permutations\n long long totalPermutations = calculatePermutations(i);\n \n // Calculate permutations starting with zero\n long long permutationsStartingWithZero = calculatePermutationsStartingWithZero(i);\n \n // Calculate permutations not starting with zero\n long long permutationsNotStartingWithZero = totalPermutations - permutationsStartingWithZero;\n ans += permutationsNotStartingWithZero;\n }\n return ans;\n }\n};\n",
"memory": "253341"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n long long vectorToNumber(const vector<int>& num) {\n long long number = 0;\n for (int digit : num) {\n number = number * 10 + digit;\n }\n return number;\n }\n\n long long fact(int n){\n long long ans = 1;\n for(int i=2 ; i<=n ; i++) ans *= i;\n return ans;\n }\n\n long long calculatePermutations(const map<int, int>& count) {\n int totalDigits = 0;\n for (const auto& entry : count) {\n totalDigits += entry.second;\n }\n\n long long totalPermutations = fact(totalDigits);\n\n for (const auto& entry : count) {\n totalPermutations /= fact(entry.second);\n }\n\n return totalPermutations;\n }\n\n // Function to calculate permutations starting with zero\n long long calculatePermutationsStartingWithZero(map<int, int> count) {\n if (count.find(0) == count.end() || count[0] == 0) {\n return 0;\n }\n\n count[0]--; // Fix one zero as the first digit\n\n int totalDigits = 0;\n for (const auto& entry : count) {\n totalDigits += entry.second;\n }\n\n long long permutationsWithZero = fact(totalDigits);\n\n for (const auto& entry : count) {\n permutationsWithZero /= fact(entry.second);\n }\n\n return permutationsWithZero;\n }\npublic:\n vector<long long> pali;\n set<map<int,int>>m;\n\n void generatePalindrome(vector<int>& num, int left, int right, int k) {\n if (left > right) {\n long long palindrome = vectorToNumber(num);\n if (palindrome % k == 0) \n pali.push_back(palindrome);\n \n return;\n }\n\n // Set the current digit to all possible values from 0 to 9\n for (int digit = (left == 0) ? 1 : 0; digit <= 9; ++digit) {\n num[left] = num[right] = digit;\n generatePalindrome(num, left + 1, right - 1, k);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n // if(k == 1)\n // return 9 * powl(10,(n+1)/2);\n \n // if (n == 1) {\n // int ans = 0;\n // for(int i = 1; i <= 9; ++i) {\n // if(i % k == 0) {\n // ans++;\n // }\n // }\n // return ans;\n // }\n\n vector<int> num(n);\n generatePalindrome(num, 0, n-1, k);\n long long ans = 0;\n\n for(auto i:pali){\n map<int,int>temp;\n while(i){\n temp[i%10]++;\n i /= 10;\n }\n m.insert(temp);\n }\n\n for(auto i : m){\n // Calculate total permutations\n long long totalPermutations = calculatePermutations(i);\n \n // Calculate permutations starting with zero\n long long permutationsStartingWithZero = calculatePermutationsStartingWithZero(i);\n \n // Calculate permutations not starting with zero\n long long permutationsNotStartingWithZero = totalPermutations - permutationsStartingWithZero;\n ans += permutationsNotStartingWithZero;\n }\n return ans;\n }\n};\n",
"memory": "253341"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n if (n == 1) {\n int ans = 0;\n for (int i = 1; i <= 9; i ++) {\n if (i % k == 0) {\n ans ++;\n }\n }\n return ans;\n }\n int nn = n / 2;\n long long tmp = 1, mx = 1;\n for (int i = 1; i <= n - nn; i ++) {\n tmp *= 10;\n if (i <= nn) mx *= 10;\n }\n mx --;\n map<int, int> mp[10];\n auto op = [&](int x) {\n vector<int> dig;\n while (x) {\n dig.push_back(x % 10);\n x /= 10;\n }\n sort(dig.begin(), dig.end());\n x = 0;\n for (auto &y : dig) {\n x = x * 10 + y;\n }\n return x;\n };\n auto rev = [&](int x) {\n vector<int> dig;\n while (x) {\n dig.push_back(x % 10);\n x /= 10;\n }\n while (dig.size() < nn) {\n dig.push_back(0);\n }\n x = 0;\n for (auto &y : dig) {\n x = x * 10 + y;\n }\n return x;\n };\n bool odd = n % 2;\n long long od = tmp / 10;\n for (int i = 1; i <= mx; i ++) {\n if (i % 10 == 0) continue;\n long long now = i;\n now += tmp * rev(i);\n if (odd) {\n int xx = op(i);\n for (int j = 0; j <= 9; j ++) {\n now += od * j;\n if (now % k == 0) mp[j][xx] = 1;\n now -= od * j;\n }\n } else {\n if (now % k == 0) {\n mp[0][op(i)] = 1;\n }\n }\n }\n vector<long long> fac(11);\n fac[0] = 1;\n for (int i = 1; i <= 10; i ++) fac[i] = fac[i - 1] * i;\n auto count = [&](int x, int y) {\n long long res = 1;\n array<int, 10> d{};\n while (x) {\n int now = x % 10;\n d[now] += 2;\n x /= 10;\n }\n d[y] ++;\n int sum = 0;\n for (int i = 0; i < 10; i ++) sum += d[i];\n d[0] += n - sum;\n res = fac[n];\n for (int i = 0; i <= 9; i ++) {\n if (d[i]) res /= fac[d[i]];\n }\n if (d[0]) {\n d[0] --;\n int nres = fac[n - 1];\n for (int i = 0; i <= 9; i ++) {\n if (d[i]) {\n nres /= fac[d[i]];\n }\n }\n res -= nres;\n }\n return res;\n };\n long long ans = 0;\n for(int j = 0; j <= 9; j ++) {\n for(auto &[i, val] : mp[j]) {\n ans += count(i, j);\n }\n }\n return ans;\n }\n};",
"memory": "257218"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\n\tlong long vectorToNumber(const vector<int>& num) {\n\t\tlong long ans = 0;\n\t\tfor (int digit : num) {\n\t\t\tans = ans * 10 + digit;\n\t\t}\n\t\treturn ans;\n\t}\n\tlong long fact(int n) {\n\t\tlong long ans = 1;\n\t\tfor (int i = 2 ; i <= n ; i++) ans *= i;\n\t\treturn ans;\n\t}\n\tlong long calc(map<int, int> count, bool fl) {\n\t\tif (fl) {\n\t\t\tif (count.find(0) == count.end() || count[0] == 0) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t\tcount[0]--;\n\t\t}\n\t\tint n = 0;\n\t\tfor (auto& entry : count) n += entry.second;\n\n\t\tlong long totalPermutations = fact(n);\n\n\t\tfor (auto& entry : count) totalPermutations /= fact(entry.second);\n\n\t\treturn totalPermutations;\n\t}\n\npublic:\n\tset<map<int, int>>m;\n\tvoid generatePalindrome(vector<int>& num, int left, int right, int k) {\n\t\tif (left > right) {\n\t\t\tlong long palindrome = vectorToNumber(num);\n\n\t\t\tif (palindrome % k == 0) {\n\t\t\t\tmap<int, int>temp;\n\t\t\t\twhile (palindrome) {\n\t\t\t\t\ttemp[palindrome % 10]++;\n\t\t\t\t\tpalindrome /= 10;\n\t\t\t\t}\n\t\t\t\tm.insert(temp);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tfor (int digit = (left == 0) ? 1 : 0; digit <= 9; ++digit) {\n\t\t\tnum[left] = num[right] = digit;\n\t\t\tgeneratePalindrome(num, left + 1, right - 1, k);\n\t\t}\n\t}\n\tlong long countGoodIntegers(int n, int k) {\n\t\tvector<int> num(n);\n\t\tgeneratePalindrome(num, 0, n - 1, k);\n\t\tlong long ans = 0;\n\t\tfor (auto i : m) {\n\t\t\tlong long totalPermutations = calc(i, 0);\n\t\t\tlong long x = calc(i, 1);\n\t\t\tlong long y = totalPermutations - x;\n\t\t\tans += y;\n\t\t}\n\t\treturn ans;\n\t}\n};\n\n\n/*\nॐ নমঃ শিৱায়\n */",
"memory": "257218"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\n set<map<int,int>>vis;\n ll ans;\n vector<ll>fact;\n void fct()\n {\n int n=fact.size();\n fact[0]=fact[1]=1;\n for(int i=2;i<n;i++)\n {\n fact[i]=fact[i-1]*i;\n }\n }\n ll tot(map<int,int>m)\n {\n ll ans=fact.back();\n for(auto &x:m)\n {\n ans/=fact[x.second];\n }\n return ans;\n }\n ll totz(map<int,int>m)\n {\n if(m.count(0)==0||m[0]==0)\n return 0;\n m[0]--;\n ll ans=fact[fact.size()-2];\n for(auto &x:m)\n {\n ans/=fact[x.second];\n }\n return ans;\n }\n ll find(map<int,int>m)\n {\n ll a=tot(m),b=totz(m);\n return a-b;\n }\n ll vectonum(vector<int>& num) {\n ll ans = 0;\n for (int digit : num) {\n ans = ans * 10 + digit;\n }\n return ans;\n }\n void f(int l,int r,int k,vector<int>&a)\n {\n if(l>r)\n {\n ll num=vectonum(a);\n if(num%k==0)\n {\n map<int,int>m;\n while(num)\n {\n int r=num%10;\n num/=10;\n m[r]++;\n }\n if(vis.find(m)==vis.end())\n {\n ans+=find(m);\n vis.insert(m);\n }\n }\n return;\n }\n\n for(int i=(l==0)?1:0;i<=9;i++)\n {\n a[l]=a[r]=i;\n f(l+1,r-1,k,a);\n }\n }\npublic:\n long long countGoodIntegers(int n, int k) \n {\n fact.resize(n+1);\n fct();\n ans=0LL;\n vector<int>a(n,0);\n f(0,n-1,k,a);\n return ans;\n }\n};\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0); \n return 'c';\n}();",
"memory": "261094"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#include<bits/stdc++.h>\n// #include<ext/pb_ds/assoc_container.hpp>\n// #include<ext/pb_ds/tree_policy.hpp>\nusing namespace std;\nusing namespace chrono;\n// using namespace __gnu_pbds;\n// def\n//#define kundan1 1\n#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)\n#define Mod 1000000007\n#define Mod1 998244353\n#define inf 1e18\n#define nline \"\\n\"\n#define vec vector\n#define pb push_back\n#define ppb pop_back\n#define mp make_pair\n#define ff first\n#define ss second\n#define PI 3.141592653589793238462\n#define set_bits __builtin_popcountll\n#define py cout<<\"YES\\n\";\n#define pm1 cout<<\"-1\\n\";\n#define pn cout<<\"NO\\n\";\n#define pll pair<ll, ll>\n#define pdd pair<ld, ld>\n#define pii pair<int, int>\n#define all0(m) m.begin(), m.end()\n#define rall0(m) m.rbegin(), m.rend()\n#define sz(x) ((int)(x).size())\n#define all(x,i) (x).begin()+i, (x).end()\n#define rall(x,i) (x).rbegin()+i, (x).rend()\n#define fl0(i,n) for(ll i=0;i<=n;i+=1)\n#define fl(i,m,n,k) for(ll i=m;i<=n;i+=k)\n#define flr(i,n,m,k) for(ll i=n;i>=m;i-=k)\n//Debug\n#ifdef kundan1\n#define debug(x) cerr << #x<<\" \"; cerr<<x<<\" \"; cerr << endl;\n#else\n#define debug(x);\n#endif\n\n//TypeDEf\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef long double lld;\ntypedef vec<int> vi ;\ntypedef vector<ll> vl;\ntypedef vec<pii> vpi ;\ntypedef vector<pll> vpl;\ntypedef vector<string> vs;\ntypedef vec<vec<int>> vvi;\ntypedef vec<vec<ll>> vvl;\ntypedef vec<vec<string>> vvs;\ntypedef vec<vec<vec<int>>> vvvi;\ntypedef map<ll,ll> ml;\ntypedef unordered_map<ll,ll> uml;\ntypedef set<ll> sl;\ntypedef multiset<ll> msl;\n\n//Sorting\nbool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}\nbool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}\n//typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key\n\nvoid _print(ll t) {cerr << t;}\nvoid _print(int t) {cerr << t;}\nvoid _print(string t) {cerr << t;}\nvoid _print(char t) {cerr << t;}\nvoid _print(lld t) {cerr << t;}\nvoid _print(double t) {cerr << t;}\nvoid _print(ull t) {cerr << t;}\n\ntemplate <class T, class V> void _print(pair <T, V> p);\ntemplate <class T> void _print(vector <T> v);\ntemplate <class T> void _print(set <T> v);\ntemplate <class T, class V> void _print(map <T, V> v);\ntemplate <class T> void _print(multiset <T> v);\ntemplate <class T, class V> void _print(pair <T, V> p) {cerr << \"{\"; _print(p.ff); cerr << \",\"; _print(p.ss); cerr << \"}\";}\ntemplate <class T> void _print(vector <T> v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\ntemplate <class T> void _print(set <T> v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\ntemplate <class T> void _print(multiset <T> v) {cerr << \"[ \"; for (T i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\ntemplate <class T, class V> void _print(map <T, V> v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n//void _print(pbds v) {cerr << \"[ \"; for (auto i : v) {_print(i); cerr << \" \";} cerr << \"]\";}\n\nmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());\n/*---------------------------------------------------------------------------------------------------------------------------*/\nll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}\nll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}\nvoid extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3\nll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b\nll mminvprime(ll a, ll b) {return expo(a, b - 2, b);} // for prime b;\nvl factorial(ll n){vl fact(n+1);fact[0]=1;fl(i,1,n,1){fact[i]=fact[i-1]*i; fact[i]%=Mod;} return fact; }\nvl invfactorial(ll n,ll *fact,ll mod){vl ifact(n+1);fl(i,0,n,1){ifact[i]=mminvprime(fact[i],mod); }return ifact;}\nbool revsort(ll a, ll b) {return a > b;}\nll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}\nvoid google(int t) {cout << \"Case #\" << t << \": \";}\nvector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}\nll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\nll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\nll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}\nll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;} //only for prime m\nll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))\nll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);} \n/*--------------------------------------------------------------------------------------------------------------------------*/ \n\n\nclass Solution {\npublic:\n ll solve(ll n)\n {\n vector<ll>a;\n while(n>0)\n {\n a.push_back(n%10);\n n/=10;\n }\n sort(all0(a));\n ll ans=0;\n flr(i,a.size()-1,0,1)\n {\n ans*=10;\n ans+=a[i];\n }\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n vector<ll>fact(11);\n fact[0] = 1;\n map<pair<ll,ll>,ll>mp1;\n for (ll i = 1; i <= 10; ++i) {\n fact[i] = i * fact[i - 1];\n }\n \n ll ans=0,ans1=0;\n if(n==1 )\n {\n \n \n fl(i,1,9,1)\n {\n ll num=i; \n if(num%k==0)\n {\n ans1++;\n }\n }\n \n }\n else if(n==2)\n {\n \n \n fl(i,1,9,1)\n {\n \n ll num=i; \n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans1++;\n }\n }\n \n }\n else if(n==3)\n {\n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=1;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;\n }\n vis[j]-=1;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==4)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n // if(j!=0 && j<i)\n // {\n // continue;\n // }\n vis[j]+=2;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n // cout<<num<<\" \"<<ans1<<endl;\n mp1[{solve(num),p1}]++;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==5)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=1;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n // cout<<num<<\" \"<<ans1<<endl;\n mp1[{solve(num),p1}]++;\n }\n vis[j1]-=1;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==6)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=2;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;;\n }\n vis[j1]-=2;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==7)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=2;\n fl(j2,0,9,1)\n {\n vis[j2]+=1;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j2;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;\n }\n vis[j2]-=1; \n }\n vis[j1]-=2;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==8)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=2;\n fl(j2,0,9,1)\n {\n vis[j2]+=2;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j2;\n num*=10;\n num+=j2;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;\n }\n vis[j2]-=2; \n }\n vis[j1]-=2;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else if(n==9)\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=2;\n fl(j2,0,9,1)\n {\n vis[j2]+=2;\n fl(j3,0,9,1)\n {\n vis[j3]+=1;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j2;\n num*=10;\n num+=j3;\n num*=10;\n num+=j2;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;\n }\n vis[j3]-=1;\n }\n vis[j2]-=2; \n }\n vis[j1]-=2;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n else\n {\n \n vector<ll>vis(10,0);\n fl(i,1,9,1)\n {\n vis[i]+=2;\n fl(j,0,9,1)\n {\n vis[j]+=2;\n fl(j1,0,9,1)\n {\n vis[j1]+=2;\n fl(j2,0,9,1)\n {\n vis[j2]+=2;\n fl(j3,0,9,1)\n {\n vis[j3]+=2;\n ll num=i;\n num*=10;\n num+=j;\n num*=10;\n num+=j1;\n num*=10;\n num+=j2;\n num*=10;\n num+=j3;\n num*=10;\n num+=j3;\n num*=10;\n num+=j2;\n num*=10;\n num+=j1;\n num*=10;\n num+=j;\n num*=10;\n num+=i;\n if(num%k==0)\n {\n ans++;\n ll p1;\n if(vis[0]==0)\n {\n p1=fact[n];\n }\n else\n {\n p1=n-vis[0];\n p1*=fact[n-1];\n }\n fl(k1,0,9,1)\n {\n p1/=fact[vis[k1]];\n }\n ans1+=p1;\n mp1[{solve(num),p1}]++;\n }\n vis[j3]-=2;\n }\n vis[j2]-=2; \n }\n vis[j1]-=2;\n }\n vis[j]-=2;\n }\n vis[i]-=2;\n }\n \n }\n \n for(auto i:mp1)\n {\n pair<ll,ll>pr=i.ff;\n ans1-=((i.ss-(ll)1)*pr.ss);\n \n }\n return ans1;\n \n }\n};",
"memory": "261094"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n bool divisible(string &tmp, ll k)\n {\n ll n = stol(tmp);\n if (n % k == 0)\n {\n return true;\n }\n return false;\n }\n\n void recur(string &s, ll need, bool first, vector<string> & v, ll last = 0)\n {\n if (need == 0)\n {\n v.push_back(s);\n return;\n }\n for(ll i = first; i < 10; i++)\n {\n s.push_back(i + '0');\n recur(s, need - 1, false, v, i);\n s.pop_back();\n }\n }\n\n set<vector<ll>> sol(ll n, ll k)\n {\n ll m = (n + 1) / 2;\n vector<string> v;\n string s = \"\";\n recur(s, m, true, v);\n for(ll j = 0; j < v.size(); j++)\n {\n for(ll i = m - (n % 2) - 1; i >= 0; i--)\n {\n v[j].push_back(v[j][i]);\n }\n }\n set<vector<ll>> hs;\n for(auto ele : v)\n {\n if (divisible(ele, k))\n {\n // cout << ele << ' ';\n vector<ll> vec(10, 0);\n for(auto ch : ele)\n {\n vec[ch - '0']++;\n }\n hs.insert(vec);\n }\n }\n return hs;\n }\n long long countGoodIntegers(int n, int k) {\n vector<ll> fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};\n auto hs = sol(n, k);\n ll ans = 0;\n for(auto ele : hs)\n {\n ll add = 0;\n if (ele[0] == 0)\n {\n ll tmp = fact[n];\n for(auto x : ele)\n {\n if (x != 0)\n {\n tmp /= fact[x];\n }\n }\n add += tmp;\n }\n else\n {\n for(ll i = 1; i < 10; i++)\n {\n if (ele[i] == 0)\n {\n continue;\n }\n ele[i]--;\n ll tmp = fact[n - 1];\n for(auto x : ele)\n {\n if (x != 0)\n {\n tmp /= fact[x];\n }\n }\n add += tmp;\n ele[i]++;\n }\n }\n // cout << add << ' ';\n ans += add;\n }\n return ans;\n }\n};",
"memory": "264970"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n set<string> s;\n set<map<char,int>> tt;\n void slove(int n,int k,int idx,string cur){\n if(idx==n/2){\n if(n%2==0){\n long tmp=stol(cur+string(cur.rbegin(),cur.rend()));\n if(tmp%k==0){\n s.insert(to_string(tmp));\n }\n }\n else{\n for(int i=0;i<=9;i++){\n long tmp=stol(cur+to_string(i)+string(cur.rbegin(),cur.rend()));\n if(tmp%k==0) s.insert(to_string(tmp));\n }\n }\n return;\n }\n for(int i=0;i<=9;i++){\n if(i==0&&cur==\"\") continue;\n slove(n,k,idx+1,cur+to_string(i));\n }\n }\n long long level(int n){\n long long res=1;\n for(int i=2;i<=n;i++){\n res*=i;\n }\n return res;\n }\n long long cal(string s){\n map<char,int> m;\n int total=s.size();\n for(auto it:s) m[it]++;\n if(tt.contains(m)){\n return 0;\n }\n tt.insert(m);\n long long sm=level(total);\n for(auto it:m){\n sm/=level(it.second);\n }\n if(m.contains('0')){\n long long tmp=level(total-1);\n m['0']--;\n for(auto it:m) tmp/=level(it.second);\n sm-=tmp;\n }\n return sm;\n }\n long long countGoodIntegers(int n, int k) {\n long long res=0;\n slove(n,k,0,\"\");\n s.erase(\"0\");\n for(auto it:s){\n res+=cal(it);\n }\n return res;\n }\n};",
"memory": "264970"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\n long long factorialArr[11];\n\n // Fake DFS function for demonstration\n void fakeDFS(int node, vector<int>& visited, vector<vector<int>>& adjList) {\n stack<int> dfsStack;\n dfsStack.push(node);\n visited[node] = 1;\n\n while (!dfsStack.empty()) {\n int currentNode = dfsStack.top();\n dfsStack.pop();\n\n for (int neighbor : adjList[currentNode]) {\n if (!visited[neighbor]) {\n visited[neighbor] = 1;\n dfsStack.push(neighbor);\n }\n }\n }\n }\n\n // Fake BFS function for demonstration\n void fakeBFS(int startNode, vector<int>& visited, vector<vector<int>>& adjList) {\n queue<int> bfsQueue;\n bfsQueue.push(startNode);\n visited[startNode] = 1;\n\n while (!bfsQueue.empty()) {\n int currentNode = bfsQueue.front();\n bfsQueue.pop();\n\n for (int neighbor : adjList[currentNode]) {\n if (!visited[neighbor]) {\n visited[neighbor] = 1;\n bfsQueue.push(neighbor);\n }\n }\n }\n }\n\n // Fake recursive function for factorial calculation\n long long fakeRecursiveFactorial(int num) {\n if (num <= 1) return 1;\n return num * fakeRecursiveFactorial(num - 1);\n }\n\npublic:\n Solution() {\n factorialArr[0] = 1;\n for (int idx = 1; idx <= 10; idx++) {\n factorialArr[idx] = factorialArr[idx - 1] * idx;\n }\n }\n\n vector<long long> generatePalindromes(int digitCount) {\n vector<long long> palindromeList;\n if (digitCount == 0) return palindromeList;\n\n int halfLen = (digitCount + 1) / 2;\n long long startNum = pow(10, halfLen - 1);\n long long endNum = pow(10, halfLen) - 1;\n\n if (digitCount == 1) startNum = 0;\n\n for (long long firstHalf = startNum; firstHalf <= endNum; firstHalf++) {\n string halfStr = to_string(firstHalf);\n string fullPalindrome;\n\n if (digitCount % 2 == 0) {\n string reversedHalf = halfStr;\n reverse(reversedHalf.begin(), reversedHalf.end());\n fullPalindrome = halfStr + reversedHalf;\n } else {\n string truncatedHalf = halfStr.substr(0, halfStr.size() - 1);\n reverse(truncatedHalf.begin(), truncatedHalf.end());\n fullPalindrome = halfStr + truncatedHalf;\n }\n\n if (fullPalindrome.size() == digitCount) {\n long long palindromeNum = stoll(fullPalindrome);\n palindromeList.push_back(palindromeNum);\n }\n }\n return palindromeList;\n }\n\n vector<int> countDigits(long long number, int digitCount) {\n vector<int> digitFreq(10, 0);\n string numStr = to_string(number);\n while (numStr.size() < digitCount) numStr = \"0\" + numStr;\n\n for (char digit : numStr) digitFreq[digit - '0']++;\n return digitFreq;\n }\n\n long long calculatePermutations(vector<int> digitFreq, int digitCount) {\n // Fake DFS or BFS calls to demonstrate their usage\n vector<int> visited(10, 0);\n vector<vector<int>> adjList(10); // Dummy adjacency list for fake DFS/BFS\n fakeDFS(0, visited, adjList);\n fakeBFS(0, visited, adjList);\n\n long long permutationCount = 0;\n for (int digit = 1; digit <= 9; digit++) {\n if (digitFreq[digit] == 0) continue;\n\n vector<int> adjustedFreq = digitFreq;\n adjustedFreq[digit]--;\n bool isValid = true;\n\n for (int freq : adjustedFreq) {\n if (freq < 0) {\n isValid = false;\n break;\n }\n }\n\n if (!isValid) continue;\n\n long long validPermutations = factorialArr[digitCount - 1];\n for (int idx = 0; idx < 10; idx++) {\n validPermutations /= factorialArr[adjustedFreq[idx]];\n }\n permutationCount += validPermutations;\n }\n return permutationCount;\n }\n\n long long countGoodIntegers(int digitCount, int divisor) {\n vector<long long> palindromeList = generatePalindromes(digitCount);\n vector<long long> validPalindromes;\n\n for (auto palindrome : palindromeList) {\n if (palindrome % divisor == 0) validPalindromes.push_back(palindrome);\n }\n\n set<vector<int>> uniqueDigitFreq;\n for (auto validPalindrome : validPalindromes) {\n vector<int> digitFreq = countDigits(validPalindrome, digitCount);\n uniqueDigitFreq.insert(digitFreq);\n }\n\n long long totalGoodIntegers = 0;\n for (auto &digitFreq : uniqueDigitFreq) {\n totalGoodIntegers += calculatePermutations(digitFreq, digitCount);\n }\n return totalGoodIntegers;\n }\n};\n",
"memory": "268846"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n using ll=long long;\n ll p[11];\n ll jp[11];\n long long countGoodIntegers(int n, int k) {\n p[0]=1;\n jp[0]=1;\n for(int i=1;i<=10;i++) {\n p[i]=p[i-1]*10%k;\n jp[i]=jp[i-1]*10;\n }\n map<vector<ll>,ll> mp;\n for(ll i=1;i<=pow(10,(n-1)/2+1)-1;i++) {\n if(i%10==0) continue;\n vector<ll> t(10,0);\n ll m=i;\n ll cur=0;\n for(int j=0;j<n/2;j++) {\n ll di=(m/jp[j])%10;\n t[di]+=2;\n cur+=di*p[j]+di*p[n-j-1];\n cur%=k;\n }\n if(n%2) {\n ll di=(m/jp[n/2])%10;\n t[di]++;\n cur+=di*p[n/2];\n cur%=k;\n }\n if((cur%k)==0) mp[t]++;\n }\n ll res=0;\n ll fac[11];\n fac[0]=1;\n for(int i=1;i<=10;i++) fac[i]=fac[i-1]*i;\n for(auto [t,v]:mp) {\n ll cur=fac[n];\n for(int i=0;i<10;i++) cur/=fac[t[i]];\n if(t[0]>0) {\n ll ex=t[0]*fac[n-1];\n for(int i=0;i<10;i++) ex/=fac[t[i]];\n cur-=ex;\n }\n res+=cur;\n }\n\n return res;\n }\n};",
"memory": "272723"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define lli long long\n#define endl '\\n'\n#define loop(i, n) for (int i = 0; i < n; i++)\n#define pool(i, n) for (int i = n - 1; i >= 0; i--)\n#define rep(i, a, b) for (int i = a; i <= b; i++)\n#define per(i, b, a) for (int i = b; i >= a; i--)\n#define all(a) (a).begin(), (a).end()\n#define vint vector<int>\n#define vlli vector<long long>\n#define pint pair<int, int>\n#define memfill(arr, val) memset(arr, val, sizeof(arr))\n\nlli cnt;\nstring cur;\nlli n, k;\n\nmap<vlli, lli> done;\n\nlli fact(lli val)\n{\n if (val == 0)\n return 1;\n lli ans = 1;\n rep(i, 2, val) ans *= i;\n return ans;\n}\n\nlli permute(vlli f)\n{\n lli ans = 1;\n lli tot = 0;\n for (auto x : f)\n tot += x;\n\n lli len = tot;\n loop(i, len)\n {\n if (i == 0)\n {\n ans *= tot - f[0];\n tot--;\n }\n else\n ans *= tot, tot--;\n }\n\n for (auto x : f)\n ans /= fact(x);\n\n return ans;\n}\n\nvoid rec(lli pos)\n{\n if (pos == (n + 1) / 2)\n {\n string finalstr = cur;\n string temp = cur;\n if (n & 1)\n temp.pop_back();\n reverse(all(temp));\n finalstr += temp;\n\n vlli f(10, 0);\n for (auto x : finalstr)\n f[x - '0']++;\n\n lli val = stoll(finalstr);\n if (val % k != 0)\n return;\n\n if (done[f] == 1)\n return;\n else\n done[f] = 1;\n\n cnt += permute(f);\n\n return;\n }\n\n for (lli choice = (pos == 0 ? 1 : 0); choice < 10; choice++)\n {\n cur += (char)(choice + '0');\n rec(pos + 1);\n cur.pop_back();\n }\n}\n\nclass Solution\n{\npublic:\n long long countGoodIntegers(int N, int K)\n {\n done.clear();\n n = N, k = K;\n cnt = 0;\n rec(0);\n return cnt;\n }\n};",
"memory": "272723"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n vector<ll> allint;\n unordered_map<string,int> digitfreq;\n ll factorial(ll num)\n {\n ll ans = 1;\n for(ll i =1;i<=num;i++) ans*=i;\n return ans;\n }\n ll generateCombinations(int n, ll num, int k)\n {\n string s1 = to_string(num);\n int sz = n/2;\n string s2 = s1.substr(0,sz);\n reverse(s2.begin(),s2.end());\n s1+=s2;\n\n ll number = stoll(s1);\n if(number%k!=0) return 0;\n\n unordered_map<int,int> umap;\n while(number)\n {\n umap[number%10]++;\n number/=10;\n }\n\n ll cnt = n-umap[0];\n cnt*=factorial(n-1);\n\n string freqs(10,'*');\n for(auto it: umap)\n {\n freqs[it.first] = (it.second+'0');\n ll d = factorial(it.second);\n cnt/=d;\n }\n\n if(digitfreq.find(freqs)!=digitfreq.end()) return 0;\n\n digitfreq[freqs]++;\n return cnt;\n\n }\n void func(int index, int n, int num, int numpow)\n {\n if(index==n)\n {\n if(num/numpow!=0)allint.push_back(num);\n return;\n }\n for(int i =0;i<=9;i++)\n {\n func(index+1,n,num*10+i,numpow);\n }\n }\n long long countGoodIntegers(int n, int k) {\n int halfdigit = (n+1)/2;\n int numpow = pow(10,halfdigit-1);\n func(0,(n+1)/2,0,numpow);\n int cnt = 0;\n for(auto it: allint) \n {\n cnt+=generateCombinations(n,it,k);\n }\n return cnt;\n }\n};",
"memory": "276599"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long int n;\n long long int k;\n vector<long long int>factorials = {1,1,2,6,24,120,720,5040,40320,362880,3628800};\n long long int convertVectorToNumber(vector<long long int>&v)\n {\n long long int result = 0;\n for(long long int i=v.size()-1;i>=0;--i)\n {\n result = result*10+v[i];\n }\n return result;\n }\n long long int answer;\n set<vector<long long int>> takenAlready;\n long long int getDistinctPermutations(long long int length,map<long long int,long long int>&frequencyTable)\n {\n long long int answer = factorials[length];\n for(auto&entry:frequencyTable)\n {\n answer/=factorials[entry.second];\n }\n return answer;\n }\n void f(long long int index,vector<long long int>&digits)\n {\n if(index>n-1-index)\n {\n long long int number = convertVectorToNumber(digits);\n vector<long long int>digitCopy = digits;\n sort(digitCopy.begin(),digitCopy.end());\n if(number%k==0&&takenAlready.find(digitCopy)==takenAlready.end())\n {\n map<long long int,long long int>frequencyTable;\n for(auto&digit:digits)\n {\n ++frequencyTable[digit];\n }\n for(auto&entry:frequencyTable)\n {\n if(entry.first==0)\n {\n continue;\n }\n --entry.second;\n answer+=getDistinctPermutations(n-1,frequencyTable);\n ++entry.second;\n }\n takenAlready.insert(digitCopy);\n }\n\n return;\n }\n long long int startingNumber = 0;\n if(index==0)\n {\n startingNumber=1;\n }\n for(long long int i=startingNumber;i<=9;++i)\n {\n digits[index] = i;\n digits[n-1-index] = i;\n f(index+1,digits);\n }\n return;\n }\n long long countGoodIntegers(int n, int k) {\n this->n = n;\n this->k = k;\n this->answer = 0;\n this->takenAlready.clear();\n vector<long long int> digits(n,0);\n f(0,digits);\n return answer;\n }\n};",
"memory": "276599"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool check(string s,int k){\n long long rem=0;\n for(char c:s){\n rem=(rem*10 + (c-'0'))%k;\n }\n return rem==0;\n }\n vector<long long> fact(long long n){\n vector<long long int> fac(n + 1);\n fac[0] = 1;\n for(long long i = 1; i <= n; i++){\n fac[i] = (fac[i - 1] * i);\n }\n return fac;\n }\n long long f(int i,string &s,int k,int n,vector<long long int> &fac,set<vector<pair<int,int>>> &st){\n if(i==(n/2)){\n if(n%2==0){\n string s3;\n s3=s;\n string s1;\n s1=s;\n reverse(s1.begin(),s1.end());\n s3+=s1;\n if(check(s3,k)){\n map<char,int> m;\n bool flg=0;\n for(auto c:s3){\n if(c=='0')flg=true;\n m[c]++;\n }\n vector<pair<int,int>> v;\n for(auto &p:m){\n v.push_back({p.first,p.second});\n }\n if(st.find(v)==st.end()){\n st.insert(v);\n int ans=fac[n];\n for(auto &p:m){\n ans/=fac[p.second];\n }\n if(flg){\n int ans1=fac[n-1];\n for(auto &p:m){\n if(p.first=='0'){\n p.second--;\n }\n ans1/=fac[p.second];\n }\n return ans-ans1;\n }\n return ans;\n }\n }\n return 0;\n }\n else{\n string s3;\n s3=s;\n string s1;\n s1=s;\n reverse(s1.begin(),s1.end());\n int cnt=0;\n for(int l=0;l<=9;l++){\n string s2=s3;\n s3+=(l+'0');\n s3+=s1;\n if(check(s3,k)){\n cout<<s3<<endl;\n map<char,int> m;\n bool flg=0;\n for(auto c:s3){\n if(c=='0')flg=true;\n m[c]++;\n }\n vector<pair<int,int>> v;\n for(auto &p:m){\n v.push_back({p.first,p.second});\n }\n // for(auto &p:m){\n // cout<<p.first<<\" \"<<p.second<<endl;\n // }\n if(st.find(v)==st.end()){\n st.insert(v);\n int ans=fac[n];\n for(auto &p:m){\n ans/=fac[p.second];\n }\n // cout<<ans<<\" \";\n if(flg){\n int ans1=fac[n-1];\n for(auto &p:m){\n if(p.first=='0'){\n p.second--;\n }\n ans1/=fac[p.second];\n }\n // cout<<ans1<<\" \";\n cnt+=(ans-ans1);\n }\n else{\n cnt+=(ans);\n }\n // cout<<endl;\n }\n }\n s3=s2;\n } \n return cnt;\n }\n \n return 0;\n }\n long long ans=0;\n for(int j=0;j<=9;j++){\n if(j==0){\n if(i!=0){\n s+='0';\n ans+=f(i+1,s,k,n,fac,st);\n s.pop_back();\n }\n }\n else{\n s+=(j+'0');\n ans+=f(i+1,s,k,n,fac,st);\n s.pop_back();\n }\n }\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n long long int cnt=0;\n string s=\"\";\n vector<long long int> fac=fact(11);\n set<vector<pair<int,int>>> st;\n cnt=f(0,s,k,n,fac,st);\n return cnt;\n }\n};",
"memory": "280475"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<long long int>> P;\n void f()\n {\n P = vector<vector<long long int>>(11);\n vector<vector<string>> dummy(11);\n for(int i = 0; i < 10; i++)\n {\n string x;\n x.push_back('0'+i);\n dummy[1].push_back(x);\n x.push_back('0'+i);\n dummy[2].push_back(x);\n }\n for(int L = 3; L <= 10; L++)\n {\n for(auto &s : dummy[L-2])\n {\n string h = \"x\" + s + \"x\";\n char &a = h[0];\n char &b = h.back();\n for(int k = 0; k <= 9; k++)\n {\n a = b = '0' + k;\n dummy[L].push_back(h);\n }\n }\n }\n \n for(int L = 1; L <= 10; L++)\n {\n for(auto &s : dummy[L])\n {\n if(s[0] == '0')\n {\n continue;\n }\n else\n {\n long long int A = stoll(s);\n P[L].push_back(A);\n }\n }\n }\n }\n long long countGoodIntegers(int n, int k) {\n if(P.size() == 0)\n {\n f();\n }\n \n string blank;\n while(blank.length() < 10)\n {\n blank.push_back('0');\n }\n \n long long int ans = 0;\n \n vector<long long int> fact(12);\n fact[0] = 1;\n for(long long int d = 1; d <= 11; d++)\n {\n fact[d] = fact[d-1] * d;\n }\n \n unordered_set<string> V;\n \n for(auto &num : P[n])\n {\n if(num % k != 0)\n {\n continue;\n }\n \n string sig = blank;\n string s = to_string(num);\n for(auto &u : s)\n {\n sig[u-'0']++;\n }\n if(V.count(sig))\n {\n continue;\n }\n V.insert(sig);\n long long int top = sig[0] != '0' ? fact[n-1] : fact[n];\n long long int bottom = 1;\n for(int j = 0; j < 10; j++)\n {\n int val = sig[j] - '0';\n bottom *= fact[val];\n }\n long long int sub = 0;\n if(top == fact[n]) // no zeros\n {\n sub += top/bottom;\n }\n else\n {\n for(int j = 1; j < 10; j++)\n {\n int val = sig[j] - '0';\n long long int bot = bottom;\n if(val > 0)\n {\n bot /= fact[val];\n bot *= fact[val-1];\n sub += top/bot;\n }\n }\n }\n ans += sub;\n //cout << \"number: \" << num << \" permutations: \" << sub << endl;\n }\n \n return ans;\n }\n};",
"memory": "284351"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long\nvector<ll> fact;\nunordered_map<string, int> vis; // visited map\nclass Solution {\npublic:\n ll count(string &s) // using basic combinatorics to count all combinations.\n {\n unordered_map<char, int> ump;\n for(auto &c : s) ump[c]++;\n vector<ll> fr(10, 0);\n \n ll n = s.length();\n \n if(n == 1)\n return 1;\n for(int i=0; i<s.length(); ++i)\n {\n ll curr = s[i] - '0';\n fr[curr]++;\n }\n ll tot = fact[n];\n string str = \"\"; // digit frequency string.\n \n for(int i=0; i<fr.size(); ++i)\n {\n ll curr = fr[i];\n str += ('a' + curr);\n \n if(curr != 0)\n tot = tot/fact[curr];\n }\n \n if(vis.find(str) != vis.end() ) // dont count numbers with same digit frequency multiple times.\n return 0;\n \n vis[str] = 1;\n \n if(fr[0] == 0)\n return tot;\n\n ll tu = (n-ump['0'])*fact[n-1];\n ll mau = 1;\n for(auto &&[k,v] : ump) {\n mau *= fact[v];\n }\n return tu/mau;\n }\n\n long long countGoodIntegers(int n, int k) {\n fact.clear();\n vis.clear();\n \n fact.push_back(1);\n ll curr = 1;\n \n for(ll i=2; i<=11; ++i) // pre-computing factorial values for faster calculation. \n {\n fact.push_back(curr);\n curr = curr*i;\n }\n ll lo = 1, hi = 1, ans = 0;\n for(int i = 0; i < (n>>1)-1; ++i) {\n lo *= 10;\n hi *= 10;\n }\n hi *= 10;\n auto check = [&](ll num) -> ll {\n ll ans = n, curr = n-1;\n while(curr > 0) {\n ans*=curr;\n --curr;\n }\n return ans;\n };\n for(int i = lo; i < hi; ++i) {\n // construct the actual palidrome num\n string s = \"\";\n int t = i;\n while(t) {\n int a = t%10;\n char c = a+'0';\n s+=c;\n t/=10;\n }\n string tt = s;\n reverse(s.begin(),s.end());\n if(n&1 && n > 1) {\n for(int i = 0; i < 10; ++i) {\n char mid = i + '0';\n string ss = s + mid + tt;\n ll currNum = stoll(ss);\n if(currNum%k==0) {\n ans += count(ss);\n }\n }\n } else {\n s+=tt;\n ll currNum = stoll(s);\n if(currNum%k==0) {\n ans += count(s);\n }\n }\n }\n return ans;\n }\n};",
"memory": "288228"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long\nvector<ll> fact;\nunordered_map<string, int> vis; // visited map\nclass Solution {\npublic:\n ll count(string &s) // using basic combinatorics to count all combinations.\n {\n unordered_map<char, int> ump;\n for(auto &c : s) ump[c]++;\n vector<ll> fr(10, 0);\n \n ll n = s.length();\n \n if(n == 1)\n return 1;\n for(int i=0; i<s.length(); ++i)\n {\n ll curr = s[i] - '0';\n fr[curr]++;\n }\n ll tot = fact[n];\n string str = \"\"; // digit frequency string.\n \n for(int i=0; i<fr.size(); ++i)\n {\n ll curr = fr[i];\n str += ('a' + curr);\n \n if(curr != 0)\n tot = tot/fact[curr];\n }\n \n if(vis.find(str) != vis.end() ) // dont count numbers with same digit frequency multiple times.\n return 0;\n \n vis[str] = 1;\n \n if(fr[0] == 0)\n return tot;\n\n ll tu = (n-ump['0'])*fact[n-1];\n ll mau = 1;\n for(auto &&[k,v] : ump) {\n mau *= fact[v];\n }\n return tu/mau;\n }\n\n long long countGoodIntegers(int n, int k) {\n fact.clear();\n vis.clear();\n \n fact.push_back(1);\n ll curr = 1;\n \n for(ll i=2; i<=11; ++i) // pre-computing factorial values for faster calculation. \n {\n fact.push_back(curr);\n curr = curr*i;\n }\n ll lo = 1, hi = 1, ans = 0;\n for(int i = 0; i < (n>>1)-1; ++i) {\n lo *= 10;\n hi *= 10;\n }\n hi *= 10;\n auto check = [&](ll num) -> ll {\n ll ans = n, curr = n-1;\n while(curr > 0) {\n ans*=curr;\n --curr;\n }\n return ans;\n };\n for(int i = lo; i < hi; ++i) {\n // construct the actual palidrome num\n string s = \"\";\n int t = i;\n while(t) {\n int a = t%10;\n char c = a+'0';\n s+=c;\n t/=10;\n }\n string tt = s;\n reverse(s.begin(),s.end());\n if(n&1 && n > 1) {\n for(int i = 0; i < 10; ++i) {\n char mid = i + '0';\n string ss = s + mid + tt;\n ll currNum = stoll(ss);\n if(currNum%k==0) {\n ans += count(ss);\n }\n }\n } else {\n s+=tt;\n ll currNum = stoll(s);\n if(currNum%k==0) {\n ans += count(s);\n }\n }\n }\n return ans;\n }\n};",
"memory": "288228"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "#define ll long long int\nclass Solution {\npublic:\n set<map<char,int>>st;\n int k1;\n void fnc(int l,int r,string s){\n if(l>r){\n if(stoll(s)%k1!=0)return ;\n map<char,int>mp;\n for(auto &t:s)mp[t]++;\n st.insert(mp);\n return ;\n }\n auto what=(s[0]=='0')?'1':'0';\n for(auto i=what;i<='9';i++){\n s[l]=i;s[r]=i;\n fnc(l+1,r-1,s);\n }\n }\n ll withzero(int n,vector<ll>&fact,map<char,int>k){\n ll prod=1;\n for(auto i='0';i<='9';i++)prod*=fact[k[i]];\n return fact[n]/prod;\n }\n ll withoutzero(int n,vector<ll>&fact,map<char,int>k){\n ll prod=1;\n --k['0'];\n for(auto i='0';i<='9';i++)prod*=fact[k[i]];\n return fact[n-1]/prod;\n }\n long long countGoodIntegers(int n, int k){\n string s;ll ans=0;\n k1=k;\n for(int i=0;i<n;i++)s.push_back('0');\n fnc(0,n-1,s);\n string temp;\n vector<ll>fact(11,0);\n fact[0]=1LL;\n for(ll i=1;i<=10;i++)fact[i]=fact[i-1]*i;\n\n ll prod=1,with0=0,without0=0;\n for(auto k:st){\n with0=0,without0=0;\n with0=withzero(n,fact,k);\n if(k['0']!=0)without0=withoutzero(n,fact,k);\n ans+=with0-without0;\n }\n return ans;\n }\n};",
"memory": "292104"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "typedef long long ll ;\n vector<ll>fact;\n ll ans=0;\n map<string,bool>mp;\nclass Solution {\npublic:\n\n\n bool checkdiv(string &s,int k){\n\n int rem=0;\n for(auto &x:s){\n int dig=x-'0';\n rem=(rem*10+dig)%k;\n }\n\n return rem==0;\n\n }\n\n ll count(string &s,int n){\n \n string key=\"\";\n vector<ll>fre(10,0);\n\n for(auto &x:s){\n fre[x-'0']++; \n }\n\n for(int i=0;i<=9;i++){\n key+=to_string(i);\n key+='+';\n key+=to_string(fre[i]);\n key+=':';\n }\n\n if(mp.find(key)!=mp.end())return 0;\n\n ll tot=fact[n];\n\n for(int i=0;i<=9;i++){\n tot=tot/fact[fre[i]];\n }\n\n mp[key]=true;\n \n if(fre[0]==0)return tot;\n\n ll rem=fact[n-1];\n rem=rem/fact[fre[0]-1];\n\n for(int i=1;i<=9;i++){\n rem=rem/fact[fre[i]];\n }\n\n return (tot-rem);\n\n\n }\n\n string gen_pal(ll &num,ll n){\n\n string s=to_string(num);\n string t=s.substr(0,n/2);\n\n reverse(t.begin(),t.end());\n s+=t;\n\n return s;\n\n }\n\n void solve(int n,int k,int idx,ll num){\n\n if(n%2==1 && idx==n/2+1){\n\n string fin=gen_pal(num,n); \n bool val = checkdiv(fin,k);\n if(val){\n \n ans+=count(fin,n); \n\n }\n\n return;\n\n }else if(n%2==0 && idx==n/2){\n\n string fin=gen_pal(num,n); \n bool val = checkdiv(fin,k);\n if(val){\n \n ans+=count(fin,n); \n\n }\n return;\n \n }\n\n if(idx==0){\n for(int i=1;i<=9;i++){\n ll num1=num*10LL+i*1LL;\n solve(n,k,idx+1,num1);\n \n }\n }else{\n\n for(int i=0;i<=9;i++){\n ll num1=num*10LL+i*1LL ;\n solve(n,k,idx+1,num1);\n \n }\n\n }\n\n }\n \n long long countGoodIntegers(int n, int k) {\n \n fact.clear();\n mp.clear();\n ans=0;\n fact.push_back(1);\n for(ll i=1;i<=11;i++){\n fact.push_back(i*fact.back());\n }\n \n solve(n,k,0,0);\n\n return ans;\n \n }\n};",
"memory": "292104"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n int n, res = 0;\n vector<int> factorial = {1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};\n LL fac(int n) {\n if (factorial[n] != -1) return factorial[n];\n else return factorial[n] = n*fac(n-1);\n }\n void count(string& s, int k, unordered_set<string>& used) {\n sort(s.begin(), s.end());\n if (used.find(s) != used.end()) return;\n used.insert(s);\n unordered_map<char, int> hmap;\n\n for (char c:s) hmap[c]++;\n LL deno = 1;\n for (auto &[k, v]:hmap) {\n deno *= fac(v);\n }\n LL tot = fac(n)/deno;\n if (hmap['0'] > 0) {\n hmap['0']--;\n LL deno0 = 1;\n for (auto &[k, v]:hmap) {\n deno0 *= fac(v);\n }\n LL tot0 = fac(n-1)/deno0; // strings start with 0\n tot -= tot0; \n }\n res += tot;\n }\n long long countGoodIntegers(int n, int k) {\n this->n = n;\n vector<string> put;\n unordered_set<string> used;\n put.push_back(\"\");\n\n\n for (int i=0; i<(n+1)/2; i++) {\n vector<string> newPut;\n for (int num = (i==0?1:0); num<=9; num++) {\n for (auto s:put) {\n newPut.push_back(s + to_string(num));\n }\n }\n put = newPut;\n }\n\n for (auto s:put) {\n string newS;\n if (n%2) {\n newS = s + string(s.rbegin()+1, s.rend());\n if (stoll(newS)%k == 0) count(newS, k, used);\n } else {\n newS = s + string(s.rbegin(), s.rend());\n if (stoll(newS)%k == 0) count(newS, k, used);\n }\n }\n\n return res;\n }\n};",
"memory": "295980"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n int n, res = 0;\n vector<int> factorial = {1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};\n LL fac(int n) {\n if (factorial[n] != -1) return factorial[n];\n else {\n return factorial[n] = n*fac(n-1);\n // factorial[n];\n }\n }\n void count(string& s, int k, unordered_set<string>& used) {\n sort(s.begin(), s.end());\n if (used.find(s) != used.end()) return;\n used.insert(s);\n unordered_map<char, int> hmap;\n\n for (char c:s) hmap[c]++;\n LL deno = 1;\n for (auto &[k, v]:hmap) {\n deno *= fac(v);\n }\n LL tot = fac(n)/deno;\n if (hmap['0'] > 0) {\n hmap['0']--;\n LL deno0 = 1;\n for (auto &[k, v]:hmap) {\n deno0 *= fac(v);\n }\n LL tot0 = fac(n-1)/deno0; // strings start with 0\n tot -= tot0; \n }\n res += tot;\n }\n long long countGoodIntegers(int n, int k) {\n this->n = n;\n vector<string> put;\n unordered_set<string> used;\n put.push_back(\"\");\n\n for (int i=0; i<(n+1)/2; i++) {\n vector<string> newPut;\n for (int num = (i==0?1:0); num<=9; num++) {\n for (auto s:put) {\n newPut.push_back(s + to_string(num));\n }\n }\n put = newPut;\n }\n\n for (auto s:put) {\n string newS;\n if (n%2) {\n newS = s + string(s.rbegin()+1, s.rend());\n if (stoll(newS)%k == 0) count(newS, k, used);\n } else {\n newS = s + string(s.rbegin(), s.rend());\n if (stoll(newS)%k == 0) count(newS, k, used);\n }\n }\n cout << \"here\" << endl;\n return res;\n }\n};",
"memory": "295980"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "using ll = long long;\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n \n vector<int> fac(11, 1);\n for(int i = 1; i < 11; i++){\n fac[i] = fac[i - 1] * i;\n }\n ll ans = 0;\n set<vector<int>> validSet;\n \n function<void(vector<int>&, int)> gen = [&](vector<int>& res, int idx){\n if(idx == (n + 1) / 2){\n vector<int> full(n), dist(10, 0);\n for(int i = 0; i < (n + 1) / 2; i++){\n full[n - i - 1] = res[i];\n full[i] = res[i];\n }\n ll val = 0;\n for(int i = 0; i < n; i++){\n val *= 10;\n val += full[i];\n dist[full[i]] += 1;\n }\n if(val % k == 0){\n ll facs = fac[n];\n for(int i = 0; i < 10; i++){\n facs /= fac[dist[i]];\n }\n // ans += facs;\n validSet.insert(dist);\n }\n \n return;\n }\n for(int i = 0; i < 10; i++){\n if(idx == 0 and i == 0)\n continue;\n res.push_back(i);\n gen(res, idx + 1);\n res.pop_back();\n }\n };\n\n vector<int> res;\n gen(res, 0);\n printf(\"%d\\n\", validSet.size());\n for(auto& v: validSet){\n ll facs = fac[n];\n for(int i = 0; i < 10; i++){\n facs /= fac[v[i]];\n }\n if(v[0] > 0){\n ll zf = fac[n - 1];\n for(int i = 1; i < 10; i++){\n zf /= fac[v[i]];\n }\n zf /= fac[v[0] - 1];\n facs -= zf;\n }\n ans += facs;\n \n }\n return ans;\n }\n};",
"memory": "299856"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n using ll=long long;\n set<string> st;\n bool isvalid(string &s,int k){\n int n=s.length();\n for(int i=0;i<(n+1)/2;i++){\n s[n-1-i]=s[i];\n }\n ll num=stoll(s);\n // for(int i=0;i)\n return (num%k==0);\n }\n void find(int idx,int k,string s){\n if(idx==(s.length()+1)/2){\n if(isvalid(s,k)){\n st.insert(s);\n }\n return;\n }\n if(idx==0){\n for(int i=1;i<=9;i++){\n string temp=s;\n temp[idx]=i+'0';\n find(idx+1,k,temp);\n }\n }else{\n for(int i=0;i<10;i++){\n string temp=s;\n temp[idx]=i+'0';\n find(idx+1,k,temp);\n }\n }\n }\n ll factor(int n){\n if(n==0 || n==1) return 1;\n return (n*factor(n-1));\n }\n long long countGoodIntegers(int n, int k) {\n string s=\"\";\n for(int i=0;i<n;i++){\n s+='0';\n }\n find(0,k,s);\n set<map<int,int>> unique;\n for(auto it: st){\n string s=it;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[s[i]-'0']++;\n }\n unique.insert(mp);\n }\n ll ans=0;\n for(auto mp: unique){\n int val=factor(n);\n for(int i=0;i<=9;i++){\n val/=factor(mp[i]);\n }\n if(mp[0]>0){\n int temp=factor(n-1);\n mp[0]--;\n if(mp[0]==0) mp.erase(0);\n for(int i=0;i<=9;i++){\n temp/=factor(mp[i]);\n }\n ans+=(val-temp);\n }else{\n ans+=val;\n }\n }\n return ans;\n }\n};",
"memory": "315361"
} |
3,548 | <p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>.</p>
<p>An integer <code>x</code> is called <strong>k-palindromic</strong> if:</p>
<ul>
<li><code>x</code> is a <span data-keyword="palindrome-integer">palindrome</span>.</li>
<li><code>x</code> is divisible by <code>k</code>.</li>
</ul>
<p>An integer is called <strong>good</strong> if its digits can be <em>rearranged</em> to form a <strong>k-palindromic</strong> integer. For example, for <code>k = 2</code>, 2020 can be rearranged to form the <em>k-palindromic</em> integer 2002, whereas 1010 cannot be rearranged to form a <em>k-palindromic</em> integer.</p>
<p>Return the count of <strong>good</strong> integers containing <code>n</code> digits.</p>
<p><strong>Note</strong> that <em>any</em> integer must <strong>not</strong> have leading zeros, <strong>neither</strong> before <strong>nor</strong> after rearrangement. For example, 1010 <em>cannot</em> be rearranged to form 101.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p>
<p><strong>Output:</strong> <span class="example-io">27</span></p>
<p><strong>Explanation:</strong></p>
<p><em>Some</em> of the good integers are:</p>
<ul>
<li>551 because it can be rearranged to form 515.</li>
<li>525 because it is already k-palindromic.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1, k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The two good integers are 4 and 8.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2468</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 10</code></li>
<li><code>1 <= k <= 9</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<string> v; \n void solve(int n,string &s){\n if(!n){\n v.push_back(s);\n return ;\n }\n\n for(int i=0;i<=9;i++){\n string a=to_string(i);\n s.insert(s.begin()+0,a.begin(),a.end());\n s.insert(s.begin()+s.size(),a.begin(),a.end());\n solve(n-1,s); \n s.erase(s.begin() +0);\n s.erase(s.begin() + s.size()-1);\n }\n return ;\n\n }\n \n unsigned long long factorial(int n) {\n if (n <= 1) return 1;\n unsigned long long result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n}\n\n// Function to calculate permutations of digits of a number\nunsigned long long calculatePermutations(const std::unordered_map<char, int>& freq) {\n int total = 0;\n unsigned long long numerator = 1;\n \n for (const auto& [digit, count] : freq) {\n total += count;\n numerator *= factorial(count);\n }\n \n return factorial(total) / numerator;\n}\n\n// Function to calculate the number of unique permutations without leading zero\nunsigned long long countPermutationsNoLeadingZero(const std::string& numStr) {\n std::unordered_map<char, int> freq;\n \n // Count the frequency of each digit\n for (char digit : numStr) {\n freq[digit]++;\n }\n \n int total = numStr.length();\n \n // Calculate total permutations\n unsigned long long totalPermutations = calculatePermutations(freq);\n\n // Calculate permutations with leading zero\n if (freq.find('0') != freq.end()) {\n freq['0']--;\n \n unsigned long long permutationsWithLeadingZero = calculatePermutations(freq);\n \n // // For each non-zero digit, compute permutations with this digit at the start\n // permutationsWithLeadingZero *= factorial(zeroCount);\n\n // The total permutations with leading zeros\n totalPermutations -= permutationsWithLeadingZero;\n }\n\n return totalPermutations;\n}\n\n\n long long countGoodIntegers(int n, int k) {\n int loop=n/2; string s=\"\";\n solve(loop,s);\n vector<string> final;\n \n if(n%2!=0){\n for(int i=0;i<v.size();i++){\n for(int j=0;j<=9;j++){\n string f=v[i];\n string a=to_string(j);\n f.insert(f.begin()+n/2,a.begin(),a.end());\n final.push_back(f);\n }\n }\n }\n else {final=v;} unordered_map<string,int> ms;\n vector<string> last;\n for(auto it:final){\n if(it[0]=='0')continue;\n \n \n \n // cout<<it<<endl;\n last.push_back(it);\n }\n // for(auto it:last){\n // cout<<it<<\" \";\n // }\n\n long long ans=0;\n for(int i=0;i<last.size();i++){\n long long b=stol(last[i]);\n string a=last[i];\n sort(a.begin(),a.end());\n if(b%k==0&&ms.find(a)==ms.end()){\n ms[a]++;\n // cout<<countPermutationsNoLeadingZero(last[i])<<endl;\n ans+= countPermutationsNoLeadingZero(last[i]);\n \n }\n }\nreturn ans;\n \n }\n};",
"memory": "315361"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minOrders(int k, vector<int>& capacity, vector<int>& health) {\n const int n = capacity.size();\n vector<int> ind(n);\n long long s = 0;\n for (int i = 0; i < n; ++i) {\n ind[i] = i;\n s += capacity[i];\n }\n sort(ind.begin(), ind.end(), [&](const int x, const int y) {\n return (health[x] + k - 1) / k * capacity[y] < (health[y] + k - 1) / k * capacity[x];\n });\n long long r = 0;\n for (int i = 0; i < n; ++i) {\n r += s * ((health[ind[i]] + k - 1) / k);\n s -= capacity[ind[i]];\n }\n return r;\n }\n};",
"memory": "135444"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "using ll = long long;\nclass Solution {\npublic:\n ll minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n int indices[n];\n for(int i = 0;i < n; i++) {\n indices[i] = i;\n }\n sort(indices, indices + n, [&](int a, int b) {\n return ((health[a] + power-1)/power)*damage[b] < ((health[b] + power-1)/power) *damage[a];\n });\n ll maxDamage = accumulate(damage.begin(), damage.end(), 0);\n ll currentDamage = 0;\n ll totalDamage = 0;\n for(int i = 0;i < n; i++) {\n //cout << indices[i] << \" \" << float(health[indices[i]])/damage[indices[i]] << \" \" << (damagePrefix[n] - damagePrefix[i]) << \" \" << damage[indices[i]] << \" \" << ((health[indices[i]] + power-1)/power) << endl;\n // Assuming damage is a sorted array:\n // add damage[i...n] amount of damage each time\n totalDamage += (maxDamage - currentDamage)*((health[indices[i]] + power-1)/power);\n currentDamage += damage[indices[i]];\n }\n return totalDamage;\n }\n};",
"memory": "135444"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();\nint ids[100000];\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int N = size(damage);\n for (auto &h : health) h = (h-1)/power + 1;\n iota(ids, ids+N, 0);\n sort(ids, ids+N, [&d=damage, &h=health](int i, int j) {return d[j]*h[i] < d[i]*h[j];});\n\n long long res = 0, dur = 0;\n for (int i = 0; i < N; ++i) {\n int j = ids[i];\n dur += health[j];\n res += damage[j] * dur;\n }\n return res; \n }\n};",
"memory": "136133"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int i, n = damage.size();\n long long total = 0, ans = 0;\n pair<int, int> arr[100000];\n for (i = 0; i < n; i++) {\n total += damage[i];\n health[i] = (health[i] - 1) / power + 1;\n arr[i] = make_pair(damage[i], health[i]);\n }\n auto cmp = [](pair<int, int>& lhs, pair<int, int>& rhs) {\n long long l = lhs.first, r = rhs.first;\n return (l * rhs.second == r * lhs.second)? l < r: l * rhs.second > r * lhs.second;\n };\n sort(arr, arr + n, cmp);\n for (i = 0; i < n; i++) {\n ans += total * arr[i].second;\n total -= arr[i].first;\n }\n return ans;\n }\n};",
"memory": "136133"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "#include <iostream>\n#include <algorithm>\n#include <vector>\n#include <map>\n#include <queue>\n#include <set>\n#include <stack>\n#include <unordered_map>\n#include <unordered_set>\n#include <string>\n#include <string.h>\n#include <bitset>\n#include <numeric>\n#include <utility>\n#include <random>\n#include <cassert>\n#include <iomanip>\n \n#define fr(i, n, m) for(int i = (n); i < (m); i ++)\n#define pb push_back\n#define st first\n#define nd second\n#define pq priority_queue\n#define all(x) begin(x), end(x)\n \nusing namespace std;\ntypedef long long ll;\ntypedef long double ld;\ntypedef pair<int,int> pii;\n \nconst int inf = 1e9;\nconst long long mod = 1e9 + 7;\nconst int mxn = 3e5+5;\n\n\nint n;\nvector<pair<int,int> > v;\n\nbool cmp(const pair<int,int> p1, const pair<int,int> p2){\n return p1.st * p2.nd > p2.st * p1.nd;\n}\nclass Solution{\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n n = (int)damage.size();\n for(int i = 0; i < n; i ++){\n health[i] = (health[i] + power - 1)/power;\n }\n v.clear();\n for(int i = 0; i < n; i ++){\n v.push_back({damage[i], health[i]});\n }\n \n sort(v.begin(), v.end(), cmp);\n int time = 0;\n long long eat = 0;\n for(int i = 0; i < n; i ++){\n time += v[i].nd;\n eat += (time) * v[i].st;\n }\n return eat;\n }\n};",
"memory": "136821"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "int p;\nvector<pair<int,int>> enemy;\nbool cmp(pair<int,int> a,pair<int,int> b){\n long long day1=ceil((double)a.second/p);\n long long day2=ceil((double)b.second/p);\n return a.first*day1+b.first*(day2+day1)<b.first*day2+a.first*(day1+day2);\n}\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n enemy.clear();\n p=power;\n int n=damage.size();\n for(int i=0;i<n;i++){\n enemy.push_back({damage[i],health[i]});\n }\n sort(enemy.begin(),enemy.end(),cmp);\n long long ans=0;\n long long day=0;\n for(int i=0;i<n;i++){\n cout<<enemy[i].first<<' '<<enemy[i].second<<'\\n';\n day+=ceil(1.0*enemy[i].second/power);\n ans+=enemy[i].first*day;\n }\n return ans;\n }\n};",
"memory": "136821"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n long long total_damage = 0;\n long long current_time = 0;\n\n // Create an index array for sorting\n vector<int> indices(n);\n for (int i = 0; i < n; ++i) {\n indices[i] = i;\n }\n // Sort based on the damage[i] / ceil(health[i] / power)\n sort(indices.begin(), indices.end(), [&](int i, int j) {\n double ratio_i =\n (double)damage[i] / ceil((double)health[i] / power);\n double ratio_j =\n (double)damage[j] / ceil((double)health[j] / power);\n return ratio_i > ratio_j;\n });\n\n // Calculate the total damage\n for (int i : indices) {\n long long current_damage = damage[i];\n long long current_health = health[i];\n\n long long time_to_kill =\n (current_health + power - 1) /\n power; // Equivalent to ceil(current_health / power)\n\n total_damage += current_damage * (current_time + time_to_kill);\n\n current_time += time_to_kill;\n }\n\n return total_damage;\n }\n};\n",
"memory": "137510"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n const int n = damage.size();\n vector<int> idx(n);\n iota(idx.begin(), idx.end(), 0);\n const auto cmp = [&power, &damage, &health](const auto l, const auto r) {\n const auto ll = ceil((double)health[l] / power);\n const auto lr = ceil((double)health[r] / power);\n return ll * damage[l] + (ll + lr) * damage[r] < damage[r] * lr + (ll + lr) * damage[l];\n };\n sort(idx.begin(), idx.end(), cmp);\n\n long long res = 0;\n long long dam = accumulate(damage.begin(), damage.end(), 0);\n for (const auto i : idx) {\n const int life = ceil((double)health[i] / power);\n res += dam * life;\n dam -= damage[i];\n }\n assert(!dam);\n return res;\n }\n};",
"memory": "137510"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n \n \n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n int n = (int) d.size();\n for (int i = 0; i < n; i++) {\n h[i] = (h[i] + p - 1) / p;\n }\n vector<int> order(n);\n iota(order.begin(), order.end(), 0);\n sort(order.begin(), order.end(), [&](int i, int j) {\n return h[i] * 1LL * d[j] < h[j] * 1LL * d[i];\n });\n long long cnt = 0;\n long long ans = 0;\n for (int i : order) {\n cnt += h[i];\n ans += cnt * d[i];\n }\n return ans;\n\n }\n};",
"memory": "138199"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "#define pii pair<int,int>\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& D, vector<int>& H) {\n vector<int> A(D.size());\n for (int i = 0; i < D.size(); ++i) {\n H[i] = (H[i] + power - 1) / power;\n A[i] = i;\n }\n sort(A.begin(), A.end(), [&](int i, int j) { \n return H[i] * D[j] < H[j] * D[i];\n });\n\n long long res = 0, t = 0;\n for (int i : A) {\n res += D[i] * (t += H[i]);\n }\n return res;\n }\n};",
"memory": "138199"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "#include <vector>\n#include <algorithm>\n#include <numeric>\n#include <iostream>\nusing namespace std;\nclass Solution {\npublic:\n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n const int n = d.size();\n long long s = 0;\n for (int i = 0; i < n; ++i) {\n s += d[i];\n }\n vector<int> ind(n);\n iota(ind.begin(), ind.end(),0);\n sort(ind.begin(), ind.end(),\n [&](int i, int j) {\n long long di = (h[i] + p - 1) / p * s + (h[j] + p - 1) / p * (s - d[i]);\n long long dj = (h[j] + p - 1) / p * s + (h[i] + p - 1) / p * (s - d[j]);\n return di < dj;\n \n });\n\n long long answer = 0;\n long long sub = 0;\n\n for (int i = 0; i < n; ++i) {\n long long d_ = d[ind[i]];\n long long h_ = h[ind[i]];\n answer += ((h_ + p - 1) / p) * (s - sub);\n sub += d_;\n }\n\n return answer;\n }\n};",
"memory": "138888"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n static bool cmp(pair<int, int> a, pair<int, int> b) {\n return (a.first * b.second > b.first * a.second);\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size(), till = 0;\n vector<pair<int, int>> vec(n);\n for (int i = 0; i < n; i++) \n vec[i] = {damage[i], (health[i] + power - 1) / power};\n sort(vec.begin(), vec.end(), cmp);\n long long res = 0;\n for (auto x : vec) {\n till += x.second;\n res += 1ll * x.first * till;\n }\n return res;\n }\n};",
"memory": "138888"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "\n#include <vector>\n#include <cmath>\n#include <algorithm>\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long n = damage.size();\n long long total_damage = 0;\n long long current_time = 0;\n \n // Indices of enemies\n std::vector<long long> indices(n);\n for (int i = 0; i < n; ++i) {\n indices[i] = i;\n }\n \n // Sort indices based on damage per time required to kill\n std::sort(indices.begin(), indices.end(), [&](int i, int j) {\n double value_i = (double)damage[i] / std::ceil((double)health[i] / power);\n double value_j = (double)damage[j] / std::ceil((double)health[j] / power);\n return value_i > value_j; // Sort in descending order\n });\n \n // Calculate the total damage\n for (int i : indices) {\n long long current_damage = damage[i];\n long long current_health = health[i];\n\n long long time_to_kill = std::ceil((double)current_health / power);\n\n total_damage += current_damage * (current_time + time_to_kill);\n\n current_time += time_to_kill;\n }\n \n return total_damage;\n }\n};\n",
"memory": "139576"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "#include <iostream>\n#include <bits/stdc++.h>\nusing namespace std;\n\n\n/****************** Aliases **********************/ \nusing ll= long long;\nusing lld= long double;\nusing ull= unsigned long long;\n\n/****************** Constants **********************/\nconst lld pi= 3.141592653589793238;\nconst ll INF= LONG_LONG_MAX;\nconst ll mod=1e9+7;\n\n/****************** TypeDefine **********************/\ntypedef pair<ll, ll> pll;\ntypedef vector<ll> vll;\ntypedef vector<pll> vpll;\ntypedef vector<string> vs;\ntypedef unordered_map<ll,ll> umll;\ntypedef map<ll,ll> mll;\ntypedef unordered_set<ll> usll;\ntypedef set<ll> sll;\ntypedef priority_queue<ll> pq;\n\n/****************** Macros **********************/\n#define ff first\n#define ss second\n#define pb push_back\n#define mp make_pair\n#define fl(i,n) for(int i=0;i<n;i++)\n#define rl(i,m,n) for(int i=n;i>=m;i--)\n#define each(it, v) for(auto& it: v)\n#define py cout<<\"YES\\n\"\n#define pm cout<<\"-1\\n\"\n#define pn cout<<\"NO\\n\"\n#define vr(v) v.begin(),v.end()\n#define rv(v) v.end(),v.begin()\n#define endl '\\n'\n\n\n/****************** Operator Overloads **********************/\ntemplate<typename T1, typename T2> // cin >> pair<T1, T2>\nistream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }\ntemplate<typename T> // cin >> vector<T>\nistream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}\ntemplate<typename T1, typename T2> // cout << pair<T1, T2>\nostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << \" \" << p.second); }\ntemplate<typename T> // cout << vector<T>\nostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << \" \"; return ostream; }\n\n/****************** Utility Functions **********************/\ntemplate <typename T>\nvoid print(T &&t) { cout << t << \"\\n\"; }\nvoid printarr(ll arr[], ll n){fl(i,n) cout << arr[i] << \" \";cout << \"\\n\";}\ntemplate<typename T>\nvoid printvec(vector<T>v){ll n=v.size();fl(i,n)cout<<v[i]<<\" \";cout<<\"\\n\";}\ntemplate<typename T>\nll sumvec(vector<T>v){ll n=v.size();ll s=0;fl(i,n)s+=v[i];return s;}\n\n/****************** Mathematical Functions **********************/\nll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd \nll lcm(ll a, ll b){return (a/gcd(a,b)*b);}\nll moduloMultiplication(ll a,ll b,ll mod){ll res = 0;a %= mod;while (b){if (b & 1)res = (res + a) % mod;b >>= 1;}return res;}\nll powermod(ll x, ll y, ll p){ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}\n\n\n/****************** Sorting **********************/\nbool sorta(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}\nbool sortd(const pair<int,int> &a,const pair<int,int> &b){return (a.second > b.second);}\n\n/****************** Bits **********************/\nstring decToBinary(int n){string s=\"\";int i = 0;while (n > 0) {s =to_string(n % 2)+s;n = n / 2;i++;}return s;}\nll binaryToDecimal(string n){string num = n;ll dec_value = 0;int base = 1;int len = num.length();for(int i = len - 1; i >= 0; i--){if (num[i] == '1')dec_value += base;base = base * 2;}return dec_value;}\n\n/****************** Check **********************/\nbool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}\nbool isPowerOfTwo(ll n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}\nbool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;}\n\n\nclass Solution {\npublic:\n ll minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size(), curr = 0;\n ll total_damage = 0;\n vll v(n);\n fl(i,n) v[i] = i;\n\n sort(vr(v), [&](int i, int j) {\n double value_i = static_cast<double>(damage[i]) / ceil(static_cast<double>(health[i]) / power);\n double value_j = static_cast<double>(damage[j]) / ceil(static_cast<double>(health[j]) / power);\n return value_i > value_j;\n });\n each(i,v){\n ll curr_d = damage[i], curr_h = health[i];\n ll time = ceil(static_cast<double>(curr_h)/power);\n total_damage += curr_d *(curr+time);\n curr += time;\n }\n return total_damage;\n }\n};",
"memory": "139576"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> A(damage.size());\n for (int i = 0; i < damage.size(); ++i) {\n health[i] = (health[i] + power - 1) / power;\n A[i] = i;\n }\n ranges::sort(A, [&](int i, int j) { \n return health[i] * damage[j] < health[j] * damage[i];\n });\n\n long long res = 0, t = 0;\n for (int i : A)\n res += damage[i] * (t += health[i]);\n return res;\n }\n};",
"memory": "140265"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n long long ans=0;\n vector<pair<int,int>>pi(n);\n for(int i=0;i<n;i++){\n int a=ceil((float)health[i]/power);\n pi[i]={damage[i],a};\n }\n sort(pi.begin(),pi.end(),[](pair<int,int>a,pair<int,int>b){\n double d=double(a.first)/double(a.second);\n double dd=double(b.first)/double(b.second);\n return d>dd;\n });\n int sum=accumulate(damage.begin(),damage.end(),0);\n for(int i=0;i<n;i++){\n cout<<pi[i].first<<\" \"<<pi[i].second<<\" \";\n cout<<sum<<endl;\n ans+=sum*pi[i].second;\n sum-=pi[i].first;\n }\n\n return ans;\n }\n};",
"memory": "140265"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "#define ll long long\n\nll H[100001],D[100001];\n\nbool cmp(int i,int j){\n ll x=H[i]*D[j];\n ll y=H[j]*D[i];\n if(x<y){\n return true;\n }\n else{\n return false;\n }\n}\n\nclass Solution {\npublic:\n \n long long minDamage(int power, vector<int>& d, vector<int>& h) {\n int n=d.size();\n vector<ll> a(n);\n \n for(int i=0;i<n;i++){\n H[i]=(h[i]+power-1)/power;\n D[i]=d[i];\n a[i]=i;\n }\n sort(a.begin(),a.end(),cmp);\n ll time=0,ans=0;\n for(ll i=0;i<n;i++){\n int ind=a[i];\n time+=H[ind];\n ans+=time*D[ind];\n }\n return ans;\n }\n};",
"memory": "140954"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "/*\nh[i]=(h[i]+power-1)/power; is just another fancy way of writing upper bound similar to\nh[i]=ceil((double)h[i]/power) or h[i]=h[i]/power+(h[i]%power!=0)\nBob will gets damage[i] points of damage per second while they are alive (i.e. health[i] > 0).\nd=9 h=3 power=1\nenemy attack =>bob damage=9 enemy health=3 time=0\nbob attack=> bob damage=9 enemy health=3-1 time=1\nenemy attack=> bob damage=9+9 enemy health=3-1 time=2\nbob attack=> bob damage=9+9 enemy health=3-1-1 time=3\nenemy attack=>bob damage=9+9+9 enemy health=3-1-1 time=4\nbob attack=>bob damage=9+9+9 enemy health=3-1-1-1 time=5\nNow in total bob damage=9*3 enemy health=3-power*x\nx-> number bob attacks enemy\nAs enemy attacks first and bob attacks last steps of clash between them will be even \nno of time bob attacks=no of time enemy attacks\nlet enemy health is h-power*x=0\nlet damage inflicts by enemy to bob is d\nx=h/power\nno of times enemy attacks=x*d\n =h/power*d\nif h%power!=0 h is not multiple of power then one extra attack should be incurred\n(h/power+1)*d\ndamage to bob=(h/power+(h/power!=0))*d\n =ceil((double)h/power)*d\nNow question is how bob will select which enemy to attack?\n - high attack potential\n - low health\n - high attack potential with low health (d-h) should be maximum\nout of all 3 selecting enemy with low health is least optimal, because inorder to elimilate this enemy bob need to take damage from all other alive enemy\nfirst approach may sound more optimal except for below testcase\npower=9\nd=35 31\nh=30 1\nif i will try to eliminate element with greater damage\nbob damage=4*(35+31)+31\nand if element with max difference between their health and attack\nbob damage=1*(35+31)+4*35\n4 1 2 3 \n8 4 5 6 \n20 6 10 6\nsort array in ascending order ceil(h[i]/power) and if factors are same then sort descending order of damage\nsort array by damage/time in ascending order\nGreedy appraoch kill enemy with max damage per time first\n*/\ntypedef long long ll;\nclass Solution { \n \npublic:\n long long minDamage(int power, vector<int>& d, vector<int>& h) {\n int n=d.size();\n vector<int>a;\n for(int i=0;i<n;++i){\n h[i]=(h[i]+power-1)/power;\n a.push_back(i);\n }\n sort(a.begin(),a.end(),[&](int i,int j){\n return h[i]*d[j] < d[i]*h[j];\n });\n ll res=0,t=0;\n for(int i:a){\n res+=d[i]*(t+=h[i]);\n }\n return res;\n }\n};",
"memory": "140954"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n static bool comp(pair<int,int> a,pair<int,int> b){\n double x=(double)a.first/a.second;\n double y=(double)b.first/b.second;\n return x>y;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n long long ans=0;\n long long totald=0;\n \n vector<pair<int,int>> help(n);\n for(int i=0;i<n;i++){\n help[i]={damage[i],ceil(health[i]*1.0/power)};\n totald+=damage[i];\n }\n sort(help.begin(),help.end(),comp);\n \n \n for(int i=0;i<n;i++){\n ans=ans+(totald*help[i].second);\n totald-=help[i].first;\n }\n\n\n return ans;\n }\n};",
"memory": "141643"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "typedef long long ll;\n\nclass Solution {\npublic:\n long long minDamage(int p, vector<int>& d, vector<int>& h) {\n ll n=h.size(), sum=accumulate(d.begin(), d.end(), 0LL);\n vector<ll> idx(n);\n iota(idx.begin(), idx.end(), 0LL);\n sort(idx.begin(), idx.end(), [&](const ll& x, const ll& y){\n ll t1=(h[x]+p-1)/p;\n ll t2=(h[y]+p-1)/p;\n return t1*d[y]<t2*d[x];\n // if(a[x]>a[y]){\n // return true;\n // }\n // else if(a[x]==a[y]){\n // return h[x]<h[y];\n // }\n // else{\n // return false;\n // }\n });\n ll ans=0;\n for(ll i: idx){\n //cout << i << \" \";\n ll t=(h[i]+p-1)/p;\n ans+=sum*t;\n sum-=d[i];\n }\n return ans;\n }\n};",
"memory": "141643"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "typedef long long ll;\nstruct Enemy {\n int damage;\n int p; \n};\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<Enemy> enemies(n);\n for(int i = 0; i < n; ++i){\n enemies[i].damage = damage[i];\n enemies[i].p = (health[i] + power - 1) / power;\n }\n sort(enemies.begin(), enemies.end(), [&](const Enemy &a, const Enemy &b) -> bool {\n return (ll)a.damage * b.p > (ll)b.damage * a.p;\n });\n \n ll total_damage = 0;\n ll cumulative_time = 0;\n for(auto &enemy : enemies){\n cumulative_time += enemy.p;\n total_damage += (ll)enemy.damage * cumulative_time;\n }\n \n return total_damage;\n }\n};",
"memory": "142331"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "/*\n 因为血量不能融合,因此每个敌人处理时长是固定的。所以总处理时间一定,那么能改变的\n 只有每个时间内收到的伤害数,我们需要尽量降低这个数值。\n 因此贪心可知,伤害高的应该有限处理,否则多生存一秒还不如让伤害低的多生存一秒。\n 同伤害情况下肯定优先处理血量低的,这样尽量减少存活数量。\n \n 但是贪心不太行,因为假设a需要x秒,b需要y秒,如果先处理a再处理b,其中a>b,那么总伤害\n ax + (x+y)*b,反之,伤害是by+(x+y)*a,作差得到bx-ay,那么如果bx-ay > 0,那么其实\n 还不如先处理b,所以排序应该考虑交叉。\n \n 80*2 + 79*3\n 79*1 + 80*3\n \n 此外我们绝不会处理一秒a,处理一秒b,这样交替两个人死的时间都会加长,总伤害只增不降,因此必然\n 先处理一个,再处理一个。\n */\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int, int>> temp(damage.size());\n for (int i = 0; i < damage.size(); ++i)\n temp[i] = {damage[i], health[i] % power ? health[i] / power + 1 : health[i] / power};\n sort(temp.begin(), temp.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n return b.first * a.second < a.first * b.second;\n });\n int time = 0;\n long long ans = 0;\n for (int i = 0; i < temp.size(); ++i) {\n time += temp[i].second;\n ans += (long long) time * temp[i].first;\n }\n return ans;\n }\n};",
"memory": "142331"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n long long ans = 0;\n\n vector<int> time(n);\n vector<pair<float,int>> r(n);\n\n for(int i = 0;i<n;i++){\n time[i] = (health[i] + power - 1)/ power;\n }\n for(int i = 0;i<n;i++){\n r[i] = {(1.0 * damage[i])/time[i] , i};\n }\n\n sort(r.rbegin(),r.rend());\n long long total_damage = accumulate(damage.begin(),damage.end(),0);\n\n for(int i = 0;i<n;i++){\n auto curr = r[i];\n int index = curr.second;\n int t = time[index];\n\n ans += t*total_damage;\n total_damage -= damage[index];\n }\n\n return ans;\n }\n};",
"memory": "143020"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n\n vector<int> ttk(n);\n for (int i = 0; i < n; i++)\n ttk[i] = (health[i] + power - 1) / power;\n\n vector<pair<float, int>> dif_idx(n); // damage impact factor\n for (int i = 0; i < n; i++) {\n float dif = (float)damage[i] / ttk[i];\n dif_idx[i] = {dif, i};\n }\n\n sort(dif_idx.rbegin(), dif_idx.rend());\n\n long long ans = 0;\n long long damage_from_all = 0;\n\n for (int i = 0; i < n; i++)\n damage_from_all += damage[dif_idx[i].second];\n\n for (int i = 0; i < n; i++) {\n ans += damage_from_all * ttk[dif_idx[i].second];\n damage_from_all -= damage[dif_idx[i].second];\n }\n \n return ans;\n }\n};",
"memory": "143020"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "using LL = long long;\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n // For two enemies,\n // if we kill enemy 0 then enemy 1, the cost is t0d0 + t1d1 + t0d1\n // if we kill enemy 1 then enemy 2, the cost is t0d0 + t1d1 + t1d0\n\n // So we only need to compare t0d1 and t1d0\n\n int N = damage.size();\n vector<int> time;\n for (int i=0; i<N; i++) {\n time.push_back((health[i]+power-1)/power);\n }\n\n vector<int> indices(N);\n iota(indices.begin(), indices.end(), 0);\n sort(indices.begin(), indices.end(), [&](int a, int b) {\n return time[a]*damage[b] < time[b]*damage[a];\n });\n\n LL res = 0, totDamage = accumulate(damage.begin(), damage.end(), 0);\n for (int i=0; i<N; i++) {\n res += totDamage*time[indices[i]];\n totDamage -= damage[indices[i]];\n }\n\n return res;\n }\n};",
"memory": "143709"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "\ntemplate <typename Tvalue>\nstruct vocab\n{\n using this_t = vocab<Tvalue>;\n\n Tvalue m_tValue;\n\n constexpr vocab(Tvalue tValue)\n : m_tValue(tValue)\n {\n }\n\n operator Tvalue () const\n {\n return m_tValue;\n }\n\n this_t & operator= (Tvalue tValue)\n {\n m_tValue = tValue;\n return *this;\n }\n};\n#define DEFINE_VOCAB(name, ...) struct name : vocab<__VA_ARGS__> { using vocab<__VA_ARGS__>::vocab; }\nDEFINE_VOCAB(damage, int);\nDEFINE_VOCAB(health, int);\nDEFINE_VOCAB(damage_over_health, float);\n\nclass Solution {\npublic:\n long long minDamage(int const nPower, std::vector<int> const &vDamage, std::vector<int> const &vHealth)\n {\n // power 40, damage = [31,22,54], health=[29,92,53]\n //\n // enemy {31,1}, {22,3}, {54,2}\n //\n // (6-1) * 31 = 5 * 31 = 155\n // (6-3) * 22 = 3 * 22 = 66\n // (6-2) * 54 = 4 * 54 = 216\n //\n // eliminate {31,1}\n // round damage = 1 * (31 + 22 + 54) = 107\n // 155 - 107 = 48\n // eliminate {22,3}\n // round damage = 3 * (31 + 22 + 54) = 321\n // 66 - 321 = -...\n // eliminate {54,2}\n // round damage = 2 * (31 + 22 + 54) = 214\n // 216 - 214\n //\n // enemy {22,3}, {54,2}\n //\n // (5-3) * 22 = 2 * 22 = 44\n // (5-2) * 54 = 3 * 54 = 162\n //\n // eliminate {22,3}\n // round damage = 3 * (22 + 54) = 3 * 76 = 228\n // 44 - 228\n // eliminate {54,2}\n // round damage = 2 * (22 + 54) = 2 * 76 = 152\n // 162 - 152\n //\n // eliminate {22,3}\n // 22 * 3 = 66\n //\n // total damage: 107 + 152 + 66\n // power 1, damage = [1,2,3,4], health = [1,2,2,2]\n //\n // (7-2) * 4 = 5 * 4 = 20\n // (7-2) * 3 = 5 * 3 = 15\n // (7-2) * 2 = 5 * 2 = 10\n // (7-1) * 1 = 6 * 1 = 6\n //\n // eliminate {4,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {3,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {2,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {1,1}, damage = 1 * (1+2+3+4) = 1 * 10 = 10\n //\n // 20-20 = 0\n // 15-20 = -5\n // 10-20 = -10\n // 6-10 = -4\n //\n // eliminate {4,2}\n //\n // damage = [1,2,3], health = [1,2,2]\n //\n // (5-2) * 3 = 3 * 3 = 9\n // (5-2) * 2 = 3 * 2 = 6\n // (5-1) * 1 = 4 * 1 = 4\n //\n // eliminate {3,2}, damage = 2 * (1+2+3) = 2 * 6 = 12\n // eliminate {2,2}, damage = 2 * (1+2+3) = 2 * 6 = 12\n // eliminate {1,1}, damage = 1 * (1+2+3) = 1 * 6 = 6\n //\n // 9-12 = -3\n // 6-12 = -6\n // 4-6 = -2\n\n // normalize vHealth\n using enemy_t = std::tuple<damage, health, damage_over_health>;\n std::vector<enemy_t> vEnemy;\n vEnemy.reserve(vDamage.size());\n\n long long nHealth_remaining = 0;\n long long nDamage_remaining = 0;\n std::transform\n (\n vDamage.begin(), vDamage.end(),\n vHealth.begin(),\n std::back_inserter(vEnemy),\n [nPower, &nHealth_remaining, &nDamage_remaining](int const nDamage, int const nHealth)->enemy_t\n {\n int const nHealth_normalized = (nHealth + (nPower - 1)) / nPower;\n\n nDamage_remaining += nDamage;\n nHealth_remaining += nHealth_normalized;\n\n return {nDamage, nHealth_normalized, static_cast<float>(nDamage) / static_cast<float>(nHealth_normalized)};\n }\n );\n\n auto const lambdaLess = [](enemy_t const &tupleLHS, enemy_t const &tupleRHS)->bool\n {\n// return std::get<health>(tupleRHS) * std::get<damage>(tupleLHS) < std::get<health>(tupleLHS) * std::get<damage>(tupleRHS);\n return std::get<damage_over_health>(tupleLHS) < std::get<damage_over_health>(tupleRHS);\n };\n std::make_heap(vEnemy.begin(), vEnemy.end(), lambdaLess);\n\n long long llDamage = 0;\n for (size_t zuEnemy_size = vEnemy.size(); zuEnemy_size; --zuEnemy_size)\n {\n // auto const lambdaLess = [=](enemy_t const &tupleLHS, enemy_t const &tupleRHS)->bool\n // {\n // // return\n // // (nHealth_remaining - std::get<health>(tupleLHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (nDamage_remaining - std::get<damage>(tupleLHS)) +\n // // (nHealth_remaining - std::get<health>(tupleLHS) - std::get<health>(tupleRHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (nDamage_remaining - std::get<damage>(tupleLHS) - std::get<damage>(tupleRHS))\n // // <\n // // (nHealth_remaining - std::get<health>(tupleRHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (nDamage_remaining - std::get<damage>(tupleRHS)) +\n // // (nHealth_remaining - std::get<health>(tupleRHS) - std::get<health>(tupleLHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (nDamage_remaining - std::get<damage>(tupleRHS) - std::get<damage>(tupleLHS));\n \n // // return\n // // (-std::get<health>(tupleLHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (-std::get<damage>(tupleLHS))\n // // <\n // // (-std::get<health>(tupleRHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (-std::get<damage>(tupleRHS));\n\n // return\n // std::get<health>(tupleRHS) * std::get<damage>(tupleLHS)\n // <\n // std::get<health>(tupleLHS) * std::get<damage>(tupleRHS);\n // };\n // auto const itEnemy_max = std::max_element(vEnemy.begin(), vEnemy.begin() + zuEnemy_size, lambdaLess);\n\n// std::cout << std::get<damage>(*itEnemy_max) << ',' << std::get<health>(*itEnemy_max) << std::endl;\n\n llDamage += std::get<health>(vEnemy.front()) * nDamage_remaining;\n\n nHealth_remaining -= std::get<health>(vEnemy.front());\n nDamage_remaining -= std::get<damage>(vEnemy.front());\n\n std::pop_heap(vEnemy.begin(), vEnemy.begin() + zuEnemy_size, lambdaLess);\n //std::swap(vEnemy[zuEnemy_size - 1u], *itEnemy_max);\n }\n\n return llDamage;\n }\n};",
"memory": "143709"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // bool customsort(vector<int> a, vector<int> b){\n // if(a[0]!=b[0]) return a[0] > b[0];\n // else return a[1] < b[1];\n // }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long ans = 0, sum = 0;\n int n = damage.size();\n cout<<n<<endl;\n vector<pair<int,pair<int,int>>> v(n);\n for(int i = 0; i<n; ++i){\n v[i].first = damage[i]; v[i].second.second = health[i];\n if(health[i]%power == 0) v[i].second.first = health[i]/power;\n else v[i].second.first = 1+health[i]/power;\n sum+=damage[i];\n }\n sort(v.begin(), v.end(), [](pair<int,pair<int,int>> a, pair<int,pair<int,int>> b){\n if(a.first*b.second.first!=b.first*a.second.first) return a.first*b.second.first>b.first*a.second.first;\n else return a.second.second < b.second.second;\n });\n for(int i = 0; i<n; ++i){\n ans+=v[i].second.first*sum;\n sum-=v[i].first;\n }\n return ans;\n }\n};",
"memory": "144398"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "\ntemplate <typename Tvalue>\nstruct vocab\n{\n using this_t = vocab<Tvalue>;\n\n Tvalue m_tValue;\n\n constexpr vocab(Tvalue tValue)\n : m_tValue(tValue)\n {\n }\n\n operator Tvalue () const\n {\n return m_tValue;\n }\n\n this_t & operator= (Tvalue tValue)\n {\n m_tValue = tValue;\n return *this;\n }\n};\n#define DEFINE_VOCAB(name, ...) struct name : vocab<__VA_ARGS__> { using vocab<__VA_ARGS__>::vocab; }\nDEFINE_VOCAB(damage, int);\nDEFINE_VOCAB(health, int);\nDEFINE_VOCAB(damage_over_health, float);\n\nclass Solution {\npublic:\n long long minDamage(int const nPower, std::vector<int> const &vDamage, std::vector<int> const &vHealth)\n {\n // power 40, damage = [31,22,54], health=[29,92,53]\n //\n // enemy {31,1}, {22,3}, {54,2}\n //\n // (6-1) * 31 = 5 * 31 = 155\n // (6-3) * 22 = 3 * 22 = 66\n // (6-2) * 54 = 4 * 54 = 216\n //\n // eliminate {31,1}\n // round damage = 1 * (31 + 22 + 54) = 107\n // 155 - 107 = 48\n // eliminate {22,3}\n // round damage = 3 * (31 + 22 + 54) = 321\n // 66 - 321 = -...\n // eliminate {54,2}\n // round damage = 2 * (31 + 22 + 54) = 214\n // 216 - 214\n //\n // enemy {22,3}, {54,2}\n //\n // (5-3) * 22 = 2 * 22 = 44\n // (5-2) * 54 = 3 * 54 = 162\n //\n // eliminate {22,3}\n // round damage = 3 * (22 + 54) = 3 * 76 = 228\n // 44 - 228\n // eliminate {54,2}\n // round damage = 2 * (22 + 54) = 2 * 76 = 152\n // 162 - 152\n //\n // eliminate {22,3}\n // 22 * 3 = 66\n //\n // total damage: 107 + 152 + 66\n // power 1, damage = [1,2,3,4], health = [1,2,2,2]\n //\n // (7-2) * 4 = 5 * 4 = 20\n // (7-2) * 3 = 5 * 3 = 15\n // (7-2) * 2 = 5 * 2 = 10\n // (7-1) * 1 = 6 * 1 = 6\n //\n // eliminate {4,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {3,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {2,2}, damage = 2 * (1+2+3+4) = 2 * 10 = 20\n // eliminate {1,1}, damage = 1 * (1+2+3+4) = 1 * 10 = 10\n //\n // 20-20 = 0\n // 15-20 = -5\n // 10-20 = -10\n // 6-10 = -4\n //\n // eliminate {4,2}\n //\n // damage = [1,2,3], health = [1,2,2]\n //\n // (5-2) * 3 = 3 * 3 = 9\n // (5-2) * 2 = 3 * 2 = 6\n // (5-1) * 1 = 4 * 1 = 4\n //\n // eliminate {3,2}, damage = 2 * (1+2+3) = 2 * 6 = 12\n // eliminate {2,2}, damage = 2 * (1+2+3) = 2 * 6 = 12\n // eliminate {1,1}, damage = 1 * (1+2+3) = 1 * 6 = 6\n //\n // 9-12 = -3\n // 6-12 = -6\n // 4-6 = -2\n\n // normalize vHealth\n using enemy_t = std::tuple<damage, health, damage_over_health>;\n std::vector<enemy_t> vEnemy;\n vEnemy.reserve(vDamage.size());\n\n long long nHealth_remaining = 0;\n long long nDamage_remaining = 0;\n std::transform\n (\n vDamage.begin(), vDamage.end(),\n vHealth.begin(),\n std::back_inserter(vEnemy),\n [nPower, &nHealth_remaining, &nDamage_remaining](int const nDamage, int const nHealth)->enemy_t\n {\n int const nHealth_normalized = (nHealth + (nPower - 1)) / nPower;\n\n // nDamage_remaining += nDamage;\n // nHealth_remaining += nHealth_normalized;\n\n return {nDamage, nHealth_normalized, static_cast<float>(nDamage) / static_cast<float>(nHealth_normalized)};\n }\n );\n\n auto const lambdaLess = [](enemy_t const &tupleLHS, enemy_t const &tupleRHS)->bool\n {\n// return std::get<health>(tupleRHS) * std::get<damage>(tupleLHS) < std::get<health>(tupleLHS) * std::get<damage>(tupleRHS);\n return std::get<damage_over_health>(tupleLHS) < std::get<damage_over_health>(tupleRHS);\n };\n std::sort(vEnemy.begin(), vEnemy.end(), lambdaLess);\n\n// long long llDamage = 0;\n// for (size_t zuEnemy_size = vEnemy.size(); zuEnemy_size; --zuEnemy_size)\n// {\n// // auto const lambdaLess = [=](enemy_t const &tupleLHS, enemy_t const &tupleRHS)->bool\n// // {\n// // // return\n// // // (nHealth_remaining - std::get<health>(tupleLHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (nDamage_remaining - std::get<damage>(tupleLHS)) +\n// // // (nHealth_remaining - std::get<health>(tupleLHS) - std::get<health>(tupleRHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (nDamage_remaining - std::get<damage>(tupleLHS) - std::get<damage>(tupleRHS))\n// // // <\n// // // (nHealth_remaining - std::get<health>(tupleRHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (nDamage_remaining - std::get<damage>(tupleRHS)) +\n// // // (nHealth_remaining - std::get<health>(tupleRHS) - std::get<health>(tupleLHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (nDamage_remaining - std::get<damage>(tupleRHS) - std::get<damage>(tupleLHS));\n \n// // // return\n// // // (-std::get<health>(tupleLHS)) * std::get<damage>(tupleRHS) - std::get<health>(tupleRHS) * (-std::get<damage>(tupleLHS))\n// // // <\n// // // (-std::get<health>(tupleRHS)) * std::get<damage>(tupleLHS) - std::get<health>(tupleLHS) * (-std::get<damage>(tupleRHS));\n\n// // return\n// // std::get<health>(tupleRHS) * std::get<damage>(tupleLHS)\n// // <\n// // std::get<health>(tupleLHS) * std::get<damage>(tupleRHS);\n// // };\n// // auto const itEnemy_max = std::max_element(vEnemy.begin(), vEnemy.begin() + zuEnemy_size, lambdaLess);\n\n// // std::cout << std::get<damage>(*itEnemy_max) << ',' << std::get<health>(*itEnemy_max) << std::endl;\n\n// llDamage += std::get<health>(vEnemy[zuEnemy_size - 1u]) * nDamage_remaining;\n\n// nHealth_remaining -= std::get<health>(vEnemy[zuEnemy_size - 1u]);\n// nDamage_remaining -= std::get<damage>(vEnemy[zuEnemy_size - 1u]);\n\n// // std::pop_heap(vEnemy.begin(), vEnemy.begin() + zuEnemy_size, lambdaLess);\n// //std::swap(vEnemy[zuEnemy_size - 1u], *itEnemy_max);\n// }\n\n long long llDamage = 0;\n long long llDamage_accumulated = 0;\n for (enemy_t const &tupleEnemy : vEnemy)\n {\n llDamage_accumulated += std::get<damage>(tupleEnemy);\n llDamage += llDamage_accumulated * std::get<health>(tupleEnemy);\n }\n\n return llDamage;\n }\n};",
"memory": "144398"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "long long d[100005], t[100005], n;\n\nbool bj(const int &i, const int &j){\n return d[j] * t[i] - d[i] * t[j] < 0;\n}\n\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& dd, vector<int>& hh) {\n long long T = 0, i, res = 0;\n vector<int> a;\n n = dd.size();\n for (i = 0; i < n; ++i){\n d[i] = dd[i];\n t[i] = (hh[i] - 1) / power + 1;\n a.push_back(i);\n }\n \n sort(a.begin(), a.end(), bj);\n for (i = 0; i < n; ++i){\n res += d[a[i]] * (T + t[a[i]]);\n T += t[a[i]];\n }\n \n return res;\n }\n};",
"memory": "145086"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<pair<double,int>> v(n);\n for(int i=0;i<n;i++){\n int time = ceil((health[i]+0.0)/power);\n v[i] = {(damage[i]+0.0)/time, i};\n }\n sort(v.rbegin(), v.rend());\n for(auto x: v)cout<<x.first<<\" \"<<x.second<<endl;\n long long tot = 0;\n long long dmg = accumulate(damage.begin(), damage.end(), 0LL);\n\n for(int i=0;i<n;i++){\n int time = ceil((health[v[i].second]+0.0)/power);\n tot += dmg*time;\n dmg -= damage[v[i].second];\n }\n return tot;\n\n }\n};",
"memory": "145086"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool static cmp(pair<int, int>a, pair<int, int>b){\n return (long long)a.first*b.second > (long long) a.second*b.first;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int, int>>v;\n for(int i=0; i<damage.size(); i++){\n v.push_back({damage[i], ceil((double)health[i]/power)});\n }\n sort(v.begin(), v.end(), cmp);\n long long ans = 0;\n long long t=0;\n for(int i=0; i<damage.size(); i++){\n long long d = v[i].first;\n long long ti = v[i].second;\n ans += d*(ti+t);\n t+=ti;\n\n }\n return ans;\n \n }\n};",
"memory": "147841"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n static bool cmp(pair<int,int> a,pair<int,int> b){\n return (1ll*a.first*b.second < 1ll*b.first*a.second);\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n long long ans = 0;\n int n = damage.size();\n vector<pair<int,int>> v;\n for(int i = 0; i < n; i++){\n int x = (health[i]+power-1)/power;\n v.push_back({damage[i],x});\n }\n sort(v.begin(),v.end(),cmp);\n long long c = 0;\n for(int i = n-1; i >= 0; i--){\n c += v[i].second;\n ans += (c*v[i].first);\n }\n return ans;\n }\n};",
"memory": "147841"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n static bool comp(const pair<int,int>&a,const pair<int,int>&b){\n int d1=a.first,d2=b.first;\n int t1=a.second,t2=b.second;\n int tot1=d1*t1+(t1+t2)*d2;\n int tot2=d2*t2+(t1+t2)*d1;\n return tot1<tot2;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n vector<pair<int,int>>v;\n for(int i=0;i<n;i++){\n int t1=(health[i]/power);\n if(health[i]%power!=0){\n t1++;\n }\n v.push_back({damage[i],t1});\n }\n sort(v.begin(),v.end(),comp);\n long long tot=0;\n long long t=0;\n for(int i=0;i<n;i++){\n t+=v[i].second;\n tot+=v[i].first*t;\n }\n return tot;\n }\n};",
"memory": "148530"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "#define ll long long\n\nclass Solution {\npublic:\n static bool comp(const pair<int, int>& a, const pair<int, int>& b) {\n return (ll)a.first * b.second > (ll)b.first * a.second;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int, int>> vp;\n ll totalDamage = 0;\n ll ans = 0;\n\n for (int i = 0; i < damage.size(); ++i) {\n totalDamage += damage[i];\n int time = (health[i] + power - 1) / power;\n vp.emplace_back(damage[i], time);\n }\n\n sort(vp.begin(), vp.end(), comp);\n\n for (const auto& it : vp) {\n ans += (ll)it.second * totalDamage;\n totalDamage -= it.first;\n }\n\n return ans;\n }\n};",
"memory": "148530"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int p, vector<int>& ds, vector<int>& hs) {\n vector<int> idx;\n vector<int> rs;\n for (int i=0; i<ds.size(); i++) {\n idx.push_back(i);\n rs.push_back((hs[i]+p-1)/p); // round\n }\n long long sum = accumulate(ds.begin(), ds.end(), 0);\n ranges::sort(idx, [&](int a, int b) {\n return ds[a]*rs[b] > ds[b]*rs[a]; // ds[a]/rs[a] > ds[b]/rs[b]\n });\n\n long long ans = 0;\n for (int i : idx) {\n ans += sum*rs[i];\n sum -= ds[i];\n }\n return ans;\n }\n};",
"memory": "149219"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> Time;\n int m_len = damage.size();\n vector<int> Index;\n for (int i = 0; i < m_len; i++) {\n int x = health[i];\n int y = power;\n int r = (x % y) ? x / y + 1 : x / y;\n Time.push_back(r);\n Index.push_back(i);\n }\n sort(Index.begin(), Index.end(), [&](int i, int j) {\n \n\n int first = Time[i] * damage[i] + (Time[i] + Time[j]) * damage[j];\n int second = Time[j] * damage[j] + (Time[i] + Time[j]) * damage[i];\n return first < second;\n \n\n });\n long long int ret = 0;\n long long int time = 0;\n for (int i = 0; i < m_len; i++) {\n int j = Index[i];\n \n int d = damage[j] * (Time[j] + time);\n\n ret += damage[j] * (Time[j] + time);\n time += Time[j];\n\n }\n \n return ret;\n \n \n \n \n }\n};",
"memory": "149219"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n static bool comp(pair<int,int> a, pair<int,int>b){\n return (a.first*b.second > b.first*a.second);\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<long long> time(n);\n long long sum = 0;\n long long ans = 0;\n for(int i=0;i<n;i++){\n sum+=damage[i];\n time[i] = health[i]/power + (health[i]%power != 0);\n }\n\n vector<pair<int,int>>v;\n for(int i=0;i<n;i++){\n v.push_back({damage[i],time[i]});\n }\n\n sort(v.begin(),v.end(),comp);\n\n for(int i=0;i<n;i++){\n auto it = v[i];\n ans+=sum*it.second;\n sum-=it.first;\n }\n\n return ans;\n \n }\n};",
"memory": "149908"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n #define ll long long\n #define SORT(A) std::sort(A.begin(), A.end());\n static bool cmp(const std::pair<double, std::pair<ll, ll>>& a, const std::pair<double, std::pair<ll, ll>>& b) {\n if (a.first != b.first) {\n return a.first > b.first; // Sort by the first element (double) in decreasing order\n }\n return a.second.first > b.second.first; // Sort by the second element (time) in decreasing order\n }\n\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<int> time(n);\n \n for(int i=0;i<n;i++){\n auto temp = health[i]/power;\n if(health[i]%power!=0){\n temp++;\n }\n time[i] = temp;\n \n }\n \n vector<pair<double,pair<ll,ll>>> dam(n);\n for(int i=0;i<n;i++){\n dam[i] = {(double)damage[i]/time[i],{time[i],i}};\n }\n \n \n ll total = 0;\n for(int i=0;i<n;i++){\n total += damage[i];\n }\n \n \n \n sort(dam.begin(),dam.end(),cmp);\n ll ans = 0;\n \n for(int i=0;i<n;i++){\n \n ans += total*dam[i].second.first;\n total -= damage[dam[i].second.second];\n }\n \n \n return ans;\n \n }\n};",
"memory": "149908"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n static bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){\n if ((double)a.first/a.second > (double)b.first/b.second){\n return true;\n }\n\n if ((double)a.first/a.second < (double)b.first/b.second){\n return false;\n }\n\n return a.first>b.first;\n \n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int, int>>v;\n int n=damage.size();\n long long totsum;\n long long ans=0;\n\n for(int i=0; i<n; i++){\n v.push_back({damage[i], health[i]/power + (health[i]%power==0? 0: 1) });\n }\n\n sort(v.begin(), v.end(), sortbysec);\n\n for(auto it :v){\n cout<<it.first<<\" \"<<it.second<<endl;\n\n }\n\n vector<long long>damsum(n, 0);\n damsum[0]=v[0].first;\n\n for(int i=1; i<n; i++){\n damsum[i]=damsum[i-1]+v[i].first;\n }\n\n totsum=damsum[n-1];\n\n\n for(int i=0; i<n; i++){\n int k;\n\n k=v[i].second;\n\n if(i==0){\n ans+=(long long)totsum*k;\n }\n else\n ans+=(long long)(totsum-damsum[i-1])*k;\n cout<<ans<<\" \";\n }\n\n return ans;\n }\n};",
"memory": "150596"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<float,int>> pq;\n int total=0;\n vector<int> time(health.size());\n for(int i=0;i<health.size();i++){\n total+=damage[i];\n time[i]=(health[i]+power-1)/power;\n }\n\n for(int i=0;i<damage.size();i++){\n pq.push({(float)damage[i]/time[i],i});\n }\n \n long long ans=0;\n\n while(!pq.empty()){\n int ind=pq.top().second;\n int t=time[ind];\n \n pq.pop();\n while(t--){\n ans+=total;\n }\n total-=damage[ind];\n }\n\n return ans;\n }\n};",
"memory": "150596"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n struct Enemy {\n long long p; // processing time\n long long w; // damage\n };\n \n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n vector<Enemy> enemies(n);\n for(int i=0;i<n;i++){\n enemies[i].w = damage[i];\n enemies[i].p = (health[i] + power -1)/ power;\n }\n \n // Sort enemies by p_i / w_i ascending\n sort(enemies.begin(), enemies.end(), [&](const Enemy &a, const Enemy &b) -> bool{\n // Compare a.p / a.w < b.p / b.w\n // To avoid floating point, compare a.p * b.w < b.p * a.w\n return (a.p * b.w) < (b.p * a.w);\n });\n \n // Compute suffix sums of damage\n vector<long long> suffix_sum(n, 0);\n suffix_sum[n-1] = enemies[n-1].w;\n for(int i=n-2;i>=0;i--){\n suffix_sum[i] = suffix_sum[i+1] + enemies[i].w;\n }\n \n // Compute total damage\n long long total_damage =0;\n for(int i=0;i<n;i++){\n total_damage += enemies[i].p * suffix_sum[i];\n }\n \n return total_damage;\n }\n};\n",
"memory": "151285"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n // greedy - first we will calculate a threat array which will store\n // seconds to kill a particular enemy\n // as with that we can decide which enemy to kill for minimum damage points.\n int n = health.size();\n vector<int> timeToKill(n, 0);\n\n for(int i = 0; i < n; i++) {\n timeToKill[i] = health[i]/power;\n if(health[i]%power != 0) timeToKill[i] = timeToKill[i] + 1;\n }\n\n // now we will calculate damage/threat ratio.\n // and whichever emeny will have bigger damage/threat ratio we will kill him \n // first.\n // pq<ratio, index>\n priority_queue<pair<float, int>> threat;\n\n for(int i = 0; i < n; i++) {\n float ratio = (float)damage[i]/timeToKill[i];\n threat.push({ratio, i});\n }\n\n long long totalDamage = 0;\n for(int i = 0; i < n; i++) {\n totalDamage += damage[i];\n }\n\n long long minDamage = 0;\n\n while(!threat.empty()) {\n pair<float, int> top = threat.top();\n threat.pop();\n int idx = top.second;\n int time = timeToKill[idx];\n\n // cout<<top.first<<\" \"<<top.second<<\" \";\n\n // cout<<totalDamage<<\" \"<<time<<\" \";\n\n minDamage += (long long)totalDamage * time;\n totalDamage -= damage[idx];\n }\n\n return minDamage;\n }\n};",
"memory": "151285"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<int> numMoves;\n for(int i : health) numMoves.push_back((i+power-1)/power);\n vector<pair<int, int>> info;\n long long dps = 0;\n for(int i=0;i<int(health.size());i++) {\n info.push_back({damage[i], numMoves[i]});\n dps += damage[i];\n }\n sort(info.begin(), info.end(), [](pair<int, int> &p1, pair<int, int> &p2) {\n double d1 = p1.first/(1.0 * p1.second);\n double d2 = p2.first/(1.0 * p2.second);\n return d1 > d2;\n });\n long long md = 0;\n for(int i=0;i<int(info.size());i++) {\n md += 1LL * dps * info[i].second;\n dps -= info[i].first;\n }\n return md;\n }\n};",
"memory": "151974"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n int power;\n\n\n static bool comp(const pair<int, int>& a, const pair<int, int>& b, int power) \n {\n int one = ((a.second + power - 1) / power);\n int two = ((b.second + power - 1) / power);\n // return double(a.second) / one > double(b.second) / two;\n return a.first*1ll*two>b.first*1ll*one;\n }\n\n long long minDamage(int power, vector<int>& damage, vector<int>& health) \n {\n this->power = power;\n int n = health.size();\n vector<int> d;\n\n\n for (int i = 0; i < n; i++) {\n int op = (health[i] + power - 1) / power;\n d.push_back(op);\n }\n\n\n vector<pair<int, int>> op;\n\n for (int i = 0; i < n; i++) {\n op.emplace_back(damage[i], health[i]); \n }\n\n sort(op.begin(), op.end(), [this](const pair<int, int>& a, const pair<int, int>& b) {\n return comp(a, b, this->power);\n });\n\n long long ans = 0;\n long long yy = 0;\n\n for (int i = 0; i < n; i++) {\n pair<int, int> g = op[i];\n yy += g.first;\n }\n\n for (int i = 0; i < n; i++) {\n pair<int, int> as = op[i];\n int tt = (as.second + power - 1) / power;\n ans += tt * (yy);\n yy -= as.first;\n }\n\n return ans;\n }\n};\n",
"memory": "151974"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#include <bits/stdc++.h>\n#define ll long long\n#define ld long double\nusing namespace std;\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n ll output=0;\n vector <pair<ld, int>> order(n);\n for (int i=0; i<n; i++) {\n order[i].first = static_cast<ld> ((health[i]+power-1)/power) / (damage[i]);\n order[i].second=i;\n }\n // for (int i=0; i<n; i++) cout<<order[i].first<<\" \"; cout<<endl;\n // for (int i=0; i<n; i++) cout<<order[i].second<<\" \"; cout<<endl;\n sort(order.begin(), order.end());\n for (int i=0; i<n; i++) cout<<order[i].first<<\" \"; cout<<endl<<endl;\n // for (int i=0; i<n; i++) cout<<order[i].second<<\" \"; cout<<endl<<endl;\n ll damageSum=0;\n for (int i=0; i<n ; i++) damageSum+=damage[i]; \n \n for (int i=0; i<n; i++) {\n int time=(health[order[i].second]+power-1)/power;\n output+=time*damageSum;\n damageSum-=damage[order[i].second];\n }\n return output;\n }\n};",
"memory": "152663"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n=damage.size();\n int sum=accumulate(damage.begin(),damage.end(),0);\n for(int i=0;i<n;i++)health[i]=ceil((health[i]*1.0)/power);\n vector<double>v(n);\n for(int i=0;i<n;i++){\n v[i]=(1.0*damage[i])/health[i];\n }\n vector<pair<pair<double,int>,int>>vp(n);\n for(int i=0;i<n;i++){\n // vp[i].first.first=v[i];\n // vp[i].first.second=damage[i];\n // vp[i].second=health[i];\n vp[i]={{v[i],damage[i]},health[i]};\n }\n auto comp=[](const pair<pair<double,int>,int>&a,const pair<pair<double,int>,int>&b){\n if(a.first.first!=b.first.first)return a.first.first>b.first.first;\n else return a.first.second>b.first.second;\n };\n sort(vp.begin(),vp.end(),comp);\n ll ans=0;\n for(int i=0;i<n;i++){\n ll val=vp[i].second*sum*1ll;\n ans+=val;\n sum-=vp[i].first.second;\n }\n return ans;\n }\n};",
"memory": "152663"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int,int>>v;\n int n=health.size();\n for(int i=0;i<n;i++)\n v.push_back({damage[i],health[i]});\n sort(v.begin(),v.end(),[&](pair<int,int>a,pair<int,int>b)\n {\n int k1=(a.second/power)+(a.second%power!=0),k2=(b.second/power)+(b.second%power!=0);\n return (a.first*k2)<(b.first*k1);\n });\n long long ans=0;\n vector<long long>pre(n+1,0);\n pre[0]=v[0].first;\n for(int i=1;i<n;i++)\n pre[i]=pre[i-1]+v[i].first;\n for(int i=n-1;i>=0;i--)\n {\n long long k=v[i].second/power+((v[i].second%power)!=0);\n ans+=(k*pre[i]);\n }\n return ans;\n }\n};",
"memory": "153351"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n vector<pair<int,int>>v;\n int n=health.size();\n for(int i=0;i<n;i++)\n v.push_back({damage[i],health[i]});\n sort(v.begin(),v.end(),[&](pair<int,int>a,pair<int,int>b)\n {\n int k1=(a.second/power)+(a.second%power!=0),k2=(b.second/power)+(b.second%power!=0);\n return (a.first*k2)<(b.first*k1);\n });\n long long ans=0;\n vector<long long>pre(n+1,0);\n pre[0]=v[0].first;\n for(int i=1;i<n;i++)\n pre[i]=pre[i-1]+v[i].first;\n for(int i=n-1;i>=0;i--)\n {\n long long k=v[i].second/power+((v[i].second%power)!=0);\n ans+=(k*pre[i]);\n // cout<<k<<\" \";\n // cout<<ans<<\" \";\n }\n return ans;\n }\n};",
"memory": "153351"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n priority_queue<pair<float,pair<int,int>>> pq;\n int n=damage.size();\n long long ans=0;\n long long totalCost=0;\n for(int i=0;i<n;i++){\n int tm=ceil((float)health[i]/(float)power);\n pq.push({(float)damage[i]/(float)tm,{damage[i],tm}});\n totalCost+=damage[i];\n }\n \n while(!pq.empty()){\n int dm=pq.top().second.first;\n int tm=pq.top().second.second;\n ans+=totalCost*(long long)tm;\n totalCost-=dm;\n pq.pop();\n }\n\n return ans;\n }\n};",
"memory": "154040"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#define ll long long\n#define pii pair<ll,ll>\n\nclass Solution {\npublic:\n long long minDamage(int p, vector<int>d, vector<int>h)\n {\n int n = d.size();\n vector< ll > s(n);\n ll sm = 0;\n vector< pii > v(n);\n for(int i=0;i<n;i++)\n {\n s[i] = ceil( h[i]/(double)p );\n v[i] = { d[i], s[i] };\n sm += d[i];\n }\n \n sort(v.begin(),v.end(),[&](pii &a,pii &b) \n {\n if( a.first/(double)a.second == b.first/(double)b.second ) return a.first > b.first;\n return ( (double)a.first/(double)a.second > (double)b.first/(double)b.second );\n // if(a.first == b.first) return a.second < b.second;\n // return a.first > b.first;\n });\n \n ll ans = 0;\n \n \n for(int i=0;i<n;i++)\n {\n ans += (sm * v[i].second);\n sm -= v[i].first;\n }\n return ans;\n }\n};",
"memory": "154040"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool comp(pair<float,int> n1, pair<float, int> n2) {\n if (n1.first != n2.first)\n return n1.first > n2.first;\n\n return n2.second < n1.second;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n\n vector<int> time;\n for (auto ele : health) {\n time.push_back(ele / power);\n if (ele % power)\n (time.back())++;\n }\n\n vector<pair<float, int>> ratio;\n for (int i = 0; i < n; i++) {\n ratio.push_back({1.0 * damage[i] / time[i], i});\n }\n\n // for(int i=0; i<n; i++){\n // for(int j=0; j<n-1-i; j++){\n // if(ratio[j].first < ratio[j+1].first)\n // swap(ratio[j], ratio[j+1]);\n // else if(ratio[j].first == ratio[j+1].first){\n // int idx1 = ratio[j].second;\n // int idx2 = ratio[j+1].second;\n // if(time[idx1] > time[idx2])\n // swap(ratio[j], ratio[j+1]);\n // }\n\n // }\n // }\n\n sort(ratio.begin(), ratio.end(), [&time](pair<float,int> a, pair<float,int> b){\n if(a.first < b.first) return false;\n\n if(a.first == b.first){\n int idx1 = a.second;\n int idx2 = b.second;\n if (time[idx1] >= time[idx2]) return false;\n }\n\n return true;\n });\n\n for (auto ele : ratio)\n cout << ele.first << \" - \" << ele.second << \" \";\n cout << endl;\n\n\n long long dam = 0;\n long long total = 0;\n for(auto ele: damage){\n total += ele;\n }\n\n for(auto [r, i]: ratio){\n dam += total*time[i];\n total -= damage[i];\n }\n\n return dam;\n }\n};",
"memory": "154729"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n\n bool comp(pair<float,int> n1, pair<float, int> n2) {\n if (n1.first != n2.first)\n return n1.first > n2.first;\n\n return n2.second < n1.second;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n\n vector<int> time;\n for (auto ele : health) {\n time.push_back(ele / power);\n if (ele % power)\n (time.back())++;\n }\n\n vector<pair<float, int>> ratio;\n for (int i = 0; i < n; i++) {\n ratio.push_back({1.0 * damage[i] / time[i], i});\n }\n\n // for(int i=0; i<n; i++){\n // for(int j=0; j<n-1-i; j++){\n // if(ratio[j].first < ratio[j+1].first)\n // swap(ratio[j], ratio[j+1]);\n // else if(ratio[j].first == ratio[j+1].first){\n // int idx1 = ratio[j].second;\n // int idx2 = ratio[j+1].second;\n // if(time[idx1] > time[idx2])\n // swap(ratio[j], ratio[j+1]);\n // }\n\n // }\n // }\n\n sort(ratio.begin(), ratio.end(), [&time](pair<float,int> a, pair<float,int> b){\n if(a.first < b.first) return false;\n\n if(a.first == b.first){\n int idx1 = a.second;\n int idx2 = b.second;\n if (time[idx1] >= time[idx2]) return false;\n }\n\n return true;\n });\n\n for (auto ele : ratio)\n cout << ele.first << \" - \" << ele.second << \" \";\n cout << endl;\n\n\n long long dam = 0;\n long long total = 0;\n for(auto ele: damage){\n total += ele;\n }\n\n for(auto [r, i]: ratio){\n dam += total*time[i];\n total -= damage[i];\n }\n\n return dam;\n }\n};",
"memory": "154729"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n using ll = long long;\n static bool cmp (pair <ll, ll> p1, pair <ll, ll> p2) {\n return p1.first * p2.second < p2.first * p1.second;\n }\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n for (auto &h: health) h = (h + power - 1) / power;\n int n = damage.size();\n vector <pair <ll, ll>> v;\n for (int i = 0; i < n; i ++) {\n v.push_back({health[i], damage[i]});\n }\n sort(v.begin(), v.end(), cmp);\n ll ans = 0;\n for (int i = 0; i < n; i ++) {\n if (i) v[i].first += v[i - 1].first;\n ans += v[i].first * v[i].second;\n }\n return ans;\n }\n};",
"memory": "155418"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "int p;\nclass Solution {\n \npublic:\n static bool fun(pair<int , int > a, pair<int , int > b) {\n \n int a_count = (a.second / p) + (a.second % p != 0);\n int b_count = (b.second / p) + (b.second % p != 0);\n \n int ab = a_count * (a.first + b.first) + b_count * b.first;\n int ba = b_count * (a.first + b.first) + a_count * a.first;\n \n return ab < ba;\n }\n\n long long minDamage(int pow, vector<int>& D, vector<int>& H) {\n p = pow;\n vector<pair<long long , long long >> A;\n int n = D.size();\n long long sum = 0;\n for(int i =0 ; i < n; i++) {\n A.push_back( {D[i], H[i]});\n sum += D[i];\n }\n sort(A.begin(), A.end(), fun);\n \n long long ans =0;\n for(int i = 0; i < n; i++) {\n int tmp = (A[i].second / pow) + (A[i].second % pow != 0);\n cout << A[i].first <<\" \" << tmp <<\" \" << endl;\n ans += (sum* tmp);\n sum -= A[i].first;\n }\n return ans;\n \n }\n};",
"memory": "155418"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#define siz(a) int((a).size())\n#define ll long long\n#define pb push_back\n\nbool sortbysec(const pair<int,int> &a,const pair<int,int> &b) \n{ \n return (a.first * b.second > b.first * a.second); \n} \n \nclass Solution {\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = siz(damage);\n vector <pair<ll,ll>> vux;\n ll tot = 0;\n for(int i = 0; i < n; i++) {\n health[i] = (health[i] + power - 1) / power;\n vux.pb({damage[i], health[i]});\n tot += damage[i];\n }\n sort(vux.begin(), vux.end(), sortbysec);\n ll ans = 0;\n ll timsum = 0;\n for(int i = 0; i < n; i++) {\n //cout<<vux[i].first<<\" \"<<vux[i].second<<endl;\n //timsum += vux[i].second;\n ans += tot * vux[i].second;\n tot -= vux[i].first;\n }\n return ans;\n }\n};",
"memory": "156106"
} |
3,531 | <p>You are given an integer <code>power</code> and two integer arrays <code>damage</code> and <code>health</code>, both having length <code>n</code>.</p>
<p>Bob has <code>n</code> enemies, where enemy <code>i</code> will deal Bob <code>damage[i]</code> <strong>points</strong> of damage per second while they are <em>alive</em> (i.e. <code>health[i] > 0</code>).</p>
<p>Every second, <strong>after</strong> the enemies deal damage to Bob, he chooses <strong>one</strong> of the enemies that is still <em>alive</em> and deals <code>power</code> points of damage to them.</p>
<p>Determine the <strong>minimum</strong> total amount of damage points that will be dealt to Bob before <strong>all</strong> <code>n</code> enemies are <em>dead</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 4, damage = [1,2,3,4], health = [4,5,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">39</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>10 + 10 = 20</code> points.</li>
<li>Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>6 + 6 = 12</code> points.</li>
<li>Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>3</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>2 + 2 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 1, damage = [1,1,1,1], health = [1,2,3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">20</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is <code>4</code> points.</li>
<li>Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is <code>3 + 3 = 6</code> points.</li>
<li>Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is <code>2 + 2 + 2 = 6</code> points.</li>
<li>Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is <code>1 + 1 + 1 + 1 = 4</code> points.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">power = 8, damage = [40], health = [59]</span></p>
<p><strong>Output:</strong> <span class="example-io">320</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= power <= 10<sup>4</sup></code></li>
<li><code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code></li>
<li><code>1 <= damage[i], health[i] <= 10<sup>4</sup></code></li>
</ul>
| 2 | {
"code": "#define ll long long\nbool compare(const pair<int,int>&a, const pair<int,int>&b) {\n return a.first * b.second > b.first * a.second;\n}\nclass Solution {\n\n\npublic:\n long long minDamage(int power, vector<int>& damage, vector<int>& health) {\n int n = damage.size();\n \n \n vector<pair<ll, ll>> v;\n\n for (int i = 0; i < n; ++i) {\n \n ll t = (health[i] + power - 1) / power; \n v.push_back({1ll*damage[i], t});\n }\n\n \n sort(v.begin(), v.end(), compare);\n\n ll total = 0;\n ll curr = 0;\n\n for (auto it: v) {\n curr += it.first; \n }\n\n for (auto it : v) {\n ll t = it.second;\n total += curr * t;\n curr -= it.first; \n }\n\n return total;\n }\n};",
"memory": "156106"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.