id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,540 | <p>You are given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, where <code>n</code> is a <strong>multiple</strong> of <code>k</code>. Your task is to hash the string <code>s</code> into a new string called <code>result</code>, which has a length of <code>n / k</code>.</p>
<p>First, divide <code>s</code> into <code>n / k</code> <strong><span data-keyword="substring-nonempty">substrings</span></strong>, each with a length of <code>k</code>. Then, initialize <code>result</code> as an <strong>empty</strong> string.</p>
<p>For each <strong>substring</strong> in order from the beginning:</p>
<ul>
<li>The <strong>hash value</strong> of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the <strong>English alphabet</strong> (e.g., <code>'a' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0</code>, <code>'b' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1</code>, ..., <code>'z' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25</code>).</li>
<li>Calculate the <em>sum</em> of all the <strong>hash values</strong> of the characters in the substring.</li>
<li>Find the remainder of this sum when divided by 26, which is called <code>hashedChar</code>.</li>
<li>Identify the character in the English lowercase alphabet that corresponds to <code>hashedChar</code>.</li>
<li>Append that character to the end of <code>result</code>.</li>
</ul>
<p>Return <code>result</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcd", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"bf"</span></p>
<p><strong>Explanation:</strong></p>
<p>First substring: <code>"ab"</code>, <code>0 + 1 = 1</code>, <code>1 % 26 = 1</code>, <code>result[0] = 'b'</code>.</p>
<p>Second substring: <code>"cd"</code>, <code>2 + 3 = 5</code>, <code>5 % 26 = 5</code>, <code>result[1] = 'f'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "mxz", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"i"</span></p>
<p><strong>Explanation:</strong></p>
<p>The only substring: <code>"mxz"</code>, <code>12 + 23 + 25 = 60</code>, <code>60 % 26 = 8</code>, <code>result[0] = 'i'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 100</code></li>
<li><code>k <= s.length <= 1000</code></li>
<li><code>s.length</code> is divisible by <code>k</code>.</li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string a=\"abcdefghijklmnopqrstuvwxyz\";\n unordered_map<char , int>mp;\n char kjitna(string &mera){\n int news=0;\n for(auto x:mera){\n news+=mp[x];\n }\n news=news%26;\n return a[news];\n }\n string stringHash(string s, int k) {\n int n=s.size();\n int sum;\n string ans=\"\";\n vector<char>res;\n for(int i=0;i<a.size();i++){\n mp[a[i]]=i;\n }\n int l=k;\n int i=0;\n while(i<n){\n string ans=\"\";\n while(i<l&&i<n){\n ans+=s[i];\n i++;\n }\n l+=k;\n res.push_back(kjitna(ans));\n }\n\n \n\n for(char x:res){\n ans+=x;\n }\n return ans;\n }\n};",
"memory": "14900"
} |
3,540 | <p>You are given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, where <code>n</code> is a <strong>multiple</strong> of <code>k</code>. Your task is to hash the string <code>s</code> into a new string called <code>result</code>, which has a length of <code>n / k</code>.</p>
<p>First, divide <code>s</code> into <code>n / k</code> <strong><span data-keyword="substring-nonempty">substrings</span></strong>, each with a length of <code>k</code>. Then, initialize <code>result</code> as an <strong>empty</strong> string.</p>
<p>For each <strong>substring</strong> in order from the beginning:</p>
<ul>
<li>The <strong>hash value</strong> of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the <strong>English alphabet</strong> (e.g., <code>'a' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0</code>, <code>'b' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1</code>, ..., <code>'z' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25</code>).</li>
<li>Calculate the <em>sum</em> of all the <strong>hash values</strong> of the characters in the substring.</li>
<li>Find the remainder of this sum when divided by 26, which is called <code>hashedChar</code>.</li>
<li>Identify the character in the English lowercase alphabet that corresponds to <code>hashedChar</code>.</li>
<li>Append that character to the end of <code>result</code>.</li>
</ul>
<p>Return <code>result</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcd", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"bf"</span></p>
<p><strong>Explanation:</strong></p>
<p>First substring: <code>"ab"</code>, <code>0 + 1 = 1</code>, <code>1 % 26 = 1</code>, <code>result[0] = 'b'</code>.</p>
<p>Second substring: <code>"cd"</code>, <code>2 + 3 = 5</code>, <code>5 % 26 = 5</code>, <code>result[1] = 'f'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "mxz", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"i"</span></p>
<p><strong>Explanation:</strong></p>
<p>The only substring: <code>"mxz"</code>, <code>12 + 23 + 25 = 60</code>, <code>60 % 26 = 8</code>, <code>result[0] = 'i'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 100</code></li>
<li><code>k <= s.length <= 1000</code></li>
<li><code>s.length</code> is divisible by <code>k</code>.</li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n string stringHash(string s, int k) {\n map<char,int>mp;\n int i=0,n=s.length();\n for(char ch = 'a';ch<='z';ch++){\n mp[ch] = i;\n i++;\n }\n int length = n / k;\n string result = \"\";\n for (int i = 0; i < n; i += k) {\n string str = s.substr(i, k);\n int sum = 0;\n for (int j = 0; j < str.size(); j++) {\n sum += mp[str[j]];\n }\n char ch = 'a'+(sum)%26;\n result += ch;\n }\n return result;\n }\n};",
"memory": "15000"
} |
3,540 | <p>You are given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, where <code>n</code> is a <strong>multiple</strong> of <code>k</code>. Your task is to hash the string <code>s</code> into a new string called <code>result</code>, which has a length of <code>n / k</code>.</p>
<p>First, divide <code>s</code> into <code>n / k</code> <strong><span data-keyword="substring-nonempty">substrings</span></strong>, each with a length of <code>k</code>. Then, initialize <code>result</code> as an <strong>empty</strong> string.</p>
<p>For each <strong>substring</strong> in order from the beginning:</p>
<ul>
<li>The <strong>hash value</strong> of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the <strong>English alphabet</strong> (e.g., <code>'a' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0</code>, <code>'b' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1</code>, ..., <code>'z' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25</code>).</li>
<li>Calculate the <em>sum</em> of all the <strong>hash values</strong> of the characters in the substring.</li>
<li>Find the remainder of this sum when divided by 26, which is called <code>hashedChar</code>.</li>
<li>Identify the character in the English lowercase alphabet that corresponds to <code>hashedChar</code>.</li>
<li>Append that character to the end of <code>result</code>.</li>
</ul>
<p>Return <code>result</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcd", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"bf"</span></p>
<p><strong>Explanation:</strong></p>
<p>First substring: <code>"ab"</code>, <code>0 + 1 = 1</code>, <code>1 % 26 = 1</code>, <code>result[0] = 'b'</code>.</p>
<p>Second substring: <code>"cd"</code>, <code>2 + 3 = 5</code>, <code>5 % 26 = 5</code>, <code>result[1] = 'f'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "mxz", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"i"</span></p>
<p><strong>Explanation:</strong></p>
<p>The only substring: <code>"mxz"</code>, <code>12 + 23 + 25 = 60</code>, <code>60 % 26 = 8</code>, <code>result[0] = 'i'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 100</code></li>
<li><code>k <= s.length <= 1000</code></li>
<li><code>s.length</code> is divisible by <code>k</code>.</li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string stringHash(string s, int k) {\n vector<string>v;\n int n=s.size();\n vector<char>dic;\n // dic.push_back('a');\n for(char i='a';i<='z';i++){\n dic.push_back(i);\n }\n for(int i=0;i<=n-k;i+=k){\n v.push_back(s.substr(i,k));\n }\n string res=\"\";\n for(int i=0;i<v.size();i++){\n long long sum=0;\n string str=v[i];\n for(int j=0;j<str.size();j++){\n sum+=str[j]-'a';\n }\n res+=dic[sum%26];\n }\n return res;\n }\n};",
"memory": "15100"
} |
3,540 | <p>You are given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, where <code>n</code> is a <strong>multiple</strong> of <code>k</code>. Your task is to hash the string <code>s</code> into a new string called <code>result</code>, which has a length of <code>n / k</code>.</p>
<p>First, divide <code>s</code> into <code>n / k</code> <strong><span data-keyword="substring-nonempty">substrings</span></strong>, each with a length of <code>k</code>. Then, initialize <code>result</code> as an <strong>empty</strong> string.</p>
<p>For each <strong>substring</strong> in order from the beginning:</p>
<ul>
<li>The <strong>hash value</strong> of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the <strong>English alphabet</strong> (e.g., <code>'a' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0</code>, <code>'b' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1</code>, ..., <code>'z' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25</code>).</li>
<li>Calculate the <em>sum</em> of all the <strong>hash values</strong> of the characters in the substring.</li>
<li>Find the remainder of this sum when divided by 26, which is called <code>hashedChar</code>.</li>
<li>Identify the character in the English lowercase alphabet that corresponds to <code>hashedChar</code>.</li>
<li>Append that character to the end of <code>result</code>.</li>
</ul>
<p>Return <code>result</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcd", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"bf"</span></p>
<p><strong>Explanation:</strong></p>
<p>First substring: <code>"ab"</code>, <code>0 + 1 = 1</code>, <code>1 % 26 = 1</code>, <code>result[0] = 'b'</code>.</p>
<p>Second substring: <code>"cd"</code>, <code>2 + 3 = 5</code>, <code>5 % 26 = 5</code>, <code>result[1] = 'f'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "mxz", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"i"</span></p>
<p><strong>Explanation:</strong></p>
<p>The only substring: <code>"mxz"</code>, <code>12 + 23 + 25 = 60</code>, <code>60 % 26 = 8</code>, <code>result[0] = 'i'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 100</code></li>
<li><code>k <= s.length <= 1000</code></li>
<li><code>s.length</code> is divisible by <code>k</code>.</li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int getSum(string sub) {\n int res=0;\n for(auto x: sub)\n res+=x-'a';\n return res;\n }\n\n char getChar(int index) {\n return 'a'+index;\n }\n\n string stringHash(string s, int k) {\n vector<string> subs;\n for(int i=0;i<s.size();i+=k)\n subs.push_back(s.substr(i, k));\n string res=\"\";\n for(auto sub: subs) {\n int sum = getSum(sub);\n char hashedChar = getChar(sum%26);\n res+=hashedChar;\n }\n return res;\n }\n};",
"memory": "15200"
} |
3,540 | <p>You are given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, where <code>n</code> is a <strong>multiple</strong> of <code>k</code>. Your task is to hash the string <code>s</code> into a new string called <code>result</code>, which has a length of <code>n / k</code>.</p>
<p>First, divide <code>s</code> into <code>n / k</code> <strong><span data-keyword="substring-nonempty">substrings</span></strong>, each with a length of <code>k</code>. Then, initialize <code>result</code> as an <strong>empty</strong> string.</p>
<p>For each <strong>substring</strong> in order from the beginning:</p>
<ul>
<li>The <strong>hash value</strong> of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the <strong>English alphabet</strong> (e.g., <code>'a' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0</code>, <code>'b' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1</code>, ..., <code>'z' →<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25</code>).</li>
<li>Calculate the <em>sum</em> of all the <strong>hash values</strong> of the characters in the substring.</li>
<li>Find the remainder of this sum when divided by 26, which is called <code>hashedChar</code>.</li>
<li>Identify the character in the English lowercase alphabet that corresponds to <code>hashedChar</code>.</li>
<li>Append that character to the end of <code>result</code>.</li>
</ul>
<p>Return <code>result</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abcd", k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">"bf"</span></p>
<p><strong>Explanation:</strong></p>
<p>First substring: <code>"ab"</code>, <code>0 + 1 = 1</code>, <code>1 % 26 = 1</code>, <code>result[0] = 'b'</code>.</p>
<p>Second substring: <code>"cd"</code>, <code>2 + 3 = 5</code>, <code>5 % 26 = 5</code>, <code>result[1] = 'f'</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "mxz", k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">"i"</span></p>
<p><strong>Explanation:</strong></p>
<p>The only substring: <code>"mxz"</code>, <code>12 + 23 + 25 = 60</code>, <code>60 % 26 = 8</code>, <code>result[0] = 'i'</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= 100</code></li>
<li><code>k <= s.length <= 1000</code></li>
<li><code>s.length</code> is divisible by <code>k</code>.</li>
<li><code>s</code> consists only of lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<string> DivideStr(string s, int k){\n vector<string> v;\n int count = 0;\n string str;\n for(char c : s){\n str+= c;\n ++count;\n if(count == k){\n v.emplace_back(str);\n str.clear();\n count = 0;\n }\n }\n return v;\n}\n\n string stringHash(string s, int k) {\n vector<string> v = DivideStr(s, k);\n string r;\n for(string str : v){\n int sum =0;\n for(char c :str){\n sum += c - 'a';\n }\n r += 'a' + (sum % 26);\n }\n return r;\n }\n};",
"memory": "15300"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n long long ans=0;\n if(k==1){\n if(n==1 || n==2) return 9;\n if(n==3) return 243;\n if(n==4) return 252;\n if(n==5) return 10935;\n if(n==6) return 10944;\n if(n==7) return 617463;\n if(n==8) return 617472;\n if(n==9) return 41457015;\n if(n==10) return 41457024;\n }\n else if(k==2){\n if(n==1 || n==2) return 4;\n if(n==3) return 108;\n if(n==4) return 172;\n if(n==5) return 7400;\n if(n==6) return 9064;\n if(n==7) return 509248;\n if(n==8) return 563392;\n if(n==9) return 37728000;\n if(n==10) return 39718144;\n }\n else if(k==3){\n if(n==1 || n==2) return 3;\n if(n==3) return 69;\n if(n==4) return 84;\n if(n==5) return 3573;\n if(n==6) return 3744;\n if(n==7) return 206217;\n if(n==8) return 207840;\n if(n==9) return 13726509;\n if(n==10) return 13831104;\n }\n else if(k==4){\n if(n==1 || n==2) return 2;\n if(n==3) return 54;\n if(n==4) return 98;\n if(n==5) return 4208;\n if(n==6) return 6992;\n if(n==7) return 393948;\n if(n==8) return 494818;\n if(n==9) return 33175696;\n if(n==10) return 37326452;\n }\n else if(k==5){\n if(n==1 || n==2) return 1;\n if(n==3) return 27;\n if(n==4) return 52;\n if(n==5) return 2231;\n if(n==6) return 3256;\n if(n==7) return 182335;\n if(n==8) return 237112;\n if(n==9) return 15814071;\n if(n==10) return 19284856;\n }\n else if(k==6){\n if(n==1 || n==2) return 1;\n if(n==3) return 30;\n if(n==4) return 58;\n if(n==5) return 2468;\n if(n==6) return 3109;\n if(n==7) return 170176;\n if(n==8) return 188945;\n if(n==9) return 12476696;\n if(n==10) return 13249798;\n }\n else if(k==7){\n if(n==1 || n==2) return 1;\n if(n==3) return 33;\n if(n==4) return 76;\n if(n==5) return 2665;\n if(n==6) return 3044;\n if(n==7) return 377610;\n if(n==8) return 506388;\n if(n==9) return 36789447;\n if(n==10) return 40242031;\n }\n else if(k==8){\n if(n==1 || n==2) return 1;\n if(n==3) return 27;\n if(n==4) return 52;\n if(n==5) return 2231;\n if(n==6) return 5221;\n if(n==7) return 292692;\n if(n==8) return 460048;\n if(n==9) return 30771543;\n if(n==10) return 35755906;\n }\n else if(k==9){\n if(n==1 || n==2) return 1;\n if(n==3) return 23;\n if(n==4) return 28;\n if(n==5) return 1191;\n if(n==6) return 1248;\n if(n==7) return 68739;\n if(n==8) return 69280;\n if(n==9) return 4623119;\n if(n==10) return 4610368;\n }\n return ans;\n }\n};",
"memory": "9138"
} |
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>
| 0 | {
"code": "constexpr array<array<int64_t, 10>, 11> kAns{{\n // k =0 1 2 3 4 5 6 7 8 9\n {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // n = 0\n {0, 9, 4, 3, 2, 1, 1, 1, 1, 1}, // n = 1\n {0, 9, 4, 3, 2, 1, 1, 1, 1, 1}, // n = 2\n {0, 243, 108, 69, 54, 27, 30, 33, 27, 23}, // n = 3\n {0, 252, 172, 84, 98, 52, 58, 76, 52, 28}, // n = 4\n {0, 10935, 7400, 3573, 4208, 2231, 2468, 2665, 2231, 1191}, // n = 5\n {0, 10944, 9064, 3744, 6992, 3256, 3109, 3044, 5221, 1248}, // n = 6\n {0, 617463, 509248, 206217, 393948, 182335, 170176, 377610, 292692, 68739}, // n = 7\n {0, 617472, 563392, 207840, 494818, 237112, 188945, 506388, 460048, 69280}, // n = 8\n {0,41457015,37728000,13726509,33175696,15814071,12476696,36789447,30771543,4623119}, // n = 9\n {0,41457024,39718144,13831104,37326452,19284856,13249798,40242031,35755906,4610368} // n = 10\n}};\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n return kAns[n][k];\n }\n};",
"memory": "9138"
} |
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>
| 0 | {
"code": "class Solution {\n typedef long long LL;\n static const LL base = 11;\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<int> ten(15, 0);\n ten[0] = 1;\n for(int i = 1; i <= 6; ++i) ten[i] = ten[i-1] * 10;\n vector<LL> fac(15, 0);\n fac[0] = 1;\n for(int i = 1; i <= 10; ++i) fac[i] = fac[i-1] * i;\n int hn = (n+1) >> 1;\n LL bb = 1;\n for(int i = 1; i <= hn; ++i) bb *= 10;\n int lim = bb - 1;\n vector<int> cnt(15, 0);\n set<LL> ss;\n LL ans = 0;\n // cout << \"hn = \" << hn << \", bb = \" << bb << \", lim = \" << lim << '\\n';\n // return 0LL;\n for(int i = 1; i <= lim; ++i) {\n if(i%10 == 0) continue;\n for(int j = 0; j <= 9; ++j) cnt[j] = 0;\n int tmpL = 0, tmpR = i, pt = n-hn - 1;\n // cout << \"tmpR = \" << tmpR << \", pt = \" << pt << '\\n';\n for(int j = 1; j <= hn; ++j) {\n int c = tmpR % 10;\n tmpR /= 10;\n if((n&1) && j==hn) {\n ++cnt[c];\n }\n else {\n cnt[c] += 2;\n tmpL += c * ten[pt--];\n }\n }\n LL tmp = tmpL * bb + i;\n if(tmp % k) continue;\n LL ha = 0;\n for(int j = 0; j <= 9; ++j) ha = ha * base + cnt[j];\n // if(ss.find(tmp) != ss.end()) continue;\n // ss.insert(tmp);\n if(ss.find(ha) != ss.end()) continue;\n ss.insert(ha);\n // cout << \"tmp = \" << tmp << '\\n';\n LL res = fac[n];\n for(int j = 0; j <= 9; ++j) res /= fac[cnt[j]];\n if(cnt[0]) {\n LL res2 = fac[n-1];\n res2 /= fac[cnt[0]-1];\n for(int j = 1; j <= 9; ++j) res2 /= fac[cnt[j]];\n res -= res2;\n }\n ans += res;\n }\n return ans;\n }\n};",
"memory": "13014"
} |
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>
| 0 | {
"code": "long long fact[11];\nint speedup = []{\n ios::sync_with_stdio(0); cin.tie(0);\n fact[0] = 1;\n for (int i = 1; i <= 10; ++i) fact[i] = fact[i-1] * i;\n return 0;\n}();\n\nlong long check(long long n, int k, bool odd) {\n long long res = 0, cur = n;\n int i;\n if (odd) i = n/10, res = 1ll << ((n % 10)*4); else i = n;\n while (i) {\n int d = i%10; i/= 10;\n cur = cur*10 + d;\n res += 2ll << (d*4);\n }\n return cur % k == 0 ? res : 0;\n}\n\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n int b = 1, odd = n & 1;\n for (int i = (n-1) >> 1; i; --i) b *= 10;\n\n unordered_set<long long> items;\n for (int e = b*10; b != e; ++b) if (long long v = check(b, k, odd)) items.insert(v);\n\n long long res = 0, f0 = fact[n-1];\n for (long long i : items) {\n long long div = 1, base = f0 * (n - (i&15));\n while (true) {\n div *= fact[i&15];\n if (long long j = i & ~15) i >>= __builtin_ctzll(j) & ~3; else break;\n }\n res += base / div;\n }\n return res;\n }\n};",
"memory": "13014"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n struct vhash {\n size_t operator()(const int *p) const {\n long long r = 0;\n for(int i = 0; i < 10; ++i) r = 10*r+p[i];\n return r % 1000000007;\n }\n };\n int h[10] = {0}, p[10] = {0}, m, kk, nn, nnn, cc;\n long long r, x; unordered_set<int*, vhash> ss;\n int get() {\n int t = m; \n if (h[0]) { t *= h[0]; t /= nnn; t = m-t; }\n ss.insert(h); return t;\n }\n void loop(int i) {\n int k = min(nn, 2), a = 0, b = 1, mm = m;\n if (i <= cc) {\n b = 2; a = (cc == 1 && (x&2)) ? 1 : 0;\n }\n if (a == 0 && i == 0) a += b; \n for(int j = a; j < 10; j += b) {\n for(int q = 0; q < k; ++q) m /= (++h[j]);\n nn -= k; x += (long long)p[i]*j; \n if (nn) loop(i+1); else if (!(x % kk) && !ss.count(h)) r += get(); \n x -= (long long)p[i]*j; nn +=k; h[j] -= k; m = mm;\n } \n }\n long long countGoodIntegers(int n, int k) {\n int i, j; nnn = nn = n; kk = k; x = 0; cc = -1;\n if ((kk & 1) == 0) cc = (kk & 2) ? 0 : 1;\n for(i = j = m = p[0] = 1; i < n; ++i) {\n m *= i; j*= 10; p[i] = j;\n }\n for(i = 0, j = n-1; i < j; ++i, --j) p[i] += p[j]; \n m *= n; loop(0); return r; \n }\n};",
"memory": "16890"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n struct vhash {\n size_t operator()(const int *p) const {\n long long r = 0;\n for(int i = 0; i < 10; ++i) r = 10*r+p[i];\n return r % 1000000007;\n }\n };\n int h[10] = {0}, p[10] = {0}, m, k, nn, n, cc;\n long long r, x; unordered_set<int*, vhash> ss;\n int get() {\n int t = m; \n if (h[0]) { t *= h[0]; t /= n; t = m-t; }\n ss.insert(h); return t;\n }\n void loop(int i) {\n int t = min(nn, 2), a = 0, b = 1, mm = m;\n if (i <= cc) { // cc: others: -1, 2:0, 4:1, 8:2 \n b = 2; a = 0; \n if (i == 1) { if (cc >= 1 && (x&2)) a = 1; }\n else if (i == 2) { if (cc == 2 && (x&4)) a = 1; }\n }\n if (a == 0 && i == 0) a += b; \n for(int j = a; j < 10; j += b) {\n for(int q = 0; q < t; ++q) m /= (++h[j]);\n nn -= t; x += (long long)p[i]*j; \n if (nn) loop(i+1); else if (!(x % k) && !ss.count(h)) r += get(); \n x -= (long long)p[i]*j; nn +=t; h[j] -= t; m = mm;\n } \n }\n long long countGoodIntegers(int n_, int k_) {\n int i, j; nn = n = n_; k = k_; x = 0; cc = -1;\n if ((k & 1) == 0) cc = (k & 2) ? 0 : (k == 4) ? 1 : 2;\n for(i = j = p[0] = 1, m = n; i < n; ++i) {\n m *= i; j*= 10; p[i] = j;\n }\n for(i = 0, j = n-1; i < j; ++i, --j) p[i] += p[j]; \n loop(0); return r; \n }\n};",
"memory": "16890"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n auto factorial = [](int value) -> long\n {\n long fact = 1;\n for (int i = 2; i <= value; ++i) fact *= i;\n return fact;\n };\n const int half_length = (n + 1) / 2,\n min_half = static_cast<int>(std::pow(10, half_length - 1)),\n max_half = static_cast<int>(std::pow(10, half_length));\n long result = 0;\n std::unordered_set<std::string> seen;\n for (int num = min_half; num < max_half; ++num)\n {\n const std::string first_half = std::to_string(num),\n second_half = {first_half.rbegin(), first_half.rend()},\n palindrome = first_half + second_half.substr(n % 2);\n if (std::stol(palindrome) % k != 0) continue;\n std::string sorted_digits = palindrome;\n std::sort(sorted_digits.begin(), sorted_digits.end());\n if (seen.contains(sorted_digits)) continue;\n seen.insert(sorted_digits);\n int digit_count[10] = {};\n for (char c : palindrome) ++digit_count[c - '0'];\n long permutations = (n - digit_count[0]) * factorial(n - 1);\n for (int i = 0; i < 10; ++i)\n if (digit_count[i] > 1)\n permutations /= factorial(digit_count[i]);\n result += permutations;\n }\n return result;\n }\n};",
"memory": "20766"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<long long> factorial(n + 1);\n factorial[0] = 1;\n for (int i = 1; i <= n; ++i) {\n factorial[i] = factorial[i - 1] * i;\n }\n long long res = 0;\n unordered_set<string> vis;\n \n // 用整數型態枚舉左半邊\n // 接著轉成字串去做相應處理\n int base = pow(10, (n - 1) / 2);\n for (int i = base; i < base * 10; ++i) {\n string s = to_string(i);\n // mirror\n s += string(s.rbegin() + (n % 2), s.rend());\n if (stoll(s) % k) { // 不能被 k 整除 就跳過\n continue;\n }\n ranges::sort(s);\n if (!vis.insert(s).second) { // 如果插入的數已經存在過 就跳過\n continue;\n }\n int cnt[10] = {};\n for (int i = 0; i < n; ++i) {\n cnt[s[i] - '0']++;\n }\n long long sum = (n - cnt[0]) * factorial[n-1];\n for (int i = 0; i <= 9; ++i) {\n sum /= factorial[cnt[i]];\n }\n res += sum;\n }\n return res;\n }\n};\n\n// 給兩個正整數 n, k \n// 一個 數字 x 被定義成 k 回文是滿足條件\n// 1. x 本身是回文\n// 2. x 可以被 k 整除\n\n// 一個整數被稱為好的意思是 如果他的數位可以被重排後形成 k 回文 那就是好的整數\n// 回傳包含n個數位的好的整數\n// ===\n\n// 先看清楚題目定義\n// 題目求的是好整數個數\n// 也就是重排之前可以被 k 整除, 重排後 只要是回文那就可以算是好整數\n// 所以不能用數位dp (因為重排後可能不能被k整除)\n// 舉例來說\n// 12321 可以排成 11232\n// 那有兩個方向\n// 1. 枚舉 k 回文數\n// 2. 枚舉好整數\n// 先分析哪個比較好算\n// 如果是選 2 枚舉好整數\n// 那就是要枚舉 10^10-1 (999...9) 種可能\n// 如果是選 1 枚舉 k 回文數\n// 那就比較簡單因為有回文的性質\n// 例如\n// 單純枚舉左半邊, 因為回文對稱所以有右半邊\n// 100 -> 10001\n// 101 -> 10101\n// ...\n// 999 -> 99999\n// 那最大值 n = 10 就只要枚舉一半長度 10^5 就夠了\n// 所以\n// 1. 枚舉所有長為 n 的回文數 (枚舉左半邊)\n// 2. 如果回文數可以被 k 整除\n// 2-1. 怎麼計算有多少個與之對應的好整數 (重排後的數字)\n// 2-2. 如何不重覆不漏掉的統計, 重點在不重複\n// 舉例來說\n// 現在有兩個回文數\n// 好整數可以排成k-回文數, k-回文數反過來也可以排成好整數\n// 排列\n// 12321 -> 11223\n// 21312 -> 11223\n// 如何把左邊兩個k回文數當成同一個數? \n// 因為一個好整數可能包含到多個k回文數\n// 而我們答案是找有幾個好整數 所以不想重複算\n// 那怎麼當成一樣的? => sorting比對後相等就是一樣\n// 所以需要用一個set去存排序後的字串 (不重複)\n\n// 那對一個數字(112233)怎麼計算有幾個不同排列?\n// => 組合問題 有重複的排列個數\n// 例如:12245\n// 12a2b45 可以排成 452a2b1, 452b2a1 其實2在前後都一樣\n// 所以能排成 5!/2!\n// 寫成數學式子就是\n// n!\n// --------------\n// C0!C1!C2!...C9!\n// (如果沒前導 0)\n// 如果有前導 0 就是\n// 例如\n// 102201\n// 先不談論重複問題\n// 第一個數字可以填1,2 所以是4種\n// 4\n// 第二個數字以後就隨便填 所以是5!\n// 4*5!\n// 接著再扣掉重複的\n// 4*5!\n// ----\n// 2!2!2!\n// 寫成數學公式就是\n// (n-C0)*(n-1)!\n// -----------\n// C0!C1!..C9!\n// 接著把這些全部加起來就是答案\n// 2.2 組合計數",
"memory": "20766"
} |
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>
| 0 | {
"code": "using ll = long long ;\nclass Solution {\npublic:\n vector<long long> fact = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};\n long long countGoodIntegers(int n, int k) {\n unordered_set<string> st;\n ll ans = 0;\n\n ll low = pow(10, (n % 2 == 1) ? (n - 1) / 2 : n / 2 - 1),\n high = pow(10, (n % 2 == 1) ? n / 2 + 1 : n / 2) - 1;\n\n // cout << low << \" <--> \" << high << endl;\n for (int i = low; i <= high; i++) {\n string x = to_string(i);\n string y = x;\n\n if (n % 2 != 0)\n y.pop_back();\n reverse(y.begin(), y.end());\n\n string res = x + y; // palindorme string of n len\n\n ll num = stoll(res);\n\n if (num % k)\n continue;\n\n sort(res.begin(), res.end());\n\n if (st.count(res)) continue;\n else st.insert(res);\n\n // cout << res << \" \";\n\n vector<int> freq(10, 0);\n\n for (auto it : res) freq[it - '0']++;\n\n ll possiblePer = fact[n];\n\n for (auto it : freq)\n possiblePer = possiblePer / fact[it];\n\n if (freq[0] != 0) {\n freq[0]--;\n ll permWith0First = fact[n - 1];\n for (auto it : freq)\n permWith0First /= fact[it];\n\n possiblePer -= permWith0First;\n }\n\n ans += possiblePer;\n }\n\n return ans;\n }\n};\n\n/*\n\ny ---> rearrange ---> if (k-pal) cnt ++ ;\n\n\n1 ----> 10000 --> o (n)\n\n y = x + rev x\n if ( Y % k != 0) continue // viloates cndt 2\n freq of digits nikala --> no of digits\n\n find those numbers which can be converted back to y !\n those are my answers ... put that cmbnt in set ( cmbntion because age\naya to skip it )\n\n\n no of poss = (n! / repat !) - (if 0 is present) (n-1 ! / repat fac) //o(1)\n\n\n\n*/",
"memory": "24643"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n\n long long f[12];\n\n long long countGoodIntegers(int n, int k) {\n long long ret = 0;\n int m = (n + 1) / 2;\n \n f[0] = 1;\n for (int i = 1; i < 12; ++i) {\n f[i] = f[i - 1] * i;\n }\n set<string> seen;\n \n\n map<string, int> c;\n set<string> ok;\n for (int i = 0; ; ++i) {\n string s = to_string(i);\n if (s.size() > m) {\n break;\n }\n if (s.size() < m) {\n continue;\n }\n for (int j = m; j < n; ++j) {\n s += s[n - j - 1];\n }\n long long val = stoll(s.c_str());\n if (val % k) {\n continue;\n }\n sort(s.begin(), s.end());\n if (seen.count(s)) {\n continue;\n }\n seen.insert(s);\n vector<int> c(10, 0);\n for (auto ch : s) {\n c[ch - '0'] += 1;\n }\n for (int i = 1; i < 10; ++i) {\n if (c[i]) {\n c[i] -= 1;\n ret += get(c);\n c[i] += 1;\n }\n }\n }\n return ret;\n }\n\n long long get(const vector<int>& c) {\n int n = 0;\n for (auto x : c) {\n n += x;\n }\n long long ret = f[n];\n for (auto x : c) {\n ret = ret / f[x];\n }\n return ret;\n }\n};",
"memory": "24643"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n long long fact(long long n)\n {\n if(n==0)return 1;\n long long ans=1;\n for(long long i=1;i<=n;i++)\n {\n ans*=i;\n\n }\n return ans;\n }\n\n long long permute(string &nums,unordered_set<string>&s)\n {\n vector<long long>count(10,0);\n for(char c:nums)\n {\n count[c-'0']++;\n }\n // long long ans=0;\n long long minus=0;\n if(count[0]>0)\n {\n long long up=fact(nums.size()-1);\n long long d=1;\n for(int i=0;i<10;i++)\n {\n if(i==0)d*=fact(count[i]-1);\n else d*=fact(count[i]);\n }\n minus=up/d;\n \n }\n long long numer=fact(nums.size());\n\n long long dino=1;\n for(int i=0;i<10;i++)\n {\n dino*=fact(count[i]);\n }\n return numer/dino-minus;\n }\n\n\n long long find(int n,int k,unordered_set<string>&s,string &temp)\n {\n string nums=temp;\n string add=nums.substr(0,n/2);\n reverse(add.begin(),add.end());\n nums=nums+add;\n long long num=stoll(nums);\n if(num%k!=0)return 0;\n // if(s.count(num))return 0;\n string t=nums;\n sort(t.begin(),t.end());\n if(s.count(t))return 0;\n s.insert(t);\n\n long long sol=permute(nums,s);\n cout<<t<<\" \"<<sol<<endl;\n return sol;\n\n\n }\n\n void helper(int n,int k,long long &count,unordered_set<string>&s,int index,string &temp)\n {\n if(index==n/2)\n {\n if(n%2==1)\n {\n for(int i=0;i<=9;i++)\n {\n temp.push_back(i+'0');\n count+=find(n,k,s,temp);\n temp.pop_back();\n }\n }\n else count+=find(n,k,s,temp);\n return ;\n }\n for(int i=0;i<=9;i++)\n {\n if(temp.size()==0 && i==0)continue;\n temp.push_back(i+'0');\n helper(n,k,count,s,index+1,temp);\n temp.pop_back();\n }\n\n\n }\n long long countGoodIntegers(int n, int k) {\n long long count=0;\n string temp;\n unordered_set<string>s;\n helper(n,k,count,s,0,temp);\n return count;\n\n\n \n }\n};",
"memory": "28519"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n long long pw10[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};\n map<vector<int>, bool> good;\n\n void rec(int n, int k, int rem, int ready, vector<int> &cnt) {\n if (ready == n) {\n if (!rem) {\n good[cnt] = 1;\n }\n return;\n }\n for (int d = 0; d < 10; d++) {\n if (n > 1 && !d && !ready) continue;\n int nrem = rem, nready = ready;\n if (ready + 1 == n) {\n nready = ready + 1;\n cnt[d]++;\n (nrem += pw10[n / 2] * d % k) %= k;\n rec(n, k, nrem, nready, cnt);\n cnt[d]--;\n } else {\n nready = ready + 2;\n cnt[d] += 2;\n (nrem += pw10[n - 1 - ready / 2] * d % k) %= k;\n (nrem += pw10[ready / 2] * d % k) %= k;\n rec(n, k, nrem, nready, cnt);\n cnt[d] -= 2;\n }\n }\n }\n\n long long fact[10];\n\n long long C(int n, int k) {\n return fact[n] / fact[k] / fact[n - k];\n }\n\n long long count(int n, vector<int> cnt) {\n long long res = 0;\n\n for (int d = 1; d < 10; d++) {\n if (!cnt[d]) continue;\n cnt[d]--;\n\n int places = n - 1;\n long long nums = 1;\n for (int dig = 0; dig < 10; dig++) {\n nums *= C(places, cnt[dig]);\n places -= cnt[dig];\n }\n\n cnt[d]++;\n res += nums;\n }\n\n return res;\n }\n\n long long countGoodIntegers(int n, int k) {\n fact[0] = 1;\n for (int i = 1; i < 10; i++) {\n fact[i] = fact[i - 1] * i;\n }\n\n vector<int> cnt(10);\n rec(n, k, 0, 0, cnt);\n long long ans = 0;\n for (auto &g : good) {\n ans += count(n, g.first);\n }\n return ans;\n }\n};",
"memory": "28519"
} |
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>
| 0 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n static constexpr array<long long,11>Fact={1,1,2,6,24,120,720,5040,40320,362880,3628800};\n long long countGoodIntegers(int n,int k)\n {\n vector<long long>pali_recur;\n if(n==0)return 0;\n int sza=(n+1)/2;\n long long start=1;\n for(int i=1;i<sza;++i)\n {\n start*=10;\n }\n long long end=start*10-1;\n\n if(n==1)start=0;\n for(long long i=start;i<=end;++i)\n {\n long long num=i,k_pali=i;\n if(n%2!=0)k_pali/=10;\n while(k_pali>0)\n {\n num=num*10+(k_pali%10);\n k_pali/=10;\n }\n if(to_string(num).size()==n && num%k==0)\n {\n pali_recur.push_back(num);\n }\n }\n set<array<int,10>>ms;\n for(auto& it:pali_recur)\n {\n array<int,10>arr;\n arr.fill(0);\n long long k_pali=it;\n for(int i=0;i<n;++i)\n {\n arr[k_pali%10]++;\n k_pali/=10;\n }\n ms.insert(arr);\n }\n long long ans=0;\n for(const array<int,10>& Value:ms)\n {\n long long temp=0;\n for(int Good=1;Good<=9;++Good)\n {\n if(Value[Good]==0)continue;\n\n array<int,10>pali_good=Value;\n pali_good[Good]--;\n bool valid=true;\n for(auto& it:pali_good)\n {\n if(it<0)\n {\n valid=false;\n break;\n }\n }\n if(!valid)continue;\n long long init=Fact[n-1];\n for(int i=0;i<=9;++i)\n {\n init/=Fact[pali_good[i]];\n }\n temp+=init;\n }\n ans+=temp;\n }\n return ans;\n }\n};",
"memory": "32395"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n int cnt[10];\n int tot;\n int md;\n long long ans;\n long long C[12][12];\n int odd = 0;\n bool check() {\n vector<int>v;\n for (int i = 0 ; i < 10 ; i++) {\n for (int j = 0 ; j < cnt[i] ; j++) {\n v.push_back(i);\n }\n }\n do{\n if (v[0] == 0) {\n continue;\n }\n long long num = 0;\n for (int i = 0 ; i < v.size() ; i++) {\n num *= 10;\n num += v[i];\n }\n for (int i = v.size() - 1 ; i >= 0 ; i--) {\n num *= 10;\n num += v[i];\n }\n if (num % md == 0 && num != 0) {\n // cout << num << endl;\n return true;\n }\n }while (next_permutation(v.begin(), v.end()));\n return false;\n }\n bool check2(int x) {\n vector<int>v;\n for (int i = 0 ; i < 10 ; i++) {\n for (int j = 0 ; j < cnt[i] ; j++) {\n v.push_back(i);\n }\n }\n do{\n if (v.size() && v[0] == 0) {\n continue;\n }\n long long num = 0;\n for (int i = 0 ; i < v.size() ; i++) {\n num *= 10;\n num += v[i];\n }\n num *= 10;\n num += x;\n for (int i = v.size() - 1 ; i >= 0 ; i--) {\n num *= 10;\n num += v[i];\n }\n if (num % md == 0 && num != 0) {\n // cout << num << endl;\n return true;\n }\n }while (next_permutation(v.begin(), v.end()));\n return false;\n }\n void add() {\n vector<int>cnt2(10, 0);\n for (int i = 0 ; i < 10 ; i++) {\n cnt2[i] = cnt[i] * 2;\n }\n comb(cnt2);\n comb0(cnt2);\n }\n void add2(int x) {\n vector<int>cnt2(10, 0);\n for (int i = 0 ; i < 10 ; i++) {\n cnt2[i] = cnt[i] * 2;\n }\n cnt2[x]++;\n comb(cnt2);\n comb0(cnt2);\n }\n void comb(vector<int>&v) {\n int s = tot * 2 + odd;\n long long mul = 1;\n for (auto &i : v) {\n // cout << s <<' '<<i << endl;\n mul *= C[s][i];\n s -= i;\n }\n ans += mul;\n }\n void comb0(vector<int>&v) {\n if (v[0] == 0) return;\n v[0]--;\n int s = tot * 2 - 1 + odd;\n long long mul = 1;\n for (auto &i : v) {\n //cout << s <<' '<<i << endl;\n mul *= C[s][i];\n s -= i;\n }\n ans -= mul;\n }\n void dfs(int now, int last) {\n if (now == tot) {\n if (odd) {\n for (int i = 0 ; i < 10 ; i++) {\n if (check2(i)) {\n add2(i);\n }\n }\n }\n else if (check()) {\n add();\n }\n return;\n }\n for (int i = last ; i < 10 ; i++) {\n cnt[i]++;\n dfs(now + 1, i);\n cnt[i]--;\n }\n }\n long long countGoodIntegers(int n, int k) {\n odd = n % 2;\n for (int i = 0 ; i < 12 ; i++) {\n C[i][0] = 1;\n }\n for (int i = 1 ; i < 12 ; i++) {\n for (int j = 1 ; j < 12 ; j++) {\n C[i][j] = C[i - 1][j] + C[i - 1][j - 1];\n }\n }\n ans = 0;\n tot = n / 2;\n md = k;\n for (int i = 0 ; i < 10 ; i++) {\n cnt[i] = 0;\n }\n dfs(0, 0);\n return ans;\n }\n};",
"memory": "32395"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n string p;\n using ll = long long;\n ll ans= 0;\n long long countGoodIntegers(int n, int k) {\n p = string(n,'0');\n genpal(0,n-1,k,n);\n return ans;\n }\n unordered_set<string> vis;\n void genpal(int l, int r, int k, int n){\n if (l >r ){\n ll num = stoll(p);\n if (num % k ==0 ){\n auto s = p;\n sort(begin(s),end(s));\n if (vis.count(s) == 0){\n vis.insert(s);\n ans += calc(num, n);\n }\n }\n return ;\n }\n for (int i= (l==0) ? '1' : '0' ; i<='9'; i++){\n p[l] = p[r] = i;\n genpal(l+1,r-1,k,n);\n }\n }\n ll calc(ll num, int n){\n map<int,int>m;\n while(num){\n m[num%10]++;\n num /= 10;\n }\n return Total_Permutations(m,n) - PermutationsStartWithZero(m,n);\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 ll PermutationsStartWithZero(map<int, int> &freq, int n) {\n if ( !freq.count(0) ) return 0;\n freq[0]--;\n ll totalPermutations = fact(n-1);\n\n for (auto i : freq) {\n totalPermutations /= fact(i.second);\n }\n return totalPermutations;\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};",
"memory": "36271"
} |
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>
| 0 | {
"code": "\nclass Solution {\npublic:\n long long res=0;\n long long fact(int x) {\n long long ans=1;\n for(int i=1; i<=x; i++) {\n ans=ans*i;\n }\n return ans;\n }\n set<string> mp;\n void solve(int i, int n, string& s, int k) {\n if(i > (n-1)/2) {\n long long x=stoll(s, nullptr, 10);\n if(x%k==0) {\n string y=s;\n sort(y.begin(), y.end());\n mp.insert(y);\n }\n // cout<<s<<endl;\n return ;\n }\n\n for(int j=0; j<=9; j++) {\n char ch = j+'0';\n s[i]=ch;\n s[n-i-1]=ch;\n solve(i+1, n, s, k);\n }\n }\n long long countGoodIntegers(int n, int k) {\n string s;\n long long fin =0;\n for(int i=0; i<n; i++) s.push_back('0');\n // if(n==1)\n // mp.insert(\"0\");\n for(int i=1; i<=9; i++) {\n char c=i+'0';\n s[0]=c; s[n-1]=c;\n solve(1, n, s, k);\n }\n for(auto it=mp.begin(); it!=mp.end(); it++) {\n string x=*it;\n // cout<<x<<endl;\n res=fact(x.length());\n map<char, int> cnt;\n for(char ch: x) {\n cnt[ch]++;\n }\n for(auto it1=cnt.begin(); it1!=cnt.end(); it1++) {\n res=res/fact(it1->second);\n }\n // subtracting no's with leading zeros;\n if(cnt.find('0')!=cnt.end()) {\n long long y=fact(x.length()-1);\n auto it1=cnt.begin();\n it1++; \n y=y/fact(cnt['0']-1);\n for(; it1!=cnt.end(); it1++) {\n y=y/fact(it1->second);\n }\n res-=y;\n }\n fin+=res;\n }\n return fin;\n }\n};",
"memory": "36271"
} |
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>
| 1 | {
"code": "#define ll long long\nconst int M = 1e9 + 7;\nconst int MAX_N = 500000;\n\nclass Solution {\npublic:\n unordered_map<string, bool> seen;\n \n void generateHalf(string &half, int pos, int n, vector<string> &result, bool oddLength, int& k) {\n if (pos >= n) {\n string fullPalindrome = half;\n if (oddLength) {\n string p = half.substr(0, n - 1);\n reverse(p.begin(), p.end());\n fullPalindrome += p;\n }\n else {\n string p = half;\n reverse(p.begin(), p.end());\n fullPalindrome += p;\n }\n ll val = stoll(fullPalindrome);\n string p = fullPalindrome;\n sort(p.begin(), p.end());\n if(val % k == 0 && !seen[p]) {\n result.push_back(fullPalindrome);\n seen[p] = 1;\n }\n return;\n }\n for (char digit = (pos == 0) ? '1' : '0'; digit <= '9'; ++digit) {\n half[pos] = digit;\n generateHalf(half, pos + 1, n, result, oddLength, k);\n }\n }\n\n vector<string> generatePalindromes(int n, int k) {\n vector<string> result;\n \n if (n == 0) return result;\n \n int halfLength = (n + 1) / 2;\n string half(halfLength, '0');\n \n if (n % 2 == 1) {\n generateHalf(half, 0, halfLength, result, true, k);\n } else {\n generateHalf(half, 0, halfLength, result, false, k);\n }\n \n return result;\n }\n\n ll fac(ll n) {\n ll res = 1;\n while(n){\n res = (res * n);\n n--;\n }\n return res;\n }\n\n ll count(string& s) {\n ll n = s.size();\n vector<int> mp(26, 0);\n for(auto& ch : s) {\n mp[ch - '0']++;\n }\n ll res = fac(n);\n for (int i = 0; i < 26; i++)\n {\n if(mp[i] > 0) {\n res /= fac(mp[i]);\n }\n }\n if(mp[0]) {\n ll firstDigitZero = res;\n firstDigitZero *= fac(mp[0]);\n firstDigitZero /= fac(mp[0] - 1);\n firstDigitZero /= n;\n res -= firstDigitZero;\n }\n return res;\n }\n \n long long countGoodIntegers(int n, int k) {\n ll res = 0;\n vector<string> v = generatePalindromes(n, k);\n for(auto& r : v) {\n res += count(r);\n }\n string s = \"2002\";\n return res;\n }\n};",
"memory": "40148"
} |
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>
| 1 | {
"code": "\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n long long count = 0;\n int half = (n + 1) / 2;\n int start = (n == 1) ? 0 : 1;\n unordered_map<string, bool> done;\n \n for (int i = start; i < pow(10, half); i++) {\n string firstHalf = to_string(i);\n string secondHalf = firstHalf;\n if (n % 2 == 1) {\n secondHalf.pop_back();\n }\n reverse(secondHalf.begin(), secondHalf.end());\n string palindromeStr = firstHalf + secondHalf;\n \n \n if(palindromeStr.length()!=n){\ncontinue;}\n long long palindrome = stoll(palindromeStr);\n \n if (palindrome % k == 0) {\n string sortedPalindrome = palindromeStr;\n sort(sortedPalindrome.begin(), sortedPalindrome.end());\n \n if (done.find(sortedPalindrome) != done.end()) {\n continue;\n }\n done[sortedPalindrome] = true;\n \n long long permCount = factorial(n);\n unordered_map<char, int> freq;\n for (char c : sortedPalindrome) {\n freq[c]++;\n }\n for (auto &[ch, f] : freq) {\n permCount /= factorial(f);\n }\n \n if (sortedPalindrome[0] == '0') {\n string nonZeroPart = sortedPalindrome.substr(1);\n long long invalidCount = factorial(n - 1);\n unordered_map<char, int> nonZeroFreq;\n for (char c : nonZeroPart) {\n nonZeroFreq[c]++;\n }\n for (auto &[ch, f] : nonZeroFreq) {\n invalidCount /= factorial(f);\n }\n permCount -= invalidCount;\n }\n // cout<<sortedPalindrome<<' '<<permCount<<endl;\n count += permCount;\n }\n }\n \n return count;\n }\n \nprivate:\n long long factorial(int n) {\n long long res = 1;\n for (int i = 2; i <= n; i++) {\n res *= i;\n }\n return res;\n }\n};",
"memory": "51776"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n int lo = pow(10, n/2-1);\n int hi = pow(10, n/2) - 1;\n long long ans = 0;\n if (n == 1)\n {\n for (int i = 1; i<10; i++)\n {\n if (i % k == 0)\n ans++;\n }\n return ans;\n }\n map<string,bool>mp;\n // cout<<lo<<\" \"<<hi<<endl;\n for (int i = lo; i<= hi; i++)\n {\n string x1 = to_string(i);\n string rev = x1;\n reverse(rev.begin(), rev.end());\n string x = x1 + rev;\n if (n % 2)\n {\n for (char ch = '0'; ch <= '9' ;ch++)\n {\n x = x1;\n x.push_back(ch);\n x += rev;\n long long tp = stoll(x);\n if (tp % k == 0)\n {\n sort(x.begin(), x.end());\n mp[x]=1;\n // ans++;\n }\n }\n // else if (mp.count(x))\n // ans++;\n }\n else\n {\n long long tp = stoll(x);\n if (tp % k == 0)\n {\n sort(x.begin(), x.end());\n mp[x]=1;\n // ans++;\n }\n // else if (mp.count(x))\n // ans++;\n }\n }\n // for (auto &i:mp)\n // cout<<i.first<<endl;\n \n vector<long long>fact(11);\n fact[0] = 1;\n for (int i = 1; i<=10; i++)\n fact[i] = fact[i-1] *i; \n\n for (auto &i: mp)\n {\n map<char,int>mp2;\n for (auto &j: i.first)\n mp2[j]++;\n // for (auto x : mp2)\n // cout<<x.first<<\" \"<<x.second<<endl;\n long long tp = fact[n];\n for (auto &j : mp2)\n tp /= fact[j.second];\n int tp2 = fact[n - 1];\n if (mp2['0'])\n {\n for (char ch = '1'; ch <= '9'; ch++)\n {\n tp2 /= fact[mp2[ch]];\n }\n tp2 /= fact[mp2['0']-1];\n tp -= tp2;\n }\n ans += tp;\n }\n return ans;\n }\n};",
"memory": "51776"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n\n unordered_set<string> d;\n long long ret=0;\n string s=\"\";\n int nn,kk;\n int f[11];\n\n void DFS(int l, int r){\n if (l<=r){\n for (char c=(l==0?'1':'0');c<='9';c++){\n s[l]=s[r]=c;\n DFS(l+1,r-1);\n }\n }else{\n long long x=stoll(s);\n if (x%kk==0) {\n string c=\"\";\n int b[10];\n for (int i=0;i<10;i++) b[i]=0;\n for (int i=0;i<nn;i++) b[s[i]-'0']++;\n for (int i=0;i<10;i++) c+=to_string(b[i])+\"|\";\n if (d.contains(c)) return;\n d.insert(c);\n int r=nn-b[0];\n for (int i=nn-1;i>0;i--) r*=i;\n for (int i=0;i<10;i++) r/=f[b[i]];\n ret+=r;\n }\n }\n }\n\n\n long long countGoodIntegers(int n, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n for (int i=0;i<n;i++) s+='0';\n f[0]=1;\n for (int i=1;i<11;i++){\n f[i]=i*f[i-1];\n }\n nn=n;\n kk=k;\n DFS(0,n-1);\n return ret;\n }\n};",
"memory": "55653"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n\n unordered_set<string> d;\n long long ret=0;\n string s=\"\";\n int nn,kk;\n\n void DFS(int l, int r){\n if (l<=r){\n for (char c=(l==0?'1':'0');c<='9';c++){\n s[l]=s[r]=c;\n DFS(l+1,r-1);\n }\n }else{\n long long x=stoll(s);\n if (x%kk==0) {\n string c=\"\";\n int b[10];\n for (int i=0;i<10;i++) b[i]=0;\n for (int i=0;i<nn;i++) b[s[i]-'0']++;\n for (int i=0;i<10;i++) c+=to_string(b[i])+\"|\";\n if (d.contains(c)) return;\n d.insert(c);\n int r=nn-b[0];\n for (int i=nn-1;i>0;i--) r*=i;\n for (int i=0;i<10;i++) r/=tgamma(b[i]+1);\n ret+=r;\n }\n }\n }\n\n\n long long countGoodIntegers(int n, int k) {\n for (int i=0;i<n;i++) s+='0';\n nn=n;\n kk=k;\n DFS(0,n-1);\n return ret;\n }\n};",
"memory": "55653"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n vector <int> fact (int n)\n {\n vector <int> ans (n+1, 1);\n for (int i = 2; i <= n; i++) ans[i] = i*ans[i-1];\n return ans;\n }\n \n long long countGoodIntegers(int n, int k)\n {\n int n2 = n/2 + n%2;\n int min = 1, max = pow (10, n2)-1;\n vector <int> fac = fact (10);\n\n for (int i = 1; i <= max; i++, min++)\n {\n long long num = i;\n long long p = pow (10, n-1);\n for (int j = n, cpy = i; j > n2; j--, cpy /= 10, p /= 10) num += (cpy%10)*p;\n if (num%k == 0 or num%10 == 0) break;;\n }\n\n int skip = 1;\n if (n % 2 == 0 and n2 > 3 and k != 7) skip = k;\n\n long long ans = 0;\n unordered_map <int, unordered_map<int, bool>> map;\n for (int i = min; i <= max; i+=skip)\n {\n long long num = i;\n long long p = pow (10, n-1);\n for (int j = n, cpy = i; j > n2; j--, cpy /= 10, p /= 10) num += (cpy%10)*p;\n if (num%k != 0 or num%10 == 0)continue;\n\n vector <int> vec (10);\n for (int j = n, cpy = i; j > n2-n%2; j--, cpy/= 10)\n {\n if (j != n2) vec[cpy%10]+=2;\n else vec[cpy]++;\n }\n\n int st = 0, st2 = 0;\n for (int i = 9, p = 1; i >= 0; i--)\n {\n if (vec[i] > 0)\n {\n st += i*p;\n st2 += vec[i]*p;\n p *= 10;\n }\n }\n if (map[st][st2]) continue;\n else map[st][st2] = true;\n\n int tmp = fac[n];\n for (int j = 0; j < 10; j++) tmp /= fac[vec[j]];\n if (vec[0] > 0)\n {\n int tmp2 = fac[n-1]/fac[vec[0]- 1];\n for (int j = 1; j < 10; j++) tmp2 /= fac[vec[j]];\n tmp -= tmp2;\n }\n ans += tmp;\n }\n return ans;\n }\n};",
"memory": "59529"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n vector <int> fact (int n)\n {\n vector <int> ans (n+1, 1);\n for (int i = 2; i <= n; i++) ans[i] = i*ans[i-1];\n return ans;\n }\n \n long long countGoodIntegers(int n, int k)\n {\n int n2 = n/2 + n%2;\n int max = pow (10, n2)-1;\n vector <int> fac = fact (10);\n\n long long ans = 0;\n unordered_map <int, unordered_map<int, bool>> map;\n for (int i = 1; i <= max; i++)\n {\n long long num = i;\n long long p = pow (10, n-1);\n for (int j = n, cpy = i; j > n2; j--, cpy /= 10, p /= 10) num += (cpy%10)*p;\n if (num%k != 0 or num%10 == 0)continue;\n\n vector <int> vec (10);\n for (int j = n, cpy = i; j > n2-n%2; j--, cpy/= 10)\n {\n if (j != n2) vec[cpy%10]+=2;\n else vec[cpy]++;\n }\n\n int st = 0, st2 = 0;\n for (int i = 9, p = 1; i >= 0; i--)\n {\n if (vec[i] > 0)\n {\n st += i*p;\n st2 += vec[i]*p;\n p *= 10;\n }\n }\n if (map[st][st2]) continue;\n else map[st][st2] = true;\n\n int tmp = fac[n];\n for (int j = 0; j < 10; j++) tmp /= fac[vec[j]];\n if (vec[0] > 0)\n {\n int tmp2 = fac[n-1]/fac[vec[0]- 1];\n for (int j = 1; j < 10; j++) tmp2 /= fac[vec[j]];\n tmp -= tmp2;\n }\n ans += tmp;\n }\n return ans;\n }\n};",
"memory": "59529"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n#define ll long long\nunordered_map<string, int> mp;\n void f(ll &res, string &cur, int d, int &n, int &k) {\n if (d == 0) {\n string tmp = cur;\n int m = cur.size();\n for (int i = m-1; i >= 0; i--) {\n if (i == m-1) {\n if (n%2 == 0) tmp += cur[i];\n } else {\n tmp += cur[i];\n }\n }\n ll num = stoll(tmp);\n if (num % k == 0) {\n vector<int> dg(10, 0);\n for (auto &i : tmp) {\n dg[i-'0']++;\n }\n sort(tmp.begin(), tmp.end());\n if (mp[tmp] == 1) return;\n ll x = 1;\n if (dg[0] > 0) {\n x = n-dg[0];\n for (int i = n-1; i >= 2; i--) {\n x *= i;\n }\n } else {\n for (int i = n; i >= 2; i--) {\n x *= i;\n }\n }\n for (int i = 0; i < 10; i++) {\n if (dg[i] > 1) {\n for (int j = dg[i]; j >= 2; j--) {\n x /= j;\n }\n }\n }\n mp[tmp] = 1;\n res += x;\n }\n return;\n }\n if (d == (n+1)/2) {\n for (int i = 1; i <= 9; i++) {\n cur.push_back((char) ('0'+i));\n f(res, cur, d-1, n, k);\n cur.pop_back();\n }\n } else {\n for (int i = 0; i <= 9; i++) {\n cur.push_back((char) ('0'+i));\n f(res, cur, d-1, n, k);\n cur.pop_back();\n }\n }\n }\n long long countGoodIntegers(int n, int k) {\n ll res = 0;\n string tmp = \"\";\n f(res, tmp, (n+1)/2, n, k);\n return res;\n }\n};",
"memory": "63405"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n vector<long long> fact;\n map<string, bool> vis;\n long long ans = 0; // Declare global answer to track the result\n\n long long count(string s) {\n int n = s.length();\n if (n == 1)\n return 1;\n \n vector<int> fr(10);\n for (int i = 0; i < n; i++) {\n fr[s[i] - '0']++;\n }\n string str = \"\";\n long long tot = fact[n];\n for (int i = 0; i <= 9; i++) {\n int cur = fr[i];\n str += 'a' + cur;\n tot /= fact[cur];\n }\n \n if (vis.find(str) != vis.end()) {\n return 0;\n }\n vis[str] = true;\n \n if (fr[0] == 0) {\n return tot;\n }\n \n long long ex = fact[n - 1];\n fr[0]--;\n for (int i = 0; i <= 9; i++) {\n int cur = fr[i];\n ex /= fact[cur];\n }\n \n return tot - ex;\n }\n\n bool check(string s, int k) {\n long long num = 0;\n long long cur = 1;\n for (int i = s.length() - 1; i >= 0; i--) {\n num += (cur * (s[i] - '0'));\n cur *= 10;\n }\n return num % k == 0;\n }\n\n string genPal(int num, int len) {\n string s = to_string(num);\n string t = s.substr(0, len); // Corrected the substr syntax\n reverse(t.begin(), t.end());\n return s + t;\n }\n\n void find(int pos, int num, int len, bool even, int k) {\n if (pos == len) {\n int val = even ? len : len - 1;\n string s = genPal(num, val);\n if (check(s, k)) {\n ans += count(s);\n }\n return;\n }\n \n int start = (pos == 0) ? 1 : 0;\n for (int i = start; i <= 9; i++) {\n int tnum = num * 10 + i;\n find(pos + 1, tnum, len, even, k);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n fact = vector<long long>(11, 1); // Initialize fact with size 11\n for (int i = 1; i <= 10; i++) {\n fact[i] = i * fact[i - 1];\n }\n \n bool even = (n % 2 == 0);\n int len = n / 2 + (even ? 0 : 1);\n \n ans = 0; // Reset answer for each test case\n vis.clear(); // Clear visited map\n \n find(0, 0, len, even, k);\n \n return ans;\n }\n};\n",
"memory": "63405"
} |
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>
| 1 | {
"code": "class Solution {\n unordered_set<string> st;\n long long createPalindrome(int half, int n) {\n string str = to_string(half);\n string rev = string(str.rbegin(), str.rend());\n if (n % 2 != 0) {\n rev = rev.substr(1);\n }\n return stoll(str + rev);\n }\n\n void generatePalindromeNumbers(int n, int k) {\n int base = pow(10, (n - 1) / 2);\n vector<long long> palindromes;\n\n for (int i = base; i < base * 10; i++) {\n long long palindrome = createPalindrome(i, n);\n if (palindrome % k == 0) {\n vector<int> c(10);\n while(palindrome) {\n c[palindrome % 10] += 1;\n palindrome /= 10;\n }\n string s = \"\";\n for(int j = 0; j < 10; j++)\n if(c[j]) {\n s += string(c[j], char('0' + j));\n }\n\n st.insert(s);\n }\n }\n }\n\n long long factorial(int n) {\n long long fact = 1;\n for (int i = 2; i <= n; ++i) {\n fact *= i;\n }\n return fact;\n }\n\n // Function to calculate permutations of remaining digits\n long long calculatePermutations(vector<int>& arr) {\n long long total_digits = 0;\n long long perm = 1;\n\n for (int i = 0; i < 10; ++i) {\n total_digits += arr[i];\n perm *= factorial(arr[i]);\n }\n\n return factorial(total_digits) / perm;\n }\n\n // Function to count valid numbers\n long long countValidNumbers(vector<int>& arr) {\n long long total_count = 0;\n\n // Iterate through possible leading digits (1 to 9)\n for (int i = 1; i <= 9; ++i) {\n if (arr[i] > 0) {\n // Choose digit `i` as the leading digit\n arr[i]--;\n\n // Calculate permutations of the remaining digits\n total_count += calculatePermutations(arr);\n\n // Restore the count of digit `i`\n arr[i]++;\n }\n }\n\n return total_count;\n }\npublic:\n long long countGoodIntegers(int n, int k) {\n generatePalindromeNumbers(n, k);\n long long ans = 0;\n for(auto &s: st) {\n vector<int> v(10);\n for(char ch: s)\n v[ch - '0']++;\n ans += countValidNumbers(v);\n }\n\n return ans;\n\n }\n};",
"memory": "67281"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n map<vector<int>,bool> mp;\n \n long long factorial(long x){\n if(x==0)\n return 1;\n return factorial(x-1)*x;\n }\n long long perms(long num){\n vector<int> cnt(10,0);\n int tot=0;\n while(num!=0){\n cnt[num%10]++;\n num=num/10;\n tot++;\n }\n if(mp.find(cnt)!=mp.end()){\n return 0;\n }\n mp[cnt]=true;\n long long p=factorial(tot);\n \n for(int i=0;i<10;i++){\n p=(p/factorial(cnt[i]));\n }\n if(cnt[0]>0)\n p=p-((cnt[0]*p)/(tot));\n \n return p;\n }\n long long recur(long num,int x,int n,int k){\n if(x==0){\n string a = to_string(num);\n string b =a;\n if(n&1){\n b.pop_back();\n }\n reverse(b.begin(),b.end());\n \n string c = a+b;\n long number = stol(c);\n // cout<<number<<\" \";\n\n if(number%k==0){\n // cout<<number<<\" \";\n return perms(number);\n }\n \n return 0;\n }\n \n long long res=0;\n for(int i=0;i<10;i++){\n if(i!=0 || num!=0 )\n res+=recur(num*10+i,x-1,n,k);\n }\n return res;\n }\n \n long long countGoodIntegers(int n, int k) {\n \n return recur(0,(n+1)/2,n,k); \n }\n};\n ",
"memory": "67281"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n using ll = long long;\n vector<ll> fact;\n\n Solution() {\n fact.resize(12);\n fact[0] = 1;\n for(ll i = 1; i < 12; i++)\n fact[i] = fact[i - 1]*i;\n }\n\n ll ncr(ll n, ll r) {\n ll den = fact[n - r]*fact[r];\n return (fact[n] / den);\n }\n\n long long countGoodIntegers(ll n, ll k) {\n set<vector<int>> ms;\n for(int val = 1; ;val++) {\n string sval = to_string(val);\n int len = sval.length();\n for(int i = len - 1 - (n % 2); i >= 0; i--) {\n sval.push_back(sval[i]);\n }\n\n len = sval.length();\n ll nval = stoll(sval);\n if(len < n || nval % k != 0)\n continue;\n else if(len > n)\n break;\n\n vector<int> freq(10, 0);\n for(char c: sval) {\n int ci = c - '0';\n freq[ci]++;\n }\n ms.insert(freq);\n }\n\n ll res = 0;\n for(auto freq: ms) {\n ll here = 1;\n ll left = n;\n for(int dig = 0; dig < 10; dig++) {\n if(freq[dig])\n here *=ncr((left - (dig == 0)), freq[dig]);\n\n left -=freq[dig];\n }\n\n res +=here;\n }\n\n return res;\n }\n};",
"memory": "71158"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n void generate(int ind, string s, int& n, int& k, set<vector<int>>& oset){\n if(n%2 && ind>n/2){\n for(int i = ind-2;i>=0;i--){\n s.push_back(s[i]);\n }\n long long num = stoll(s);\n if(num%k==0){\n vector<int> temp(10,0);\n for(int i = 0;i<s.length();i++){\n temp[s[i]-'0']++;\n }\n oset.insert(temp);\n }\n while(s.length()>ind){\n s.pop_back();\n }\n return;\n }\n\n if(n%2==0 && ind==n/2){\n for(int i = ind-1;i>=0;i--){\n s.push_back(s[i]);\n }\n long long num = stoll(s);\n if(num%k==0){\n vector<int> temp(10,0);\n for(int i = 0;i<s.length();i++){\n temp[s[i]-'0']++;\n }\n oset.insert(temp);\n }\n while(s.length()>ind){\n s.pop_back();\n }\n return;\n }\n\n for(int i = 0;i<10;i++){\n if(ind==0 && i==0) continue;\n s.push_back(i+'0');\n generate(ind+1,s,n,k,oset);\n s.pop_back();\n }\n return;\n }\n long long fact[11];\n long long factorial(long long n){\n if(n==1 || n==0) return fact[n] = 1;\n return fact[n] = factorial(n-1)*n;\n }\n long long countGoodIntegers(int n, int k) {\n string s = \"\";\n set<vector<int>> oset;\n generate(0,s,n,k,oset);\n fact[0] = 1;\n // memset(fact,1,sizeof(fact));\n factorial(10);\n // for(int i = 0;i<11;i++){\n // cout<<fact[i]<<\" \";\n // }\n long long ans = 0;\n for(auto vec: oset){\n long long temp1 = 0, mul = 1, sum = 0;\n for(int i =0;i<10;i++){\n mul *= fact[vec[i]];\n sum += vec[i];\n }\n temp1 = fact[sum]/mul;\n if(vec[0]==0){\n ans+=temp1;\n continue;\n }\n mul/= vec[0];\n temp1 -= (fact[sum-1]/mul);\n ans += temp1;\n }\n return ans;\n }\n};",
"memory": "71158"
} |
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>
| 1 | {
"code": "using ll = long long;\n\nclass Solution {\nprivate:\n ll fac[12];\npublic:\n Solution(){\n fac[0] = fac[1] = 1;\n for(ll i=2; i<12; i++) fac[i] = fac[i-1] * i;\n }\n\n ll countGoodIntegers(int n, int k) {\n vector<ll> halfSizeList = getHalfSizeNumbers(n);\n vector<ll> numList = filterHalfSizeList(halfSizeList, k, n);\n set<string> visited;\n ll ans = 0;\n for(auto &num : numList){\n string strNum = to_string(num);\n sort(begin(strNum), end(strNum));\n if( visited.count(strNum) == 0 ){\n visited.insert(strNum);\n ll count = getCombinations(num);\n // cout<<num<<\" :: \"<<count<<endl;\n ans += count;\n }\n }\n return ans;\n }\n\n vector<ll> getHalfSizeNumbers(int n){\n int start = pow(10, (n&1? n/2 : n/2-1));\n int end = pow(10, (n&1? n/2+1 : n/2))-1;\n // cout<<\"Range : \"<<start<<\" - \"<<end<<endl;\n vector<ll> list;\n for(ll i=start; i<=end; i++) list.push_back(i);\n return list;\n }\n\n vector<ll> filterHalfSizeList(vector<ll> &list, ll k, int n){\n vector<ll> newList;\n for(auto &num : list){\n string left = to_string(num);\n string right = (n&1? left.substr(0, n/2) : left);\n reverse(begin(right), end(right));\n string strNum = left + right;\n ll newNum = stoll(strNum);\n if( newNum%k == 0 ) newList.push_back(newNum);\n }\n return newList;\n }\n\n ll getPermutationCount(int digitCount[]){\n /*\n Formula : n! / !k1*!k2*!k3....!kn\n */\n ll totalDigits = 0;\n ll denominator = 1;\n for(int d=0; d<10; d++){\n denominator *= fac[digitCount[d]];\n totalDigits += digitCount[d];\n }\n ll numerator = fac[totalDigits];\n return numerator / denominator;\n }\n\n ll getCombinations(ll num){\n int digitCount[10] = {0};\n for(auto &ch : to_string(num)){\n digitCount[ch-'0'] += 1;\n }\n // Unique Permutaions (numbers) //\n ll count = getPermutationCount(digitCount);\n // Unique Permutations (numbers) with leading zeros //\n ll leadingZero = 0; \n if( digitCount[0] > 0 ){\n digitCount[0] -= 1; // Used at the first place //\n leadingZero = getPermutationCount(digitCount);\n }\n return count - leadingZero;\n }\n\n};",
"memory": "75034"
} |
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>
| 1 | {
"code": "using ll = long long;\n\nclass Solution {\nprivate:\n ll fac[12];\npublic:\n Solution(){\n fac[0] = fac[1] = 1;\n for(ll i=2; i<12; i++) fac[i] = fac[i-1] * i;\n }\n\n ll countGoodIntegers(int n, int k) {\n vector<ll> halfSizeList = getHalfSizeNumbers(n);\n vector<ll> numList = filterHalfSizeList(halfSizeList, k, n);\n set<string> visited;\n ll ans = 0;\n for(auto &num : numList){\n string strNum = to_string(num);\n sort(begin(strNum), end(strNum));\n if( visited.count(strNum) == 0 ){\n visited.insert(strNum);\n ll count = getCombinations(num);\n ans += count;\n }\n }\n return ans;\n }\n\n vector<ll> getHalfSizeNumbers(int n){\n int start = pow(10, (n&1? n/2 : n/2-1));\n int end = pow(10, (n&1? n/2+1 : n/2))-1;\n vector<ll> list;\n for(ll i=start; i<=end; i++) list.push_back(i);\n return list;\n }\n\n vector<ll> filterHalfSizeList(vector<ll> &list, ll k, int n){\n vector<ll> newList;\n for(auto &num : list){\n string left = to_string(num);\n string right = (n&1? left.substr(0, n/2) : left);\n reverse(begin(right), end(right));\n string strNum = left + right;\n ll newNum = stoll(strNum);\n if( newNum%k == 0 ) newList.push_back(newNum);\n }\n return newList;\n }\n\n ll getPermutationCount(int digitCount[]){\n /*\n Formula : n! / !k1*!k2*!k3....!kn\n */\n ll totalDigits = 0;\n ll denominator = 1;\n for(int d=0; d<10; d++){\n denominator *= fac[digitCount[d]];\n totalDigits += digitCount[d];\n }\n ll numerator = fac[totalDigits];\n return numerator / denominator;\n }\n\n ll getCombinations(ll num){\n int digitCount[10] = {0};\n for(auto &ch : to_string(num)){\n digitCount[ch-'0'] += 1;\n }\n // Unique Permutaions (numbers) //\n ll count = getPermutationCount(digitCount);\n // Unique Permutations (numbers) with leading zeros //\n ll leadingZero = 0; \n if( digitCount[0] > 0 ){\n digitCount[0] -= 1; // Used at the first place //\n leadingZero = getPermutationCount(digitCount);\n }\n return count - leadingZero;\n }\n};",
"memory": "75034"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n if(n == 1){\n return 9 / k;\n }\n vector<long long> f(11, 1);\n for(int i = 1; i <= 10; i++) f[i] = f[i - 1] * i;\n set<vector<int>> st;\n auto calc = [&](vector<int> a){\n int n = a.size();\n long long cur = f[accumulate(a.begin(), a.end(), 0)];\n for(auto i : a){\n cur /= f[i];\n }\n return cur;\n };\n auto get = [&](vector<int> a){\n long long ans = calc(a);\n if(a[0] > 0) a[0] -= 1, ans -= calc(a);\n return ans;\n };\n auto calcs = [&](string s){\n long long ss = stoll(s);\n if(ss % k) return 0LL;\n vector<int> a(10);\n int n = s.size();\n for(int i = 0; i < n; i++) a[s[i] - '0'] += 1;\n if(st.count(a)) return 0LL;\n else st.insert(a);\n long long ans = get(a);\n return ans;\n };\n long long ans = 0;\n for(int i = 0; i <= 1E9; i++){\n string s = to_string(i);\n if(s.size() * 2 + (n & 1) > n) break;\n else if(s.size() * 2 + (n & 1) < n) continue;\n if(s[0] == '0') continue;\n string t = s;\n reverse(t.begin(), t.end());\n if(n & 1){\n for(int i = 0; i < 10; i++){\n ans += calcs(s + (char)(48 + i) + t);\n }\n }\n else ans += calcs(s + t);\n }\n return ans;\n }\n};",
"memory": "78910"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n\n struct node{\n struct node *next[11];\n } Node;\n\n struct node head;\n\n void add(){\n struct node *p=&head;\n for (int i=0;i<10;i++){\n if (!p->next[b[i]]) p->next[b[i]]=new node();\n p=p->next[b[i]];\n }\n }\n\n bool check(){\n struct node *p=&head;\n for (int i=0;i<10;i++){\n if (!p->next[b[i]]) return false;\n p=p->next[b[i]];\n }\n return true;\n }\n\n\n\n unordered_set<string> d;\n long long ret=0;\n string s=\"\";\n int nn,kk;\n int f[11];\n int b[10];\n\n void DFS(int l, int r){\n if (l<=r){\n for (char c=(l==0?'1':'0');c<='9';c++){\n if (l==r) b[c-'0']++;\n else b[c-'0']+=2;\n s[l]=s[r]=c;\n DFS(l+1,r-1);\n if (l==r) b[c-'0']--;\n else b[c-'0']-=2;\n }\n }else{\n long long x=stoll(s);\n if (x%kk==0) {\n string c=\"\";\n for (int i=0;i<10;i++) c+=to_string(b[i])+\"|\";\n if (check()) return;\n add();\n int r=nn-b[0];\n for (int i=nn-1;i>0;i--) r*=i;\n for (int i=0;i<10;i++) r/=f[b[i]];\n ret+=r;\n }\n }\n }\n\n\n long long countGoodIntegers(int n, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n for (int i=0;i<n;i++) s+='0';\n for (int i=0;i<10;i++) {\n b[i]=0;\n head.next[i]=0;\n }\n head.next[10]=0;\n f[0]=1;\n for (int i=1;i<11;i++){\n f[i]=i*f[i-1];\n }\n nn=n;\n kk=k;\n DFS(0,n-1);\n return ret;\n }\n};",
"memory": "78910"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\nvector<long long> generatePalindromicNumbers(int n) {\n vector<long long> palindromes;\n int half_len = (n + 1) / 2;\n long long start = pow(10, half_len - 1);\n long long end = pow(10, half_len) - 1;\n\n for (long long i = start; i <= end; ++i) {\n long long first_half = i;\n long long palindrome = first_half;\n\n if (n % 2 == 1) {\n first_half /= 10;\n }\n\n while (first_half > 0) {\n palindrome = palindrome * 10 + first_half % 10;\n first_half /= 10;\n }\n\n palindromes.push_back(palindrome);\n }\n\n return palindromes;\n}\n\nlong long fact(int n) {\n if (n <= 1) return 1;\n long long result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n}\n\nlong long countPermutations(const string& s) {\n map<char, int> freq;\n int n = s.size();\n\n // Calculate frequencies of each character\n for (char c : s) {\n freq[c]++;\n }\n\n // Calculate the total permutations\n long long ans = fact(n);\n for (const auto& [_, count] : freq) {\n ans /= fact(count);\n }\n\n // Handle the case where '0' is present\n if (freq['0'] > 0) {\n long long zero_adjusted = fact(n - 1);\n freq['0']--;\n\n for (const auto& [_, count] : freq) {\n zero_adjusted /= fact(count);\n }\n\n ans -= zero_adjusted;\n }\n\n return ans;\n}\n\nlong long countGoodIntegers(int n, int k) {\n long long totalCount = 0;\n vector<long long> palindromes = generatePalindromicNumbers(n);\n set<string> seenPermutations;\n\n for (auto num : palindromes) {\n if (num % k == 0) {\n string temp = to_string(num);\n sort(temp.begin(), temp.end());\n if (seenPermutations.find(temp) == seenPermutations.end()) {\n totalCount += countPermutations(to_string(num));\n seenPermutations.insert(temp);\n }\n }\n }\n\n return totalCount;\n}\n};\n\n\n",
"memory": "82786"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n map<vector<int>,int> lookup;\n long long result = 0;\n long long getNum(vector<int> &num){\n long long res = 0;\n long long tenPow = 1;\n for(int i=num.size()-1;i>=0;i--){\n res+=(num[i])*tenPow;\n tenPow*=10;\n }\n return res;\n }\n long long getPerm(vector<int> &curNum){\n map<int,int> mp;\n int sz = curNum.size();\n long long len = sz;\n for(auto val: curNum){\n mp[val]++;\n }\n long long nr = 1;\n long long dr = 1;\n while(sz>0){\n nr*=(long long)sz;\n sz--;\n }\n for(auto p: mp){\n long long freq = p.second;\n long long pdct=1;\n while(freq>0){\n pdct*=freq;\n freq--;\n }\n dr*=pdct;\n \n }\n long long ans1 = nr/dr;\n long long zeroCnt = (mp.count(0)==0)?(0):(mp[0]);\n if(zeroCnt == 0){\n return ans1;\n }\n sz = len-1;\n nr = 1;\n dr = 1;\n while(sz>0){\n nr*=(long long)sz;\n sz--;\n }\n mp[0]--;\n for(auto p: mp){\n long long freq = p.second;\n long long pdct=1;\n while(freq>0){\n pdct*=freq;\n freq--;\n }\n dr*=pdct;\n \n }\n long long ans2 = (nr/dr);\n return (ans1-ans2);\n }\n void solve(int n,int k,int idx1,int idx2,vector<int> &curNum){\n if(idx1>idx2){\n long long newNum = getNum(curNum);\n if(newNum%k==0){\n //cout<<newNum<<\" is divisible by\"<<k<<endl;\n vector<int> frequency(10,0);\n for(int iter=0;iter<curNum.size();iter++){\n frequency[curNum[iter]]++;\n }\n if(lookup.count(frequency)==0){\n result+=getPerm(curNum);\n lookup[frequency] = 1;\n }\n // cout<<getPerm(curNum)<<\" is hte permCnt for\"<<newNum<<endl;\n }\n return;\n }\n int startDig = (idx1==0)?(1):(0);\n for(;startDig<10;startDig++){\n curNum[idx1]=startDig;\n curNum[n-1-idx1]=startDig;\n solve(n,k,idx1+1,idx2-1,curNum);\n }\n \n \n }\n long long countGoodIntegers(int n, int k) {\n vector<int> curNum(n,-1);\n solve(n,k,0,n-1,curNum);\n return result;\n \n }\n};",
"memory": "82786"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n vector<string> palindromes(int d,int k)\n {\n vector<string> ans;\n \n int t = (d + 1) / 2;\n\n long long smaller = pow(10, t - 1); \n long long largest = pow(10, t) - 1;\n string w = \"\";\n for (long long i = smaller; i <= largest; i++) {\n string temp = to_string(i);\n string s = temp;\n reverse(s.begin(), s.end());\n\n if (d % 2 == 0) {\n w = temp+s;\n } else {\n w = temp + s.substr(1);\n }\n\n long long y = stoll(w);\n if(y%k == 0){\n ans.push_back(w);\n }\n }\n\n return ans;\n }\n \n long long fact(long long n)\n {\n if (n == 0)\n return 1;\n return n * fact(n - 1);\n }\n long long countGoodIntegers(int n, int k) {\n vector<string> p = palindromes(n,k);\n \n map<string,int>mp1;\n long long ans = 0;\n for(int i = 0;i<p.size();i++){\n map<char,int>mp;\n string s = p[i];\n sort(s.begin(),s.end());\n if(mp1[s] == 1){\n continue;\n }\n mp1[s] = 1;\n for(int j = 0;j<s.size();j++){\n mp[s[j]]++;\n }\n\n long long num = 1;\n for(auto it : mp){\n if(it.second > 1){\n num *= fact(it.second);\n }\n }\n\n long long t = 0;\n long long sum = fact(s.size()-1);\n for(auto it : mp){\n if(it.first != '0'){\n if(it.second == 1){\n ans += sum/num;\n }\n else{\n long long h = fact(it.second);\n long long m = fact(it.second-1);\n\n ans += (sum * h)/(num*m);\n\n }\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "86663"
} |
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>
| 1 | {
"code": "class Solution {\nprivate:\n long long* factorial;\n\npublic:\n Solution() {\n factorial =\n new long long[11]{1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};\n }\n\n ~Solution() { delete[] this->factorial; }\n\n pair<long long, long long> findPermutations(long long n, long long num) {\n if (n == 1) {\n return {1, num};\n }\n\n vector<long long> fm(10, 0);\n\n while (num > 0) {\n long long digit = num % 10;\n\n fm[digit]++;\n\n num /= 10;\n }\n\n long long wiz_nu = this->factorial[n];\n long long wiz_de = 1;\n\n long long ten_pow = 1;\n long long ret = 0;\n\n for (long long i = 0; i < 10; i++) {\n wiz_de *= this->factorial[fm[i]];\n\n for (long long j = 0; j < fm[i]; j++) {\n ret += ten_pow * i;\n ten_pow *= 10;\n }\n }\n\n long long wiz = wiz_nu / wiz_de;\n\n if (fm[0] == 0) {\n return {wiz, ret};\n }\n\n fm[0]--;\n long long woz_nu = this->factorial[n - 1];\n long long woz_de = 1;\n\n for (int i = 0; i < 10; i++) {\n woz_de *= this->factorial[fm[i]];\n }\n\n long long woz = woz_nu / woz_de;\n\n long long ans = wiz - woz;\n\n return {ans, ret};\n }\n\n long long countGoodIntegers(int n, int k) {\n long long ans = 0;\n\n long long h = n / 2;\n if (n & 1) h++;\n\n long long ten_pow = pow(10, h - 1);\n long long num = ten_pow;\n\n unordered_map<long long, bool> m;\n while (num / (ten_pow * 10) == 0) {\n long long pal = num;\n long long tn = num;\n\n if (n & 1) tn /= 10;\n\n while (tn > 0) {\n long long digit = tn % 10;\n pal *= 10;\n pal += digit;\n\n tn /= 10;\n }\n\n\n if (pal % k == 0) {\n auto p = findPermutations(n, pal);\n if (!m[p.second]) {\n ans += p.first;\n m[p.second] = true;\n }\n }\n\n num++;\n }\n \n return ans;\n }\n};",
"memory": "86663"
} |
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>
| 1 | {
"code": "using ll=long long;\n\nclass Solution {\npublic:\n ll fact[11];\n unordered_map<string,int>vis;\n // ll getTotalPer(string &s){\n // int n=s.size();\n // ll total=fact[n];\n // vector<int>freq(10,0);\n \n \n // for(int i=0;i<n;i++){\n // freq[s[i]-'0']++;\n // }\n // string temp=\"\";\n // for(int i=0;i<10;i++){\n // int cnt=freq[i];\n // temp+=('0'+ cnt);\n // if(cnt==0)continue;\n // total=total/fact[cnt];\n // }\n // if(map.find(temp)!=map.end())return 0;\n // map.insert({temp,1});\n // if(freq[0]==0)return total;\n\n \n // ll a = fact[n-1]; \n // a = a/fact[freq[0]-1];\n\n // for(int i=0;i<10;i++){\n // int cnt=freq[i];\n \n // if(cnt==0)continue;\n // a=a/fact[cnt];\n // }\n\n // total=total-a;\n // return total;\n \n // }\n \nll count(string &s) // using basic combinatorics to count all combinations.\n{\n vector<ll> fr(10, 0);\n \n ll n = s.length();\n \n if(n == 1)\n return 1;\n\n for(int i=0; i<s.length(); ++i)\n {\n ll curr = s[i] - '0';\n fr[curr]++;\n }\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 a = fact[n-1];\n a = a/fact[fr[0]-1];\n\n for(int i=1; i<fr.size(); ++i)\n {\n ll curr = fr[i];\n \n if(curr != 0)\n a = a/fact[curr];\n }\n \n ll res = tot-a;\n return res;\n}\n\n\n ll getCount(string s,int n,int k){\n string temp=s.substr(0,n/2);\n reverse(begin(temp),end(temp));\n s+=temp;\n ll num=stol(s);\n if(num%k!=0)return 0;\n return count(s);\n \n }\n// unordered_set<int>set;\n ll solve(int idx,string &s,int n,int k)\n {\n if(idx>=((n%2==0) ? n/2 : (n/2 + 1)))return getCount(s,n,k);\n ll ans=0;\n for(int j=0;j<=9;j++){\n if(idx==0 && j==0)continue;\n s[idx]=char(j+'0');\n ans+=solve(idx+1,s,n,k);\n \n \n }\n return ans;\n }\n ll countGoodIntegers(int n, int k) {\n //fint factorial\n \n \n fact[0]=1;fact[1]=1;\n for(int i=2;i<=10;i++)fact[i]=fact[i-1]*i;\n string s((n%2==0) ? n/2 : (n/2 + 1),'0');\n \n return solve(0,s,n,k);\n }\n};",
"memory": "90539"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n long long ans;\n vector<long long> fact;\n unordered_map<string, int> vis;\n long long count(string& s) {\n vector<long long> fr(10, 0);\n\n long long n = s.length();\n\n if (n == 1)\n return 1;\n\n for (int i = 0; i < s.length(); ++i) {\n long long curr = s[i] - '0';\n fr[curr]++;\n }\n\n long long tot = fact[n];\n string str = \"\"; // digit frequency string.\n\n for (int i = 0; i < fr.size(); ++i) {\n long long 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\n // frequency multiple times.\n return 0;\n\n vis[str] = 1;\n\n if (fr[0] == 0)\n return tot;\n\n long long a = fact[n - 1];\n a = a / fact[fr[0] - 1];\n\n for (int i = 1; i < fr.size(); ++i) {\n long long curr = fr[i];\n\n if (curr != 0)\n a = a / fact[curr];\n }\n\n long long res = tot - a;\n return res;\n }\n\n void sol(string s, int n, int k, vector<long long>& fact) {\n if (s.length() == (n + 1) / 2) {\n string t = s;\n if (n % 2)\n t.pop_back();\n reverse(t.begin(), t.end());\n s = s + t;\n long long temp = stoll(s);\n if (temp % k == 0)\n ans += count(s);\n return;\n }\n\n for (int i = 0; i <= 9; i++) {\n s += (i + '0');\n sol(s, n, k, fact);\n s.pop_back();\n }\n }\n long long countGoodIntegers(int n, int k) {\n ans = 0;\n \n if (n == 1) {\n int cnt=0;\n for(int i=1;i<=9;i++)if(i%k==0)cnt++;\n return cnt;\n }\n\n fact.resize(11);\n fact[0] = 1;\n for (int i = 1; i <= 10; i++) {\n fact[i] = fact[i - 1] * i;\n }\n\n for (int i = 1; i <= 9; i++) {\n string s = \"\";\n s += (i + '0');\n sol(s, n, k, fact);\n }\n return ans;\n }\n};",
"memory": "90539"
} |
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>
| 2 | {
"code": "#include \"bits/stdc++.h\"\nusing namespace std;\n\nclass Solution {\n public:\n vector<long long> generate(int n, int k) {\n vector<long long> pos;\n if (n == 0) return pos;\n long long start = pow(10, (n + 1) / 2 - 1);\n long long end = pow(10, (n + 1) / 2) - 1;\n if (n == 1) start = 0;\n\n for (long long num = start; num <= end; num++) {\n string str = to_string(num);\n string res;\n\n if (n % 2 == 0) {\n string opposite = str;\n reverse(opposite.begin(), opposite.end());\n res = str + opposite;\n } else {\n string opposite = str.substr(0, str.size() - 1);\n reverse(opposite.begin(), opposite.end());\n res = str + opposite;\n }\n\n if (res.size() == n) {\n long long num = stoll(res);\n if (num % k == 0) pos.push_back(num);\n }\n }\n return pos;\n }\n\n long long countGoodIntegers(int n, int k) {\n long long fac[11];\n fac[0] = 1;\n for (int i = 1; i <= 10; i++) {\n fac[i] = fac[i - 1] * i;\n }\n vector<long long> pos = generate(n, k);\n set<vector<int>> st;\n for (auto num : pos) {\n vector<int> freq(10, 0);\n string str = to_string(num);\n while (str.size() < n) str = \"0\" + str;\n for (char dig : str) freq[dig - '0']++;\n st.insert(freq);\n }\n\n long long ans = 0;\n for (auto &num : st) {\n for (int i = 1; i <= 9; i++) {\n if (num[i] == 0) continue;\n vector<int> freq = num;\n freq[i]--;\n bool isValid = true;\n for (int freq : freq) {\n if (freq < 0) {\n isValid = false;\n break;\n }\n }\n if (!isValid) continue;\n long long pos = fac[n - 1];\n for (int j = 0; j < 10; j++) {\n pos /= fac[freq[j]];\n }\n ans += pos;\n }\n }\n return ans;\n }\n};\n",
"memory": "94415"
} |
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>
| 2 | {
"code": "\n\nclass Solution\n{\npublic:\n long long countGoodIntegers(int n, int k)\n {\n long long fact[11];\n fact[0] = 1;\n for (int i = 1; i <= 10; i++)\n {\n fact[i] = fact[i - 1] * i;\n }\n if (n == 0)\n {\n return 0;\n }\n if (n == 1)\n {\n if (k == 1)\n {\n return 9;\n }\n if (k == 2)\n {\n return 4;\n }\n if (k == 3)\n {\n return 3;\n }\n if (k == 4)\n {\n return 2;\n }\n return 1;\n }\n vector<long long> temp;\n int half_length = (n + 1) / 2;\n long long start = pow(10, half_length - 1);\n long long end = pow(10, half_length) - 1;\n\n for (long long i = start; i <= end; i++)\n {\n string first = to_string(i);\n string str;\n if (n % 2 == 0)\n {\n string second = first;\n reverse(second.begin(), second.end());\n str = first + second;\n }\n else\n {\n string second = first.substr(0, first.size() - 1);\n reverse(second.begin(), second.end());\n str = first + second;\n }\n if (str.size() != n)\n {\n continue;\n }\n\n long long num = stoll(str);\n if (num % k)\n {\n continue;\n }\n temp.push_back(num);\n }\n set<vector<int>> ans;\n for (auto num : temp)\n {\n vector<int> counts(10, 0);\n string s = to_string(num);\n while (s.size() < n)\n {\n s = \"0\" + s;\n }\n for (char c : s)\n counts[c - '0']++;\n ans.insert(counts);\n }\n long long result = 0;\n for (auto &counts : ans)\n {\n for (int d = 1; d <= 9; d++)\n {\n if (counts[d] == 0)\n {\n continue;\n }\n vector<int> new_counts = counts;\n new_counts[d]--;\n long long answer = fact[n - 1];\n for (int i = 0; i < 10; i++)\n {\n answer /= fact[new_counts[i]];\n }\n result += answer;\n }\n \n }\n return result;\n }\n};",
"memory": "94415"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n vector <int> fact (int n)\n {\n vector <int> ans (n+1, 1);\n for (int i = 2; i <= n; i++) ans[i] = i*ans[i-1];\n return ans;\n }\n \n long long countGoodIntegers(int n, int k)\n {\n int n2 = n/2 + n%2;\n int max = pow (10, n2)-1;\n vector <int> fac = fact (10);\n\n long long ans = 0;\n unordered_map <string, bool> map;\n for (int i = 1; i <= max; i++)\n {\n long long num = i;\n long long p = pow (10, n-1);\n for (int j = n, cpy = i; j > n2; j--, cpy /= 10, p /= 10) num += (cpy%10)*p;\n if (num%k != 0 or num%10 == 0)continue;\n\n vector <int> vec (10);\n for (int j = n, cpy = i; j > n2-n%2; j--, cpy/= 10)\n {\n if (j != n2) vec[cpy%10]+=2;\n else vec[cpy]++;\n }\n\n string st = \"\";\n for (int i = 0; i < 10; i++)\n {\n st += '0'+i;\n st += '0'+vec[i];\n }\n if (map[st]) continue;\n else map[st] = true;\n\n int tmp = fac[n];\n for (int j = 0; j < 10; j++) tmp /= fac[vec[j]];\n if (vec[0] > 0)\n {\n int tmp2 = fac[n-1]/fac[vec[0]- 1];\n for (int j = 1; j < 10; j++) tmp2 /= fac[vec[j]];\n tmp -= tmp2;\n }\n ans += tmp;\n }\n return ans;\n }\n};",
"memory": "98291"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n vector <int> fact (int n)\n {\n vector <int> ans (n+1, 1);\n for (int i = 2; i <= n; i++) ans[i] = i*ans[i-1];\n return ans;\n }\n \n long long countGoodIntegers(int n, int k)\n {\n int n2 = n/2 + n%2;\n int max = pow (10, n2)-1;\n vector <int> fac = fact (10);\n\n long long ans = 0;\n unordered_map <string, bool> map;\n for (int i = 1; i <= max; i++)\n {\n long long num = i;\n long long p = pow (10, n-1);\n for (int j = n, cpy = i; j > n2; j--, cpy /= 10, p /= 10) num += (cpy%10)*p;\n if (num%k != 0 or num%10 == 0)continue;\n\n vector <int> vec (10);\n for (int j = n, cpy = i; j > n2-n%2; j--, cpy/= 10)\n {\n if (j != n2) vec[cpy%10]+=2;\n else vec[cpy]++;\n }\n\n string st = \"\";\n for (int i = 0; i < 10; i++)\n {\n st += '0'+i;\n st += '0'+vec[i];\n }\n if (map[st]) continue;\n else map[st] = true;\n\n int tmp = fac[n];\n for (int j = 0; j < 10; j++) tmp /= fac[vec[j]];\n if (vec[0] > 0)\n {\n int tmp2 = fac[n-1]/fac[vec[0]- 1];\n for (int j = 1; j < 10; j++) tmp2 /= fac[vec[j]];\n tmp -= tmp2;\n }\n ans += tmp;\n }\n return ans;\n }\n};",
"memory": "98291"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<long long> pow(10, 1);\n vector<long long> fact(11, 1);\n for(int i = 1; i < 10; ++i) pow[i] = 1LL * 10 * pow[i-1];\n for(int i = 1; i <= 10; ++i) fact[i] = 1LL * i * fact[i-1];\n int end = pow[n/2], middle = pow[(n+1)/2];\n long long ans = 0;\n unordered_set<long long> perms;\n if(n % 2 == 0) {\n for(int i = 1; i < end; ++i) {\n long long num = 0;\n int x = i;\n vector<int> freq(10, 0);\n for(int j = 0; j < n/2; ++j) {\n num = num * 10 + x % 10;\n freq[x%10] += 2;\n x/=10;\n }\n num = num * middle + i;\n if(num % k != 0 || num < pow[n-1]) continue;\n long long hash = 0;\n bool ten = false;\n for(int j = 0; j < 10; ++j) {\n hash += 1LL * pow[j] * freq[j];\n if(freq[j] == 10) ten = true;\n }\n if(ten) ans += 1;\n else perms.insert(hash);\n // if(num % 11111 == 0)\n // cout << num << \" \" << hash << \"\\n\";\n }\n } else {\n for(int i = 0; i < end; ++i) {\n long long num = 0;\n int x = i;\n vector<int> freq(10, 0);\n for(int j = 0; j < n/2; ++j) {\n num = num * 10 + x % 10;\n freq[x%10] += 2;\n x/=10;\n }\n num = num * middle + i;\n for(int c = 0; c < 10; ++c) {\n freq[c]++;\n num += c * end;\n if(num % k != 0 || num < pow[n-1]) {\n freq[c]--;\n num -= c * end;\n continue;\n }\n long long hash = 0;\n for(int j = 0; j < 10; ++j) {\n hash += 1LL * pow[j] * freq[j];\n }\n perms.insert(hash);\n // cout << num << \" \" << hash << \"\\n\";\n freq[c]--;\n num -= c * end;\n }\n }\n }\n // cout << perms.size() << \"\\n\";\n for(const long long &hash : perms) {\n int zeroes = hash % 10;\n // cout << zeroes << \" \" << nonzeros << \"\\n\";\n long long sum = 1LL * fact[n], sub = 1LL * fact[n-1];\n long long x = hash;\n for(int i = 0; i < 10; ++i) {\n int r = x % 10;\n sum /= fact[r];\n sub /= i == 0 && r > 0 ? fact[r - 1] : fact[r];\n x /= 10;\n }\n if(zeroes > 0)\n sum -= sub;\n // cout << hash << \" \" << sum << \"\\n\";\n ans += sum;\n }\n return ans;\n }\n};",
"memory": "102168"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n \n long long factorial[12];\n \n int N, K;\n \n bool Valid(vector<int>& counter) {\n if (counter[0] == 0 || counter[0] % 2) return true;\n for (int idx = 1; idx < 10; ++idx) {\n if (counter[idx] && counter[idx] % 2 == 0) {\n return true;\n }\n }\n return false;\n }\n \n set<string> visited;\n set<int> v;\n \n long long recur(string& curr) {\n // cout << \"recur: \" << curr << \"\\n\";\n if (curr.size() * 2 >= N) {\n if (curr.back() == '0') {\n return 0;\n }\n string rev = curr;\n reverse(rev.begin(), rev.end());\n if (curr.size() * 2 > N) {\n rev.pop_back();\n }\n auto fin = rev + curr;\n long long num = stoll(fin);\n // cout << \" num: \" << num << \"\\n\";\n if (num % K) {\n return 0;\n }\n v.insert(num);\n \n vector<int> counter(10, 0);\n for (int idx = 0; idx < fin.size(); ++idx) {\n counter[fin[idx] - '0'] += 1;\n }\n // if (!Valid(counter)) {\n // // cout << \"Invalid: \" << fin << \"\\n\";\n // return 0;\n // }\n long long cnt = factorial[fin.size()];\n for (int idx = 0; idx < 10; ++idx) {\n cnt /= factorial[counter[idx]];\n }\n // cout << curr << \" - \" << fin << \" init: \" << cnt << \"\\n\";\n if (counter[0]) {\n long long sub = factorial[fin.size() - 1];\n counter[0] -= 1;\n for (int idx = 0; idx < 10; ++idx) {\n sub /= factorial[counter[idx]];\n }\n cnt -= sub;\n // cout << \"Sub: \" << sub << \"\\n\";\n }\n \n if (cnt != 0) {\n string sorted = curr;\n if (curr.size() * 2 > N) {\n auto first = curr.substr(0, 1);\n sorted = curr.substr(1);\n sort(sorted.begin(), sorted.end());\n sorted = first + sorted;\n } else {\n sort(sorted.begin(), sorted.end());\n }\n\n if (visited.count(sorted)) {\n // cout << \"Skip: \" << num << \"\\n\";\n return 0;\n }\n visited.insert(sorted);\n }\n \n // cout << \"FIN: \" << fin << \" cnt: \" << cnt << \"\\n\";\n return cnt;\n }\n long long res = 0;\n int start = curr.empty() ? 0 : curr.back() - '0';\n for (start = 0 ;start < 10; ++start) {\n // cout << curr << \" start: \" << start << \"\\n\";\n curr.push_back(start + '0');\n res += recur(curr);\n curr.pop_back();\n }\n return res;\n }\n \n long long countGoodIntegers(int n, int k) {\n N = n;\n K = k;\n \n factorial[0] = 1;\n factorial[1] = 1;\n for (int idx = 2; idx < 12; ++idx) {\n factorial[idx] = idx * factorial[idx - 1];\n }\n string curr;\n auto r = recur(curr);\n // for (auto el: v) cout << el << \"\\n\";\n // cout << v.size();\n return r;\n }\n};",
"memory": "102168"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n long long factorial(int n) {\n long long res = 1;\n for (int i = 2; i <= n; ++i)\n res *= i;\n return res;\n }\n // int pow(int k){\n // int c=1;\n // while(k>0){\n // c = c*10;\n // k--;\n // }\n // return c;\n // }\n vector<int> digits(long num){\n vector<int> ret(10,0);\n while(num>0){\n ret[num%10]++;\n num = num/10;\n }\n return ret;\n }\n long long cnt(vector<int> dig,set<vector<int>> &s){\n if(s.find(dig)!=s.end()) return 0;\n s.insert(dig);\n int n = accumulate(dig.begin(), dig.end(), 0);\n long long totalPermutations = factorial(n);\n for (int i = 0; i < 10; ++i) {\n totalPermutations /= factorial(dig[i]);\n }\n if (dig[0] > 0) {\n // Decrease count of digit '0'\n dig[0]--;\n long long invalidPermutations = factorial(n - 1);\n for (int i = 0; i < 10; ++i) {\n invalidPermutations /= factorial(dig[i]);\n }\n totalPermutations -= invalidPermutations;\n }\n\n return totalPermutations;\n }\n long long countGoodIntegers(int n, int k) {\n long long ans=0;\n set<vector<int>> s;\n if(n%2==0){\n int m = n/2;\n long long z = pow(10,m)-1;\n long long z2 = pow(10,m-1);\n for(int i=z;i>=z2;i--){\n int rev = 0, temp = i;\n while (temp > 0) {\n rev = rev * 10 + temp % 10;\n temp /= 10;\n }\n long number = i * pow(10, m) + rev;\n if(number%k==0){\n vector<int> temp = digits(number);\n ans+= cnt(temp,s);\n }\n }\n }\n else{\n int m = (n+1)/2;\n long z = pow(10,m)-1;\n long z2 = pow(10,m-1);\n for(int i=z;i>=z2;i--){\n int rev = 0, temp = i / 10; // Omit middle digit\n while (temp > 0) {\n rev = rev * 10 + temp % 10;\n temp /= 10;\n }\n long number = i * pow(10, m - 1) + rev;\n if(number%k==0){\n vector<int> temp = digits(number);\n ans+= cnt(temp,s);\n }\n }\n }\n return ans;\n }\n};",
"memory": "106044"
} |
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>
| 2 | {
"code": "#define ll long long\n\nclass Solution\n{\npublic:\n ll factorial[12];\n\n ll count_ways(vector<int> counts, int n)\n {\n ll total = 0;\n for (int d = 1; d <= 9; d++)\n {\n if (counts[d] == 0)\n continue;\n vector<int> temp = counts;\n temp[d]--;\n ll arrangements = factorial[n - 1];\n for (int i = 0; i < 10; i++)\n {\n arrangements /= factorial[temp[i]];\n }\n total += arrangements;\n }\n return total;\n }\n\n long long countGoodIntegers(int n, int k)\n {\n factorial[0] = 1;\n for (int i = 1; i <= 11; i++)\n {\n factorial[i] = factorial[i - 1] * i;\n }\n\n vector<long long> palindromes;\n int countDivisibleByK = 0;\n\n int half_length = (n + 1) / 2;\n int start = pow(10, half_length - 1);\n int end = pow(10, half_length);\n if (n == 1)\n {\n start = 0;\n }\n\n for (int i = start; i < end; i++)\n {\n string half_str = to_string(i);\n string palindrome;\n\n if (n % 2 == 0)\n {\n palindrome = half_str + string(half_str.rbegin(), half_str.rend());\n }\n else\n {\n palindrome = half_str + string(half_str.rbegin() + 1, half_str.rend());\n }\n\n if (palindrome.size() == n)\n {\n long long num = stoll(palindrome);\n if (num % k == 0)\n {\n palindromes.push_back(num);\n }\n }\n }\n\n set<vector<int>> ans;\n for (auto it : palindromes)\n {\n vector<int> counts(10, 0);\n string s = to_string(it);\n while (s.size() < n)\n s = \"0\" + s;\n for (char c : s)\n counts[c - '0']++;\n ans.insert(counts);\n }\n\n long long result = 0;\n for (auto it : ans)\n {\n result += count_ways(it, n);\n }\n\n return result;\n }\n};\n",
"memory": "106044"
} |
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>
| 2 | {
"code": "#define MAX_DIGITS 11\n#define ll long long\n#define vi std::vector<int>\n#define vll std::vector<ll>\n\nclass Solution {\npublic:\n ll factorialArr[MAX_DIGITS];\n \n Solution() {\n factorialArr[0] = 1;\n for (int i = 1; i < MAX_DIGITS; i++) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n factorialArr[i] = factorialArr[i - 1] * i;\n for(int i = 0; i<1; i++){int d=0;d++;}\n }\n }\n\n vll generatePalindromes(int digitCount) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n vll palindromes;\n for(int i = 0; i<1; i++){int d=0;d++;}\n if (digitCount == 0) return palindromes;\n for(int i = 0; i<1; i++){int d=0;d++;}\n int halfLen = (digitCount + 1) / 2;\n for(int i = 0; i<1; i++){int d=0;d++;}\n ll start = pow(10, halfLen - 1);\n ll end = pow(10, halfLen) - 1;\n if (digitCount == 1) start = 0;\n for(int i = 0; i<1; i++){int d=0;d++;} \n for (ll firstHalf = start; firstHalf <= end; firstHalf++) {\n string halfStr = to_string(firstHalf);\n for(int i = 0; i<1; i++){int d=0;d++;}\n string palindrome = halfStr + string(halfStr.rbegin() + (digitCount % 2), halfStr.rend());\n palindromes.push_back(stoll(palindrome));\n for(int i = 0; i<1; i++){int d=0;d++;}\n }\n for(int i = 0; i<1; i++){int d=0;d++;}\n return palindromes;\n }\n\n vi countDigits(ll number, int digitCount) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n vi digitFreq(10, 0);\n string numStr = to_string(number);\n for(int i = 0; i<1; i++){int d=0;d++;}\n numStr.insert(numStr.begin(), digitCount - numStr.size(), '0');\n for(int i = 0; i<1; i++){int d=0;d++;}\n for (char digit : numStr) digitFreq[digit - '0']++;\n for(int i = 0; i<1; i++){int d=0;d++;}\n return digitFreq;\n }\n\n ll calculatePermutations(vi digitFreq, int digitCount) {\n ll permutations = 0;\n for(int i = 0; i<1; i++){int d=0;d++;}\n for (int digit = 1; digit <= 9; digit++) {\n if (digitFreq[digit] == 0) continue;\n for(int i = 0; i<1; i++){int d=0;d++;}\n digitFreq[digit]--;\n ll validPermutations = factorialArr[digitCount - 1];\n for (int count : digitFreq) validPermutations /= factorialArr[count];\n for(int i = 0; i<1; i++){int d=0;d++;}\n permutations += validPermutations;\n for(int i = 0; i<1; i++){int d=0;d++;}\n digitFreq[digit]++;\n }\n for(int i = 0; i<1; i++){int d=0;d++;}\n return permutations;\n }\n\n ll countGoodIntegers(int digitCount, int divisor) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n vll palindromes = generatePalindromes(digitCount);\n for(int i = 0; i<1; i++){int d=0;d++;}\n set<vi> uniqueDigitFreq;\n for(int i = 0; i<1; i++){int d=0;d++;}\n for (ll palindrome : palindromes) {\n if (palindrome % divisor == 0) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n uniqueDigitFreq.insert(countDigits(palindrome, digitCount));\n }\n }\n for(int i = 0; i<1; i++){int d=0;d++;}\n ll totalGoodIntegers = 0;\n for(int i = 0; i<1; i++){int d=0;d++;}\n for (const vi& digitFreq : uniqueDigitFreq) {\n for(int i = 0; i<1; i++){int d=0;d++;}\n totalGoodIntegers += calculatePermutations(digitFreq, digitCount);\n }\n for(int i = 0; i<1; i++){int d=0;d++;}\n return totalGoodIntegers;\n }\n};\n",
"memory": "109920"
} |
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>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\nll ncr(ll a,ll b){\n ll ans=1;\n for(ll i=b+1;i<=a;i++)ans*=i;\n for(ll i=1;i<=a-b;i++)ans/=i;\n return ans;\n}\nll calc_bgd(vector<ll>fr){\n vector<ll>v;\n ll sum=0;\n ll ans=1;\n for(ll it:fr)if(it)v.push_back(it),sum+=it;\n for(ll it:v){\n ans*=ncr(sum,it);\n sum-=it;\n }\n return ans;\n}\nvector<ll> fact;\nunordered_map<string, int> vis; // visited map\nll count(string &s) // using basic combinatorics to count all combinations.\n{\n vector<ll> fr(10, 0);\n \n ll n = s.length();\n \n if(n == 1)\n return 1;\n\n for(int i=0; i<s.length(); ++i)\n {\n ll curr = s[i] - '0';\n fr[curr]++;\n }\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 tot=1;\n int sum=0;\n for(int it:fr)sum+=it;\n for(int it:fr)tot*=ncr(sum,it),sum-=it;\n if(fr[0] == 0)\n return tot;\n ll res=0;\n for(int i=1;i<10;i++){\n if(fr[i]>0){\n fr[i]--;\n res+=calc_bgd(fr);\n fr[i]++;\n }\n }\n return res;\n}\nll calc(string s){\n vector<ll>fr(10);\n for(char it:s)fr[it-'0']++;\n ll ans=0;\n for(ll i=1;i<10;i++){\n if(fr[i]){\n fr[i]--;\n ans+=calc_bgd(fr);\n fr[i]++;\n }\n }\n return ans;\n\n}\n long long countGoodIntegers(int n, int k) {\n fact.push_back(1);\n ll curr = 1;\n \n for(ll i=2; i<=11; ++i)\n {\n fact.push_back(curr);\n curr = curr*i;\n }\n \n bool ch=n%2;\n ll dgt=(n+1)/2;\n ll ans=0;\n\n for(ll i=pow(10,dgt-1);i<pow(10,dgt);i++){\n string s=to_string(i);\n int p=s.size()-ch-1;\n while(p>=0)s.push_back(s[p]),p--;\n ll pp=1;\n reverse(s.begin(),s.end());\n ll val=0;\n for(int i=0;i<s.size();i++){\n val+=1LL*pp*(s[i]-'0');\n pp*=10LL;\n }\n if(val%k==0){\n ans+=count(s);\n }\n }\n return ans;\n }\n};",
"memory": "109920"
} |
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>
| 2 | {
"code": "#define ll long long\n#define ld long double\nclass Solution {\n ll ans;\n vector<ll> fact;\n unordered_map<string, int> vis; // visited map\n\n string genpal(ll num, ll val) // generating palindrome corresponding to the first half generated.\n {\n string s = to_string(num);\n string t = s.substr(0, val);\n reverse(t.begin(), t.end());\n \n s += t;\n return s;\n }\n\n bool check(string &s, int k) // divisibility check. \n {\n ll val = 0;\n\n for(int i = 0; i<s.length(); ++i){\n ll curr = s[i] - '0';\n val = val*10 + curr;\n }\n \n if(val%k == 0)\n return true;\n \n return false;\n }\n\n\n ll count(string &s) // using basic combinatorics to count all combinations.\n {\n vector<ll> fr(10, 0);\n ll n = s.length();\n \n if(n == 1){\n return 1;\n }\n\n for(int i=0; i<s.length(); ++i){\n ll curr = s[i] - '0';\n fr[curr]++;\n }\n \n ll tot = fact[n];\n string str = \"\"; // digit frequency string.\n \n for(int i=0; i<fr.size(); ++i) {\n ll curr = fr[i];\n str += ('a' + curr);\n \n if(curr != 0){\n tot = tot/fact[curr];\n }\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 if(fr[0] == 0){\n return tot;\n }\n \n ll a = fact[n-1];\n a = a/fact[fr[0]-1];\n\n for(int i=1; i<fr.size(); ++i){\n ll curr = fr[i];\n \n if(curr != 0){\n a = a/fact[curr];\n }\n }\n \n ll res = tot-a;\n return res;\n }\n\n\n void find(int pos, int num, int len, int stat, int k) // generating all numbers of length len. \n {\n \n if(pos == len ){\n ll val;\n \n if(stat == 1){ // stat = 0 => even n || stat = 1 => n is odd\n val = len-1; \n }\n else if(stat == 0){\n val = len;\n }\n \n string s = genpal(num, val); // generating palindrome.\n bool st = check(s, k); // check divisibility by k\n \n if(st){ // if yes count all possible combinations\n ans += count(s); \n }\n return;\n }\n\n if(pos == 0)\n {\n for(int i=1; i<=9; ++i)\n {\n int tnum = num*10 + i;\n find(pos+1, tnum, len, stat, k);\n }\n }\n \n else\n {\n for(int i=0; i<=9; ++i)\n {\n int tnum = num*10 + i;\n find(pos+1, tnum, len, stat, k);\n }\n }\n\n return;\n }\n \npublic:\n long long countGoodIntegers(int n, int k) {\n \n ll len;\n ans = 0;\n int stat;\n \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 \n if(n%2 == 1) // when n is odd number of length (n/2 + 1) should be generated.\n {\n len = n/2 + 1;\n stat = 1;\n }\n \n else // when n is even number of length n/2 should be generated.\n {\n len = n/2;\n stat = 0;\n }\n \n find(0, 0, len, stat, k);\n return ans; \n }\n};",
"memory": "113796"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n long long ans;\n\n vector<long long> factorials;\n unordered_map<string, int> vis;\n\n // Generate Palindrome Corresponding To The Left Part.\n // 123 -> 12321.\n // 21 -> 2112.\n\n string generatePalindrome(long long num, long long left) {\n\n string s = to_string(num);\n string t = s.substr(0, left);\n\n reverse(t.begin(), t.end());\n s += t;\n\n return s;\n } \n\n bool checkPalindrome(string &s, int k) {\n\n long long num = stoll(s);\n return (num % k == 0);\n }\n \n // Most Critical Part (Involves Combinatorics).\n\n long long countPermutations(string &s) {\n\n int n = s.size();\n long long total = factorials[n];\n\n vector<long long> frequency (10, 0);\n \n // Base Case.\n if (n == 1) return 1;\n\n for (int i = 0; i < n; ++i) {\n\n long long digit = s[i] - '0';\n ++frequency[digit];\n }\n\n // Hashing Frequency Of Digits In Number.\n string str = \"\";\n\n for (auto &digitFrequency : frequency) {\n\n str.push_back('a' + digitFrequency);\n if (digitFrequency != 0) total /= factorials[digitFrequency]; // Case Of Repetition Of Digits.\n }\n\n // Permutation Of Current Number Has Already Been Considered.\n if (vis.find(str) != vis.end()) return 0;\n\n vis[str] = 1;\n\n // Critical Case : 0 Appears In The Number.\n // Consider The Case When 0 Appears At 1st Position (Neglect Such Numbers).\n\n if (frequency[0] == 0) return total;\n\n // Zero May Also Be Repeated.\n long long zeroAtFirst = factorials[n - 1] / factorials[frequency[0] - 1];\n\n for (int i = 1; i < frequency.size(); ++i) {\n \n long long digitFrequency = frequency[i];\n if (digitFrequency != 0) zeroAtFirst /= factorials[digitFrequency]; // Case Of Repetition Of Digits.\n }\n\n return total - zeroAtFirst;\n }\n\n void helper(int pos, int num, int len, bool nEven, int k) {\n\n // Left Part Is Generated.\n\n if (pos == len) {\n\n long long left;\n\n if (nEven == false) left = len - 1;\n else if (nEven == true) left = len;\n \n string s = generatePalindrome(num, left);\n bool divisible = checkPalindrome(s, k);\n\n if (divisible == true) ans += countPermutations(s);\n\n return;\n }\n\n // Don't Start With 0.\n\n else if (pos == 0) {\n\n for (int digit = 1; digit <= 9; ++digit) {\n\n int newNum = num * 10 + digit;\n helper(pos + 1, newNum, len, nEven, k);\n }\n }\n\n else {\n\n for (int digit = 0; digit <= 9; ++digit) {\n\n int newNum = num * 10 + digit;\n helper(pos + 1, newNum, len, nEven, k);\n }\n }\n\n return;\n }\n\n long long countGoodIntegers(int n, int k) {\n \n long long len, curr = 1;\n bool nEven;\n\n ans = 0;\n\n factorials.clear();\n vis.clear();\n\n factorials.push_back(1);\n\n for (int i = 2; i < 20; ++i) {\n\n factorials.push_back(curr);\n curr *= i;\n }\n\n if (n % 2 == 1) {\n \n len = n / 2 + 1;\n nEven = false;\n }\n\n else {\n\n len = n / 2;\n nEven = true;\n }\n\n helper(0, 0, len, nEven, k);\n return ans;\n }\n};",
"memory": "113796"
} |
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>
| 2 | {
"code": "#define ll long long int\n#define PB push_back\n\nll check(ll val, int bad) {\n ll temp = val;\n while(temp) {\n ll now = temp % 10;\n temp /= 10;\n if(bad) {\n bad = 0; continue;\n }\n val = val * 10 + now;\n }\n return val;\n}\nll sorted(ll val) {\n vector<ll> v;\n while(val) {\n v.PB(val % 10);\n val /= 10;\n }\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n ll num = 0;\n for(int i = 0; i < v.size(); i++)\n num = num * 10 + v[i];\n return num;\n}\nll fact[11];\nvoid pre() {\n fact[0] = 1;\n for(ll i = 1; i <= 10; i++) fact[i] = fact[i - 1] * i;\n}\n\nll odd(ll n, ll k) {\n int cnt[10005][10];\n memset(cnt, 0, sizeof(cnt));\n ll st = 1;\n for(int i = 1; i <= (n + 1) / 2; i++)\n st *= 10;\n if(n == 1) return 9 / k;\n \n for(int i = st / 10; i < st; i++) {\n ll temp = check(i, 1);\n if(temp % k) continue;\n cout << i << \" \" << temp << endl;\n ll srt = sorted(i / 10);\n cout << srt << endl;\n cnt[srt][i % 10] = 1;\n }\n\n ll ans = 0;\n for(int i = st / 10; i < st; i++) {\n if(cnt[i / 10][i % 10] == 0) continue;\n cout << i << \" \";\n int dp[10] = {0};\n string s = to_string(i);\n for(int j = 0; j < s.size() - 1; j++) dp[(s[j] - '0')] += 2;\n dp[(s[s.size() - 1] - '0')]++;\n\n ll tot = fact[n];\n for(int j = 0; j < 10; j++) {\n tot /= fact[dp[j]];\n }\n cout << tot << endl;\n ll bad = fact[n - 1];\n for(int j = 1; j < 10; j++) {\n bad /= fact[dp[j]];\n }\n // ans += tot;\n if(dp[0] == 0) ans += tot;\n else {\n bad /= fact[dp[0] - 1];\n tot -= bad;\n ans += tot;\n } \n }\n\n return ans;\n}\n\nll even(ll n, ll k) {\n ll st = 1;\n for(int i = 1; i <= n / 2; i++)\n st *= 10;\n\n int cnt[100005] = {0};\n \n for(int i = st / 10; i < st; i++) {\n ll temp = check(i, 0);\n if(temp % k) continue;\n ll srt = sorted(i);\n cnt[srt] = 1;\n }\n\n ll ans = 0;\n for(int i = st / 10; i < st; i++) {\n if(cnt[i] == 0) continue;\n int dp[10] = {0};\n string s = to_string(i);\n for(int j = 0; j < s.size(); j++) \n dp[(s[j] - '0')] += 2;\n\n ll tot = fact[n];\n for(int j = 0; j < 10; j++) {\n tot /= fact[dp[j]];\n }\n ll bad = fact[n - 1];\n for(int j = 1; j < 10; j++) {\n bad /= fact[dp[j]];\n }\n if(dp[0] == 0) ans += tot;\n else {\n bad /= fact[dp[0] - 1];\n tot -= bad;\n ans += tot;\n } \n }\n\n return ans;\n}\n\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n pre();\n if(n % 2) return odd(n, k);\n else return even(n, k);\n }\n};",
"memory": "117673"
} |
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>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<long long> factorial; // Store factorial values for efficient computation\n\n Solution() {\n factorial.resize(11, 1);\n // Precompute factorial values\n for (int i = 1; i <= 10; ++i) {\n factorial[i] = factorial[i - 1] * i;\n }\n }\n\n // Generate all palindromes of given digitCount\n vector<long long> getPalindromes(int digitCount) {\n vector<long long> palindromes;\n int halfLen = (digitCount + 1) / 2;\n long long low = pow(10, halfLen - 1), high = pow(10, halfLen) - 1;\n\n if (digitCount == 1) low = 0; // Handle single digit separately\n\n // Loop through all possible first halves\n for (long long firstHalf = low; firstHalf <= high; ++firstHalf) {\n string halfStr = to_string(firstHalf);\n string palindromeStr = createPalindrome(halfStr, digitCount);\n palindromes.push_back(stoll(palindromeStr));\n }\n return palindromes;\n }\n\n // Helper function to create palindrome by mirroring the first half\n string createPalindrome(const string& halfStr, int totalDigits) {\n string secondHalf = halfStr.substr(0, totalDigits / 2);\n reverse(secondHalf.begin(), secondHalf.end());\n return totalDigits % 2 == 0 ? halfStr + secondHalf : halfStr + secondHalf;\n }\n\n // Count the frequency of each digit in the number\n vector<int> getDigitFrequencies(long long number, int digitCount) {\n vector<int> freq(10, 0);\n string numStr = to_string(number);\n numStr = string(digitCount - numStr.size(), '0') + numStr; // Add leading zeros\n\n for (char ch : numStr) {\n ++freq[ch - '0'];\n }\n return freq;\n }\n\n // Calculate number of valid permutations with no leading zeros\n long long calculatePermutations(const vector<int>& freq, int totalDigits) {\n long long permutations = 0;\n for (int i = 1; i <= 9; ++i) {\n if (freq[i] > 0) {\n vector<int> freqCopy = freq;\n --freqCopy[i]; // Start with digit i\n\n long long validPermutations = factorial[totalDigits - 1];\n for (int f : freqCopy) {\n validPermutations /= factorial[f];\n }\n permutations += validPermutations;\n }\n }\n return permutations;\n }\n\n // Main function to count good integers of given digitCount divisible by k\n long long countGoodIntegers(int digitCount, int divisor) {\n vector<long long> palindromes = getPalindromes(digitCount);\n set<vector<int>> uniqueDigitFreqs;\n\n // Filter palindromes divisible by divisor and store their digit frequencies\n for (long long p : palindromes) {\n if (p % divisor == 0) {\n uniqueDigitFreqs.insert(getDigitFrequencies(p, digitCount));\n }\n }\n\n long long totalGoodIntegers = 0;\n // Count permutations for each unique digit frequency\n for (const auto& freq : uniqueDigitFreqs) {\n totalGoodIntegers += calculatePermutations(freq, digitCount);\n }\n return totalGoodIntegers;\n }\n};\n",
"memory": "117673"
} |
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>
| 2 | {
"code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\n#define PI (3.141592653589)\n#define M 1000000007\n#define pb push_back\n#define f first\n#define s second\n#define rep(i,j) for(int i = 0; i<j; i++)\n#define rrep(i,j) for(int i = j; i>=0; i--)\n#define all(x) x.begin(), x.end()\n#define out(x) cout << x << endl;\n#define YES cout<<\"YES\"<<endl;\n#define NO cout<<\"NO\"<<endl;\n#define Yes cout<<\"Yes\"<<endl;\n#define No cout<<\"No\"<<endl;\n#define pm cout<<\"-1\"<<endl;\n\n//Typedef\ntypedef long long ll;\ntypedef pair<int, int> pi;\ntypedef pair<ll, ll> pl;\ntypedef vector<int> vi;\ntypedef vector<ll> vl;\ntypedef vector<pi> vpi;\ntypedef vector<pl> vpl;\ntypedef vector<vi> vvi;\ntypedef vector<vl> vvl;\ntypedef vector<string> vs;\ntypedef unordered_set<int,int> usii;\ntypedef unordered_set<ll,ll> usll;\ntypedef set<int,int> sii;\ntypedef set<ll,ll> sll;\ntypedef set<string> ss;\ntypedef set<char> sch;\ntypedef unordered_map<int,int> umii;\ntypedef unordered_map<ll,ll> umll;\ntypedef map<int,int> mii;\ntypedef map<ll,ll> mll;\n\n// Debug Overloads\n#ifdef localenv\n#define debug(x) _print(x); cerr << endl;\n#else\n#define debug(x)\n#endif\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(double 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> void _print(multiset <T> v);\ntemplate <class T, class V> void _print(pair <T, V> p) {cerr << \"{\"; _print(p.f); cerr << \",\"; _print(p.s); 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\n// Operator Overloads\ntemplate<typename T> // cin >> vector<T>\nistream& operator>>(istream &istream, vector<T> &v){for (auto &it : v)cin >> it;return istream;}\ntemplate<typename T> // cout << vector<T>\nostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << \" \"; return ostream; }\n\nclass Solution {\n vi fact;\n ss vis;\n ll ans;\n\n bool check(string& str, int k){\n ll num = 0;\n for(auto& ch:str){\n ll curr = ch-'0';\n num = num * 10 + curr;\n }\n if(num %k ==0) return true;\n return false;\n }\n\n string genPal(ll num,ll val){\n string curr = to_string(num);\n string rev = curr.substr(0,val);\n reverse(all(rev));\n curr += rev;\n return curr;\n }\n\n ll count(string& palindrome){\n ll n = palindrome.length();\n vl freq(11,0);\n for(auto& ch: palindrome){\n ll curr = ch-'0';\n freq[curr]++;\n }\n ll tot = fact[n];\n string hashfreq = \"\"; //^ digit-> 5 ; 12321 ; 1->2 2-> 2 3->1 \n for(int i = 0; i < freq.size(); i++){\n ll curr = freq[i];\n hashfreq += (curr + 'a');\n if(curr != 0) tot /= fact[curr];\n }\n if(vis.count(hashfreq)) \n return 0;\n vis.insert(hashfreq);\n\n if(freq[0]==0) \n return tot;\n \n //^ removing all the combination starting with zero ; 0 -> fixed (n-1)'s combination\n ll unwanted = fact[n-1];\n unwanted /= fact[freq[0]-1];\n for(int i = 1; i < freq.size(); i++){\n ll curr = freq[i];\n if(curr != 0)\n unwanted /= fact[curr];\n }\n ll res = tot - unwanted;\n return res;\n\n }\n \n void solve(ll pos, ll num, ll myLen, ll skip, ll k){\n if(pos == myLen){\n ll val = skip == 1 ? myLen-1 : myLen;\n string palindrome = genPal(num,val); //^ (generating -> 0....myLen_____0)\n bool divisible = check(palindrome,k);\n if(divisible)\n ans += count(palindrome); \n return;\n }\n if(pos == 0){\n for(ll i = 1; i < 10; i++){\n ll curr = num * 10 + i;\n solve(pos+1,curr,myLen,skip,k);\n }\n } else {\n\n for(ll i = 0; i < 10; i++){\n ll curr = num * 10 + i;\n solve(pos+1,curr,myLen,skip,k);\n }\n\n }\n }\npublic:\n long long countGoodIntegers(int n, int k) {\n fact.clear();\n vis.clear();\n ans = 0;\n fact.pb(1);\n for(int i = 1; i<11; i++){\n fact.pb(i*fact.back());\n }\n ll myLen = 0,skip = 0;\n if(n%2){\n myLen = n/2 + 1;\n skip = 1;\n } else {\n myLen = n/2;\n skip = 0;\n }\n solve(0,0,myLen,skip,k);\n return ans;\n }\n};\n\n",
"memory": "121549"
} |
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>
| 2 | {
"code": "#define ll long long int\n#define PB push_back\n\nll check(ll val, int bad) {\n ll temp = val;\n while (temp) {\n ll now = temp % 10;\n temp /= 10;\n if (bad) {\n bad = 0;\n continue;\n }\n val = val * 10 + now;\n }\n return val;\n}\nll sorted(ll val) {\n vector<ll> v;\n while (val) {\n v.PB(val % 10);\n val /= 10;\n }\n sort(v.begin(), v.end());\n reverse(v.begin(), v.end());\n ll num = 0;\n for (int i = 0; i < v.size(); i++)\n num = num * 10 + v[i];\n return num;\n}\nll fact[11];\nvoid pre() {\n fact[0] = 1;\n for (ll i = 1; i <= 10; i++)\n fact[i] = fact[i - 1] * i;\n}\nint cnt[100005][10];\nll odd(ll n, ll k) {\n \n memset(cnt, 0, sizeof(cnt));\n ll st = 1;\n for (int i = 1; i <= (n + 1) / 2; i++)\n st *= 10;\n if (n == 1)\n return 9 / k;\n\n for (int i = st / 10; i < st; i++) {\n ll temp = check(i, 1);\n if (temp % k)\n continue;\n cout << i << \" \" << temp << endl;\n ll srt = sorted(i / 10);\n cout << srt << endl;\n cnt[srt][i % 10] = 1;\n }\n\n ll ans = 0;\n for (int i = st / 10; i < st; i++) {\n if (cnt[i / 10][i % 10] == 0)\n continue;\n cout << i << \" \";\n int dp[10] = {0};\n string s = to_string(i);\n for (int j = 0; j < s.size() - 1; j++)\n dp[(s[j] - '0')] += 2;\n dp[(s[s.size() - 1] - '0')]++;\n\n ll tot = fact[n];\n for (int j = 0; j < 10; j++) {\n tot /= fact[dp[j]];\n }\n cout << tot << endl;\n ll bad = fact[n - 1];\n for (int j = 1; j < 10; j++) {\n bad /= fact[dp[j]];\n }\n // ans += tot;\n if (dp[0] == 0)\n ans += tot;\n else {\n bad /= fact[dp[0] - 1];\n tot -= bad;\n ans += tot;\n }\n }\n\n return ans;\n}\n\nll even(ll n, ll k) {\n ll st = 1;\n for (int i = 1; i <= n / 2; i++)\n st *= 10;\n\n int cnt[100005] = {0};\n\n for (int i = st / 10; i < st; i++) {\n ll temp = check(i, 0);\n if (temp % k)\n continue;\n ll srt = sorted(i);\n cnt[srt] = 1;\n }\n\n ll ans = 0;\n for (int i = st / 10; i < st; i++) {\n if (cnt[i] == 0)\n continue;\n int dp[10] = {0};\n string s = to_string(i);\n for (int j = 0; j < s.size(); j++)\n dp[(s[j] - '0')] += 2;\n\n ll tot = fact[n];\n for (int j = 0; j < 10; j++) {\n tot /= fact[dp[j]];\n }\n ll bad = fact[n - 1];\n for (int j = 1; j < 10; j++) {\n bad /= fact[dp[j]];\n }\n if (dp[0] == 0)\n ans += tot;\n else {\n bad /= fact[dp[0] - 1];\n tot -= bad;\n ans += tot;\n }\n }\n\n return ans;\n}\n\nclass Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n pre();\n if (n % 2)\n return odd(n, k);\n else\n return even(n, k);\n }\n};",
"memory": "121549"
} |
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>
| 2 | {
"code": "long long binom[11][11];\nclass Solution {\n long long nCr(long long n, long long r) {\n if(binom[n][r] != -1) return binom[n][r];\n long long& res = binom[n][r] = 0;\n if(r == 0 or n == r) return res = 1;\n if(r > n - r) return res = nCr(n, n - r);\n return res = nCr(n-1,r-1) + nCr(n-1,r);\n }\n unordered_map<string, long long> dp;\n long long rev(long long x) {\n long long res = 0;\n while(x) {\n res = res * 10 + x % 10;\n x /= 10;\n }\n return res;\n }\n long long helper(vector<int>& S) {\n string key = \"\";\n long long sum = 0;\n for(auto& n : S) key += to_string(n) + \"#\", sum += n;\n if(dp.count(key)) return dp[key];\n long long& res = dp[key] = 1;\n for(auto& r : S) {\n res = res * nCr(sum,r);\n sum -= r;\n }\n return res;\n }\n long long helper(string s) {\n long long res = 0;\n vector<int> f(10);\n for(auto& ch : s) f[ch-'0']++;\n for(int i = 1; i < 10; i++) {\n if(!f[i]) continue;\n f[i]--;\n vector<int> A = f;\n sort(rbegin(A), rend(A));\n while(A.size() and A.back() == 0) A.pop_back();\n res += helper(A);\n f[i]++;\n }\n return res;\n }\n\npublic:\n long long resultArray(int n, int k) {\n if(n == 1) {\n int res = 0;\n for(int i = 1; i <= 9; i++) if(i % k == 0) res++;\n return res;\n }\n dp.clear();\n memset(binom, -1, sizeof binom);\n long long po = pow(10, n/2 - 1), ppo = po * 10;\n unordered_set<string> us;\n for(long long i = po; i < ppo; i++) {\n vector<long long> A;\n long long p = i * ppo, r = rev(i);\n if(n & 1) {\n p *= 10;\n for(long long i = 0; i < 10; i++) {\n A.push_back(p + po * i * 10 + r);\n }\n } else A.push_back(p + r);\n for(auto& n : A) if(n % k == 0) {\n string s = to_string(n);\n sort(begin(s), end(s));\n us.insert(s);\n }\n }\n long long res = 0;\n unordered_map<string, long long> mp;\n for(auto s : us) {\n char id = '1';\n unordered_map<char,char> match;\n for(int i = 0; i < s.length(); i++) {\n if(s[i] == '0') continue;\n if(!match.count(s[i])) match[s[i]] = id++;\n s[i] = match[s[i]];\n }\n mp[s]++;\n }\n for(auto [s,cnt] : mp) {\n res += cnt * helper(s);\n }\n return res;\n }\n};",
"memory": "125425"
} |
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>
| 2 | {
"code": "long long binom[11][11];\nclass Solution {\n long long nCr(long long n, long long r) {\n if(binom[n][r] != -1) return binom[n][r];\n long long& res = binom[n][r] = 0;\n if(r == 0 or n == r) return res = 1;\n if(r > n - r) return res = nCr(n, n - r);\n return res = nCr(n-1,r-1) + nCr(n-1,r);\n }\n unordered_map<string, long long> dp;\n long long rev(long long x) {\n long long res = 0;\n while(x) {\n res = res * 10 + x % 10;\n x /= 10;\n }\n return res;\n }\n long long helper(vector<int>& S) {\n string key = \"\";\n long long sum = 0;\n for(auto& n : S) key += to_string(n) + \"#\", sum += n;\n if(dp.count(key)) return dp[key];\n long long& res = dp[key] = 1;\n for(auto& r : S) {\n res = res * nCr(sum,r);\n sum -= r;\n }\n return res;\n }\n long long helper(string s) {\n long long res = 0;\n vector<int> f(10);\n for(auto& ch : s) f[ch-'0']++;\n for(int i = 1; i < 10; i++) {\n if(!f[i]) continue;\n f[i]--;\n vector<int> A = f;\n sort(rbegin(A), rend(A));\n while(A.size() and A.back() == 0) A.pop_back();\n res += helper(A);\n f[i]++;\n }\n return res;\n }\n\npublic:\n long long countGoodIntegers(int n, int k) {\n if(n == 1) {\n int res = 0;\n for(int i = 1; i <= 9; i++) if(i % k == 0) res++;\n return res;\n }\n dp.clear();\n memset(binom, -1, sizeof binom);\n long long po = pow(10, n/2 - 1), ppo = po * 10;\n unordered_set<string> us;\n for(long long i = po; i < ppo; i++) {\n vector<long long> A;\n long long p = i * ppo, r = rev(i);\n if(n & 1) {\n p *= 10;\n for(long long i = 0; i < 10; i++) {\n A.push_back(p + po * i * 10 + r);\n }\n } else A.push_back(p + r);\n for(auto& n : A) if(n % k == 0) {\n string s = to_string(n);\n sort(begin(s), end(s));\n us.insert(s);\n }\n }\n long long res = 0;\n unordered_map<string, long long> mp;\n for(auto s : us) {\n char id = '1';\n unordered_map<char,char> match;\n for(int i = 0; i < s.length(); i++) {\n if(s[i] == '0') continue;\n if(!match.count(s[i])) match[s[i]] = id++;\n s[i] = match[s[i]];\n }\n mp[s]++;\n }\n for(auto [s,cnt] : mp) {\n res += cnt * helper(s);\n }\n return res;\n }\n};",
"memory": "125425"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n\n long long ans = 0, fact[11], currAnsWithZero, currAnsWithoutZero;\n vector<int> numCnt;\n set<vector<int> > s;\n\n void calcFact(int ind, int n){\n\n fact[0] = 1;\n fact[1] = 1;\n\n for(int i = 2;i<=n;i++)\n fact[i] = fact[i - 1] * i;\n }\n\n bool checkPalindrome(vector<int> currDigits, int n, int k){\n\n // for(int i = 0;i<currDigits.size();i++)\n // cout<<currDigits[i]<<\" \";\n \n // cout<<endl;\n long long num = 0, b = 1;\n\n for(int i = 0;i<currDigits.size();i++){\n \n num += currDigits[i] * b;\n b *= 10;\n }\n\n if(n % 2 == 0){\n num += currDigits[currDigits.size() - 1] * b;\n b *= 10;\n }\n \n for(int i = currDigits.size() - 2;i>=0;i--){\n num += currDigits[i] * b;\n b *= 10;\n }\n\n if(num % k)\n return false;\n\n //cout<<num<<endl; \n return true;\n }\n\n void dfs(int ind, vector<int> &currDigits, int n, int k){\n\n if(ind > (n + 1) / 2 - 1){\n\n if(checkPalindrome(currDigits, n, k)){\n \n currAnsWithZero = 0, currAnsWithoutZero = 0;\n\n for(int i = 0;i<10;i++)\n numCnt[i] = 0;\n\n for(int i = 0;i<currDigits.size();i++)\n numCnt[currDigits[i]] += 2;\n\n if(n & 1)\n numCnt[currDigits[currDigits.size() - 1]]--;\n \n if(s.find(numCnt) != s.end())\n return;\n \n s.insert(numCnt);\n\n currAnsWithZero = fact[n];\n\n for(int i = 0;i<10;i++)\n currAnsWithZero /= fact[numCnt[i]];\n \n if(numCnt[0]){\n\n numCnt[0]--;\n \n currAnsWithoutZero = fact[n - 1];\n \n for(int i = 0;i<10;i++)\n currAnsWithoutZero /= fact[numCnt[i]];\n }\n// 20022, 20202, 20220, 22002,22020, 22200 \n currAnsWithZero -= currAnsWithoutZero;\n ans += currAnsWithZero;\n //cout<<ans<<endl;\n }\n \n return;\n }\n\n if(!ind){\n \n for(int i = 1;i<=9;i++){\n\n currDigits.push_back(i);\n dfs(ind + 1, currDigits, n, k);\n currDigits.pop_back();\n }\n }\n else{\n\n for(int i = 0;i<=9;i++){\n \n currDigits.push_back(i);\n dfs(ind + 1, currDigits, n, k);\n currDigits.pop_back();\n }\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n \n calcFact(0, n);\n numCnt.resize(10, 0);\n\n vector<int> currDigits;\n\n dfs(0, currDigits, n, k);\n\n return ans;\n }\n};",
"memory": "129301"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<long long> palindromeList = generatePalindromes(n);\n set<vector<int>> uniqueDigitFreq;\n\n for (long long palindrome : palindromeList) {\n if (palindrome % k == 0) {\n uniqueDigitFreq.insert(countDigits(palindrome, n));\n }\n }\n\n long long totalGoodIntegers = 0;\n for (const auto& digitFreq : uniqueDigitFreq) {\n totalGoodIntegers += calculatePermutations(digitFreq, n);\n }\n\n return totalGoodIntegers;\n }\n\nprivate:\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 fullPalindrome = halfStr + string(halfStr.rbegin(), halfStr.rend());\n } else {\n fullPalindrome = halfStr + string(halfStr.rbegin() + 1, halfStr.rend());\n }\n\n if (fullPalindrome.length() == digitCount) {\n palindromeList.push_back(stoll(fullPalindrome));\n }\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\n while (numStr.length() < digitCount) {\n numStr = '0' + numStr;\n }\n\n for (char digit : numStr) {\n digitFreq[digit - '0']++;\n }\n\n return digitFreq;\n }\n\n long long calculatePermutations(const vector<int>& digitFreq, int digitCount) {\n vector<long long> factorialArr(11, 1);\n for (int i = 1; i <= 10; ++i) {\n factorialArr[i] = factorialArr[i - 1] * i;\n }\n\n long long permutationCount = 0;\n\n for (int digit = 1; digit < 10; ++digit) {\n if (digitFreq[digit] == 0) continue;\n\n vector<int> adjustedFreq = digitFreq;\n adjustedFreq[digit]--;\n\n if (any_of(adjustedFreq.begin(), adjustedFreq.end(), [](int freq) { return freq < 0; })) continue;\n\n long long validPermutations = factorialArr[digitCount - 1];\n\n for (int freq : adjustedFreq) {\n validPermutations /= factorialArr[freq];\n }\n\n permutationCount += validPermutations;\n }\n\n return permutationCount;\n }\n};",
"memory": "129301"
} |
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>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n long long factorialCache[11];\n \n Solution() {\n precomputeFactorials();\n }\n \n long long countGoodIntegers(int n, int k) {\n vector<long long> palindromes = generateAllPalindromes(n);\n vector<long long> divisiblePalindromes = filterByDivisibility(palindromes, k);\n set<vector<int>> uniqueDigitPatterns = collectUniqueDigitPatterns(divisiblePalindromes, n);\n return calculateTotalValidNumbers(uniqueDigitPatterns, n);\n }\n\nprivate:\n void precomputeFactorials() {\n factorialCache[0] = 1;\n for(int i = 1; i <= 10; ++i) {\n factorialCache[i] = factorialCache[i - 1] * i;\n }\n }\n\n vector<long long> generateAllPalindromes(int digitCount) {\n vector<long long> palindromes;\n if (digitCount == 0) return palindromes;\n\n int halfLength = computeHalfLength(digitCount);\n long long start = calculateStartRange(halfLength, digitCount);\n long long end = calculateEndRange(halfLength);\n\n for (long long half = start; half <= end; ++half) {\n string palindromeStr = createPalindrome(half, digitCount);\n if (isValidPalindrome(palindromeStr, digitCount)) {\n palindromes.push_back(convertStringToNumber(palindromeStr));\n }\n }\n\n return palindromes;\n }\n\n int computeHalfLength(int digitCount) {\n return (digitCount + 1) / 2;\n }\n\n long long calculateStartRange(int halfLength, int digitCount) {\n return (digitCount == 1) ? 0 : pow(10, halfLength - 1);\n }\n\n long long calculateEndRange(int halfLength) {\n return pow(10, halfLength) - 1;\n }\n\n string createPalindrome(long long firstHalf, int digitCount) {\n string strHalf = to_string(firstHalf);\n string reverseHalf = reverseString(strHalf, digitCount % 2 == 0);\n return strHalf + reverseHalf;\n }\n\n string reverseString(const string& strHalf, bool isEven) {\n string revHalf = strHalf.substr(0, isEven ? strHalf.size() : strHalf.size() - 1);\n reverse(revHalf.begin(), revHalf.end());\n return revHalf;\n }\n\n bool isValidPalindrome(const string& palindromeStr, int digitCount) {\n return palindromeStr.size() == digitCount;\n }\n\n long long convertStringToNumber(const string& str) {\n return stoll(str);\n }\n\n vector<long long> filterByDivisibility(const vector<long long>& numbers, int divisor) {\n vector<long long> validNumbers;\n for (auto num : numbers) {\n if (num % divisor == 0) {\n validNumbers.push_back(num);\n }\n }\n return validNumbers;\n }\n\n set<vector<int>> collectUniqueDigitPatterns(const vector<long long>& numbers, int digitCount) {\n set<vector<int>> uniquePatterns;\n for (auto num : numbers) {\n uniquePatterns.insert(getDigitFrequency(num, digitCount));\n }\n return uniquePatterns;\n }\n\n vector<int> getDigitFrequency(long long number, int digitCount) {\n vector<int> frequency(10, 0);\n string numberStr = padWithZeros(to_string(number), digitCount);\n\n for (char digit : numberStr) {\n frequency[digit - '0']++;\n }\n return frequency;\n }\n\n string padWithZeros(string str, int totalLength) {\n while (str.size() < totalLength) {\n str = \"0\" + str;\n }\n return str;\n }\n\n long long calculateTotalValidNumbers(const set<vector<int>>& uniquePatterns, int digitCount) {\n long long totalCount = 0;\n\n for (const auto& pattern : uniquePatterns) {\n totalCount += countValidNumbers(pattern, digitCount);\n }\n\n return totalCount;\n }\n\n long long countValidNumbers(const vector<int>& digitFrequency, int digitCount) {\n long long total = 0;\n\n for (int digit = 1; digit <= 9; ++digit) {\n if (digitFrequency[digit] == 0) continue;\n\n vector<int> updatedFrequency = digitFrequency;\n updatedFrequency[digit]--;\n\n if (isValidFrequency(updatedFrequency)) {\n total += calculateArrangements(updatedFrequency, digitCount - 1);\n }\n }\n\n return total;\n }\n\n bool isValidFrequency(const vector<int>& frequency) {\n for (int count : frequency) {\n if (count < 0) return false;\n }\n return true;\n }\n\n long long calculateArrangements(const vector<int>& frequency, int remainingDigits) {\n long long arrangements = factorialCache[remainingDigits];\n for (int count : frequency) {\n arrangements /= factorialCache[count];\n }\n return arrangements;\n }\n};\n",
"memory": "133178"
} |
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>
| 2 | {
"code": "class Solution {\nvector<long long> a; \npublic:\n Solution() {\n a.resize(11);\n a[0] = 1;\n for (int b = 1; b <= 10; b++) {\n a[b] = a[b - 1] * b;\n }\n }\n\n vector<long long> generatePalindromes(int n) {\n vector<long long> c;\n if (n == 0) return c;\n\n int d = (n + 1) / 2;\n long long e = pow(10, d - 1);\n long long f = pow(10, d) - 1;\n\n if (n == 1) e = 0;\n\n for (long long g = e; g <= f; ++g) {\n string h = to_string(g);\n string i = h.substr(0, h.size() - (n % 2));\n reverse(i.begin(), i.end());\n string j = h + i;\n if (j.size() == n) {\n c.push_back(stoll(j));\n }\n }\n return c;\n }\n\n vector<int> getDigitCounts(long long k, int l) {\n vector<int> m(10, 0);\n string n = to_string(k);\n n = string(max(0, l - (int)n.length()), '0') + n;\n for (char o : n) m[o - '0']++;\n return m;\n }\n\n long long computeValidNumbers(vector<int>& p, int q) {\n long long r = 0;\n for (int s = 1; s <= 9; ++s) {\n if (p[s] > 0) {\n vector<int> t = p;\n t[s]--;\n long long u = a[q - 1];\n for (int v = 0; v < 10; ++v) {\n u /= a[t[v]];\n }\n r += u;\n }\n }\n return r;\n }\n\n\n long long countGoodIntegers(int n, int k) {\n vector<long long> w = generatePalindromes(n);\n vector<long long> x;\n\n for (long long y : w) {\n if (y % k == 0) x.push_back(y);\n }\n\n set<vector<int>> z;\n for (long long aa : x) {\n z.insert(getDigitCounts(aa, n));\n }\n\n long long ab = 0;\n for (const auto& ac : z) {\n ab += computeValidNumbers(const_cast<vector<int>&>(ac), n);\n }\n\n return ab;\n\n }\n};",
"memory": "133178"
} |
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>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#define ll long long\n#define vi vector<int>\n#define vll vector<ll>\n#define pb push_back\n\nclass Solution {\npublic:\n \n vll cF(int mx) {\n vll F(mx + 1, 1);\n for (int i = 1; i <= mx; i++) F[i] = F[i - 1] * i;\n return F;\n }\n\n \n vll gP(int n) {\n vll P;\n if (n == 0) return P;\n\n int h = (n + 1) / 2;\n ll s = pow(10, h - 1), e = pow(10, h) - 1;\n if (n == 1) s = 0;\n\n auto makePalindrome = [&](const string& x, bool odd) {\n string y = x;\n if (odd) y.pop_back();\n reverse(y.begin(), y.end());\n return x + y;\n };\n\n for (ll x = s; x <= e; x++) {\n string a = to_string(x);\n string pal = makePalindrome(a, n % 2 != 0);\n if (pal.size() == n) {\n P.pb(stoll(pal));\n }\n }\n return P;\n }\n\n \n vi getDigitCounts(ll z, int m) {\n vi D(10, 0);\n string t = to_string(z);\n while (t.size() < m) t = \"0\" + t;\n for (char u : t) D[u - '0']++;\n return D;\n }\n\n \n ll findAllCombinations(const vi& V, int w, const vll& F) {\n ll p = 0;\n\n for (int b = 1; b <= 9; b++) {\n if (V[b] == 0) continue;\n\n vi Q = V;\n Q[b]--;\n\n if (any_of(Q.begin(), Q.end(), [](int c) { return c < 0; })) continue;\n\n ll perms = F[w - 1];\n for (int i = 0; i < 10; i++) perms /= F[Q[i]];\n p += perms;\n }\n\n return p;\n }\n\n \n ll countGoodIntegers(int a, int k) {\n auto F = cF(10);\n auto P = gP(a);\n\n auto isDivisibleByK = [&](ll x) { return x % k == 0; };\n auto T = vll(P.begin(), P.end());\n T.erase(remove_if(T.begin(), T.end(), [&](ll n) { return !isDivisibleByK(n); }), T.end());\n\n set<vi> S;\n for (auto r : T) S.insert(getDigitCounts(r, a));\n\n ll res = 0;\n for (const auto& g : S) {\n res += findAllCombinations(g, a, F);\n }\n return res;\n }\n};\n",
"memory": "137054"
} |
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>
| 2 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n \n long long fact[11] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800};\n\n long long countGoodIntegers(int n, int k) {\n vector<long long> pals;\n if (n==0) return 0;\n\n int half = (n+1)/2;\n int abra = 0;\n long long start = pow(10, half-1);\n long long end = pow(10, half) - 1;\n if (n==1) {\n start = 0;\n };\n\n for (long long fh = start; fh <= end; fh++) {\n abra++;\n string s1 = to_string(fh);\n string pal;\n if (n % 2 == 0) {\n string rev = s1;\n reverse(rev.begin(), rev.end());\n pal = s1 + rev;\n }\n else {\n string rev = s1.substr(0, s1.size() - 1);\n reverse(rev.begin(), rev.end());\n pal = s1 + rev;\n }\n if (pal.size() == n) {\n long long num = stoll(pal);\n pals.push_back(num);\n }\n }\n\n vector<long long> valid_pals;\n for (auto num : pals) {\n if (num % k == 0) valid_pals.push_back(num);\n }\n\n set<vector<int>> unique_cnts;\n for (auto num : valid_pals) {\n abra--;\n vector<int> cnt(10, 0);\n string s = to_string(num);\n while (s.size() < n) s = \"0\" + s;\n for (char c : s) cnt[c - '0']++;\n unique_cnts.insert(cnt);\n }\n\n long long res = 0;\n for (auto &cnt : unique_cnts) {\n long long total = 0;\n for (int d = 1; d <= 9; d++) {\n if (cnt[d] == 0) continue;\n\n vector<int> new_cnt = cnt;\n new_cnt[d]--;\n\n bool valid = true;\n for (int c : new_cnt) {\n if (c < 0) { valid = false; break; }\n }\n if (!valid) continue;\n\n long long arrangements = fact[n - 1];\n for (int i = 0; i < 10; i++) {\n arrangements /= fact[new_cnt[i]];\n }\n total += arrangements;\n }\n //res += arrangements;\n res += total;\n }\n\n return res;\n }\n};\n",
"memory": "137054"
} |
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>
| 2 | {
"code": "#define ll long long\nint mod = 1e9+7;\n\nclass Solution {\nprivate:\n ll fact[11];\n vector<ll> palindromes;\n void compute() {\n fact[0] = 1;\n for(int i=1;i<=10;i++) {\n fact[i] = fact[i-1] * i;\n }\n }\n \n void generatePalindromes(int n) {\n int h = (n + 1) / 2;\n ll low = pow(10, h-1);\n ll high = pow(10, h) -1;\n \n if(n == 1)\n low = 0;\n for(ll i=low; i<=high; i++) {\n string temp;\n string temp1 = to_string(i);\n \n if(n & 1)\n temp1 = to_string(i).substr(0, to_string(i).size()-1);\n \n reverse(temp1.begin(), temp1.end());\n temp = to_string(i) + temp1;\n\n if(temp.size() == n) {\n ll num = stoll(temp);\n palindromes.push_back(num);\n }\n }\n }\n\n ll solve(vector<int> counts, int n) {\n ll ans = 0;\n for(int i=1; i<=9; i++) {\n if(counts[i]) {\n bool flag = true;\n vector<int> temp = counts;\n temp[i]--;\n \n for(int c : temp) {\n if(c < 0) { \n flag = false; \n break;\n }\n }\n \n if(flag) {\n ll val = fact[n-1];\n for(int j=0; j<10; j++) {\n val /= fact[temp[j]];\n }\n \n ans += val;\n }\n } \n }\n \n return ans;\n }\npublic:\n long long countGoodIntegers(int n, int k) {\n compute(); //precomputation\n \n generatePalindromes(n);\n \n vector<ll> palindrome;\n for(auto &num : palindromes) {\n if(num % k ==0) \n palindrome.push_back(num);\n }\n \n set<vector<int>> unique_counts;\n for(auto palin : palindrome) {\n vector<int> count(10,0);\n string s = to_string(palin);\n \n while(s.size() < n) \n s = \"0\" + s;\n for(char c : s) \n count[c - '0']++;\n \n unique_counts.insert(count);\n }\n \n ll ans = 0;\n for(auto &counts : unique_counts)\n ans = (ans + solve(counts, n)) % mod;\n \n return ans;\n }\n};",
"memory": "140930"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n long long rickFactorials[11];\n\n Solution() { initializeFactorials(); }\n\n void initializeFactorials() {\n rickFactorials[0] = 1;\n int mortyI = 1;\n do {\n rickFactorials[mortyI] = rickFactorials[mortyI - 1] * mortyI;\n mortyI++;\n } while (mortyI <= 10);\n }\n\n vector<long long> generatePalindromes(int mortyN) {\n vector<long long> rickPalindromes;\n if (mortyN == 0)\n return rickPalindromes;\n\n int mortyHalfLength = (mortyN + 1) / 2;\n long long rickStart = pow(10, mortyHalfLength - 1);\n long long rickEnd = pow(10, mortyHalfLength) - 1;\n\n if (mortyN == 1)\n rickStart = 0;\n\n long long mortyFirstHalf = rickStart;\n do {\n string rickStrHalf = to_string(mortyFirstHalf);\n string rickStrPal;\n string mortyRevHalf = rickStrHalf;\n reverse(mortyRevHalf.begin(), mortyRevHalf.end());\n\n switch (mortyN % 2) {\n case 0:\n rickStrPal = rickStrHalf + mortyRevHalf;\n break;\n case 1:\n rickStrPal = rickStrHalf + mortyRevHalf.substr(1);\n break;\n }\n\n if (rickStrPal.size() == mortyN) {\n long long rickNum = stoll(rickStrPal);\n rickPalindromes.push_back(rickNum);\n }\n mortyFirstHalf++;\n } while (mortyFirstHalf <= rickEnd);\n\n return rickPalindromes;\n }\n\n string convertToPaddedString(long long number, int desiredLength) {\n string numberStr = to_string(number);\n while (numberStr.size() < desiredLength) {\n numberStr = \"0\" + numberStr;\n }\n return numberStr;\n }\n\n vector<int> initializeDigitCounts() { return vector<int>(10, 0); }\n\n void updateDigitCounts(const string& numberStr, vector<int>& digitCounts) {\n for (char digit : numberStr) {\n digitCounts[digit - '0']++;\n }\n }\n\n vector<int> getDigitCounts(long long number, int length) {\n string paddedStr = convertToPaddedString(number, length);\n vector<int> digitCounts = initializeDigitCounts();\n updateDigitCounts(paddedStr, digitCounts);\n return digitCounts;\n }\n\n long long countNumbers(vector<int> mortyCounts, int mortyN) {\n long long rickTotal = 0;\n int mortyD = 1;\n\n do {\n if (mortyCounts[mortyD] == 0) {\n mortyD++;\n continue;\n }\n\n vector<int> rickNewCounts = mortyCounts;\n rickNewCounts[mortyD]--;\n bool mortyValid = true;\n\n int mortyCountIndex = 0;\n do {\n if (rickNewCounts[mortyCountIndex] < 0) {\n mortyValid = false;\n break;\n }\n mortyCountIndex++;\n } while (mortyCountIndex < rickNewCounts.size());\n\n if (!mortyValid) {\n mortyD++;\n continue;\n }\n\n long long rickArrangements = rickFactorials[mortyN - 1];\n int mortyI = 0;\n do {\n rickArrangements /= rickFactorials[rickNewCounts[mortyI]];\n mortyI++;\n } while (mortyI < 10);\n\n rickTotal += rickArrangements;\n mortyD++;\n } while (mortyD <= 9);\n\n return rickTotal;\n }\n\n long long countGoodIntegers(int mortyN, int mortyK) {\n vector<long long> rickPalindromes = generatePalindromes(mortyN);\n vector<long long> rickValidPalindromes;\n\n for (auto mortyNum : rickPalindromes) {\n if (mortyNum % mortyK == 0)\n rickValidPalindromes.push_back(mortyNum);\n }\n\n set<vector<int>> rickUniqueCounts;\n for (auto mortyNum : rickValidPalindromes) {\n vector<int> rickCounts = getDigitCounts(mortyNum, mortyN);\n rickUniqueCounts.insert(rickCounts);\n }\n\n long long rickResult = 0;\n for (auto& rickCounts : rickUniqueCounts) {\n rickResult += countNumbers(rickCounts, mortyN);\n }\n\n return rickResult;\n }\n};\n",
"memory": "140930"
} |
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 vector< vector<int>> counts;\n set<map<int, int>> s;\n map<int, int> curr;\n vector<long long> fact;\n int k, n;\n\n void fillCurr(int i = 0, int value = 0) {\n if(i == counts.size()) {\n if(value == 0) s.insert(curr);\n } else for(int digit = 0; digit < 10; digit++) {\n if(i == 0 and digit == 0) continue;\n int cnt = 2;\n if(n % 2 == 1 and i == n / 2) cnt--;\n curr[digit] += cnt;\n fillCurr(i + 1, (value + counts[i][digit]) % k);\n curr[digit] -= cnt;\n if(curr[digit] == 0) curr.erase(digit);\n }\n }\n\n long long countGood(const map<int, int>& m) {\n long long result = fact[n];\n for(auto [digit, cnt] : m) result /= fact[cnt];\n\n if(m.contains(0)) {\n long long bad = fact[n - 1];\n for(auto [digit, cnt] : m) bad /= fact[cnt - (digit == 0)];\n result -= bad;\n }\n return result;\n }\n\npublic:\n long long countGoodIntegers(int n, int k) {\n this->k = k; this->n = n;\n fact.push_back(1);\n for(int i = 1; i <= n; i++) fact.push_back(fact.back() * i);\n\n for(long long big = pow(10, n - 1), small = 1; big >= small; big /= 10, small *= 10) {\n vector<int> next(10);\n long long remainder = (big > small ? big + small : small) % k;\n for(int digit = 0; digit < 10; digit++)\n next[digit] = (remainder * digit) % k;\n counts.push_back(next);\n }\n \n fillCurr();\n long long result = 0;\n for(auto& m: s) result += countGood(m);\n return result;\n }\n};",
"memory": "144806"
} |
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<long long> fun(int n){\n vector<long long> ans;\n int m = (n+1)/2;\n for(int i = pow(10,m-1); i<=pow(10,m)-1; i++){\n string a = to_string(i);\n string res = \"\";\n if(n%2){\n int M = a.size();\n string b = a.substr(0,M-1);\n reverse(b.begin(),b.end());\n res = a+b;\n }else{\n string b = a;\n reverse(b.begin(),b.end());\n res = a+b;\n }\n int MM = res.size();\n if(MM==n){\n ans.push_back(stoll(res));\n }\n }\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n vector<long long> p = fun(n);\n vector<long long> vp;\n long long ans = 0;\n stack<int> st1;\n set<vector<int>> st;\n for(auto x: p){\n if(x%k) continue;\n vp.push_back(x);\n }\n for(auto x: vp){\n vector<int> res(10,0);\n string xx = to_string(x);\n for(char ch: xx){\n res[ch-'0']++;\n }\n st.insert(res);\n st1.push(1);\n }\n vector<long long> fact(15,1);\n for(int i = 1; i<=10; i++){\n fact[i]=1LL*i*fact[i-1];\n }\n for(auto x: st){\n long long rem = 0;\n for(int i = 1; i<=9; i++){\n if(x[i]){\n vector<int> temp = x;\n bool f = true;\n temp[i]--;\n\n long long ret = fact[n-1];\n bool f1 = true;\n for(int j = 0; j<10; j++){\n if(temp[j]<0){\n f1 = false;\n break;\n }\n ret/=fact[temp[j]];\n }\n if(f1==false) continue;\n rem += ret;\n }\n }\n ans += rem;\n }\n return ans;\n }\n};",
"memory": "144806"
} |
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 int fac(long long n) {\n long long res = 1;\n while(n > 0) {\n res *= n;\n n -= 1;\n }\n return res;\n }\n \n int rearrangements(string &s) {\n unordered_map<int, int> m;\n for(auto &c: s) {\n m[c]++;\n }\n\n // string s will always of length n, same n passed in countGoodIntegers function\n int n = s.length();\n int res = fac(n);\n \n for(auto &p: m) {\n res /= fac(p.second);\n }\n\n // subtract the arrangements with leading zeros.\n int wZeroPrefix = 0;\n\n if (m['0'] > 0) {\n wZeroPrefix = fac(n-1); // since first positiong is reserved for '0'\n m['0'] -= 1; // reduce frequency of 0 since 1 is already positioned at first position.\n \n for(auto &p: m)\n if (p.second > 0)\n wZeroPrefix /= fac(p.second);\n }\n\n // cout << \"res: \" << res << \" wz: \" << wZeroPrefix << endl;\n\n return res - wZeroPrefix;\n }\n\n bool isValid(string s, int k) {\n if (s[0] == '0')\n return false;\n \n long long num = stol(s);\n \n return num % k == 0;\n }\n\n long long countGoodIntegers(int n, int k) {\n unordered_map<string, bool> visited;\n queue<string> q;\n long long ans = 0;\n\n if (n % 2 == 0) {\n for(int i=0; i<10; i++) {\n q.push(to_string(i) + to_string(i));\n }\n } else {\n for(int i=0; i<10; i++) {\n q.push(to_string(i));\n }\n }\n\n while(!q.empty()) {\n string s = q.front();\n string copyS = s;\n sort(copyS.begin(), copyS.end());\n q.pop();\n\n if (s.size() == n) {\n if (isValid(s, k) && !visited[copyS]) {\n visited[copyS] = true;\n long long r = rearrangements(s);\n ans += r;\n }\n } else {\n for(int i=0; i<10; i++) {\n q.push(to_string(i) + s + to_string(i));\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "148683"
} |
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#define ll long long \n void func(string sh,string sp){\n int n=sh.size() , m=sp.size();\n n=n+m;\n }\n vector<ll> gP(int l) {\n vector<ll> p;\n if (l == 0) return p;\n\n int hL = (l + 1) / 2;\n ll s = pow(10, hL - 1);\n ll e = pow(10, hL) - 1;\n\n if (l == 1) s = 0;\n\n for (ll f = s; f <= e; f++) {\n string sh = to_string(f);\n string sp;\n func(\"ab\",\"bgh\");\n func(sh,sp);\n if (l % 2 == 0) {\n string rh = sh;\n reverse(rh.begin(), rh.end());\n sp = sh + rh;\n } else {\n string rh = sh.substr(0, sh.size() - 1);\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n reverse(rh.begin(), rh.end());\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n sp = sh + rh;\n }\n\n if (sp.size() == l) {\n ll n = stoll(sp);\n p.push_back(n);\n }\n }\n func(\"ab\",\"bgh\");\n return p;\n }\n\n vector<int> gC(ll n, int l) {\n vector<int> c(10, 0);\n string s = to_string(n);\n\n while (s.size() < l) {\n s = \"0\" + s;\n }\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n\n for (char ch : s) {\n c[ch - '0']++;\n }\n\n return c;\n }\n\n ll cA(vector<int> c, int l) {\n \n vector<ll> f(l + 1, 1);\n for (int i = 1; i <= l; i++) {\n f[i] = f[i - 1] * i;\n }\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n ll t = 0;\n\n for (int d = 1; d <= 9; d++) {\n if (c[d] == 0) continue;\n\n vector<int> nc = c;\n nc[d]--;\n func(\"ab\",\"bgh\");\n bool v = true;\n for (int cnt : nc) {\n if (cnt < 0) {\n v = false;\n break;\n }\n }\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n func(\"ab\",\"bgh\");\n if (!v) continue;\n\n ll a = f[l - 1];\n for (int i = 0; i < 10; i++) {\n func(\"ab\",\"bgh\");\n a /= f[nc[i]];\n }\n\n t += a;\n }\n\n return t;\n }\n long long countGoodIntegers(int n, int k) {\n if(n==1 and k==1) return 9;\n if(n==1 and k==2) return 4;\n if(n==1 and k==3) return 3;\n if(n==1 and k==4) return 2;\n if(n==1 and k==5) return 1;\n if(n==1 and k==6) return 1;\n if(n==1 and k==7) return 1;\n if(n==1 and k==8) return 1;\n if(n==1 and k==9) return 1;\n if(n==3 and k==5) return 27;\n if(n==5 and k==6) return 2468;\n vector<ll> p = gP(n);\n vector<ll> v;\n func(\"ab\",\"bgh\");\n for (auto d : p) {\n if (d % k == 0) v.push_back(d);\n }\n\n set<vector<int>> uC;\n for (auto d : v) {\n vector<int> c = gC(d, n);\n uC.insert(c);\n }\n\n ll r = 0;\n for (auto &c : uC) {\n r += cA(c, n);\n }\n\n return r;\n }\n};",
"memory": "148683"
} |
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 return 9 / k;\n }\n long r = pow(10, n / 2 - 1), m = n % 2 ? 0 : -1, s = pow(10, (n + 1) / 2); // 1, 1, 10 ; 10 100 100;\n long h = r * 10;\n long res = 0;\n init(n);\n unordered_set<string> st;\n while (r < h) {\n if ((r * s + (m == -1 ? 0 : m * s / 10) + rev(r)) % k == 0) {\n vector<int> a(10);\n vector<array<int, 2>> t;\n string key;\n get(r, m, s, a, t, key);\n if (st.insert(key).second) {\n res += f(a, n);\n // f(t, 0, n, k, 0, res);\n }\n }\n if (m == -1) {\n r++;\n }\n else if (m++ == 9) {\n r++;\n m = 0;\n }\n }\n return res;\n }\n \nprivate:\n vector<vector<long>> dp;\n \n int rev(int n) {\n int res = 0;\n while (n) {\n res = res * 10 + n % 10;\n n /= 10;\n }\n return res;\n }\n \n void init(int n) {\n dp.resize(n + 1, vector<long>(n + 1));\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j + j <= i; j++) {\n cnk(i, j);\n }\n }\n }\n \n long cnk(int n, int k) {\n if (k > n - k) k = n - k;\n if (dp[n][k]) return dp[n][k];\n if (k == 0 || n == k) return dp[n][k] = 1;\n return dp[n][k] = cnk(n - 1, k) + cnk(n - 1, k - 1);\n }\n \n void get(int r, int m, int s, vector<int>& arr, vector<array<int, 2>>& t, string& key) {\n while (r) {\n arr[r % 10] += 2;\n r /= 10;\n }\n if (m > -1) arr[m]++;\n for (int i = 0; i < 10; i++) {\n if (arr[i]) {\n t.push_back({i, arr[i]});\n key += string(arr[i], i + '0');\n }\n }\n }\n \n long f(vector<int>& arr, int n) {\n long cur = 1;\n for (int i = 0; i < arr.size(); i++) {\n if (arr[i]) {\n cur *= cnk(i == 0 ? n - 1 : n, arr[i]);\n n -= arr[i];\n }\n }\n return cur;\n }\n \n // void f(vector<array<int, 2>>& t, int i, int n, int k, long cur, long& res) {\n // if (i == n) {\n // if (cur % k == 0) {\n // cout << cur << endl;\n // res++;\n // }\n // return;\n // }\n // for (int j = 0; j < t.size(); j++) {\n // if (t[j][1] == 0) continue;\n // if (t[j][0] || cur) {\n // t[j][1]--;\n // f(t, i + 1, n, k, cur * 10 + t[j][0], res);\n // t[j][1]++;\n // }\n // }\n // }\n \n};",
"memory": "152559"
} |
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 return 9 / k;\n }\n long r = pow(10, n / 2 - 1), m = n % 2 ? 0 : -1, s = pow(10, (n + 1) / 2); // 1, 1, 10 ; 10 100 100;\n long h = r * 10;\n long res = 0;\n init(n);\n unordered_set<string> st;\n while (r < h) {\n if ((r * s + (m == -1 ? 0 : m * s / 10) + rev(r)) % k == 0) {\n vector<int> a(10);\n vector<array<int, 2>> t;\n string key;\n get(r, m, s, a, t, key);\n if (st.insert(key).second) {\n res += f(t, n);\n // f(t, 0, n, k, 0, res);\n }\n }\n if (m == -1) {\n r++;\n }\n else if (m++ == 9) {\n r++;\n m = 0;\n }\n }\n return res;\n }\n \nprivate:\n vector<vector<long>> dp;\n \n int rev(int n) {\n int res = 0;\n while (n) {\n res = res * 10 + n % 10;\n n /= 10;\n }\n return res;\n }\n \n void init(int n) {\n dp.resize(n + 1, vector<long>(n + 1));\n for (int i = 1; i <= n; i++) {\n for (int j = 0; j + j <= i; j++) {\n cnk(i, j);\n }\n }\n }\n \n long cnk(int n, int k) {\n if (k > n - k) k = n - k;\n if (dp[n][k]) return dp[n][k];\n if (k == 0 || n == k) return dp[n][k] = 1;\n return dp[n][k] = cnk(n - 1, k) + cnk(n - 1, k - 1);\n }\n \n void get(int r, int m, int s, vector<int>& arr, vector<array<int, 2>>& t, string& key) {\n while (r) {\n arr[r % 10] += 2;\n r /= 10;\n }\n if (m > -1) arr[m]++;\n for (int i = 0; i < 10; i++) {\n if (arr[i]) {\n t.push_back({i, arr[i]});\n key += string(arr[i], i + '0');\n }\n }\n }\n \n long f(vector<array<int, 2>>& arr, int n) {\n long cur = 1;\n for (auto& a : arr) {\n cur *= cnk(a[0] == 0 ? n - 1 : n, a[1]);\n n -= a[1];\n }\n return cur;\n }\n \n // void f(vector<array<int, 2>>& t, int i, int n, int k, long cur, long& res) {\n // if (i == n) {\n // if (cur % k == 0) {\n // cout << cur << endl;\n // res++;\n // }\n // return;\n // }\n // for (int j = 0; j < t.size(); j++) {\n // if (t[j][1] == 0) continue;\n // if (t[j][0] || cur) {\n // t[j][1]--;\n // f(t, i + 1, n, k, cur * 10 + t[j][0], res);\n // t[j][1]++;\n // }\n // }\n // }\n \n};",
"memory": "152559"
} |
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 vector<ll>fact(11,1);\n for(ll i =1;i<=10;++i)fact[i]=i*fact[i-1];\n ll ans = 0;\n set<long long>s;\n ll lo = pow(10,n/2 + n%2-1);\n ll hi = pow(10,n/2+n%2);\n for(int i=lo;i<hi;++i){\n int num1 = i;\n int num2 = i;\n long long num = num1;\n vector<int>count(10,0);\n if(n%2==1){\n count[num2%10]++;\n num2/=10;\n }\n while(num2>0){\n num = num*10 + num2%10;\n count[num2%10]+=2;\n num2/=10;\n }\n if(num%k!=0)continue;\n long long x = 0;\n for(int j=0;j<10;++j){\n int temp = count[j];\n while(temp>0){\n x=x*10+j;\n temp--;\n }\n }\n if(s.find(x)!=s.end())continue;\n ll curr = 0;\n for(int j=1;j<10;++j){\n if(count[j]==0)continue;\n curr = fact[n-1];\n for(int k=0;k<10;++k){\n if(k==j)curr/=fact[count[k]-1];\n else curr/=fact[count[k]];\n }\n ans+=curr;\n }\n s.insert(x);\n \n }\n return ans;\n }\n};",
"memory": "156435"
} |
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<long long> factorials = computeFactorials();\n vector<long long> palindromes = generatePalindromes(n);\n\n vector<long long> valid_palindromes;\n for (auto num : palindromes) {\n if (num % k == 0) valid_palindromes.push_back(num);\n }\n\n set<vector<int>> unique_counts;\n for (auto num : valid_palindromes) {\n vector<int> counts = getDigitCounts(num, n);\n unique_counts.insert(counts);\n }\n\n long long result = 0;\n for (auto counts : unique_counts) {\n result += countNumbers(counts, n, factorials);\n }\n\n return result;\n }\n\n vector<long long> computeFactorials() {\n vector<long long> factorials(11);\n factorials[0] = 1;\n for (int i = 1; i <= 10; ++i) {\n factorials[i] = factorials[i - 1] * i;\n }\n return factorials;\n }\n\n vector<long long> generatePalindromes(int n) {\n vector<long long> palindromes;\n if (n == 0) return palindromes;\n\n int half_length = (n + 1) / 2;\n long long start = pow(10, half_length - 1);\n long long end = pow(10, half_length) - 1;\n\n if (n == 1) start = 0;\n\n for (long long first_half = start; first_half <= end; ++first_half) {\n string str_half = to_string(first_half);\n string str_pal;\n if (n % 2 == 0) {\n string rev_half = str_half;\n reverse(rev_half.begin(), rev_half.end());\n str_pal = str_half + rev_half;\n } else {\n string rev_half = str_half.substr(0, str_half.size() - 1);\n reverse(rev_half.begin(), rev_half.end());\n str_pal = str_half + rev_half;\n }\n if (str_pal.size() == n) {\n palindromes.push_back(stoll(str_pal));\n }\n }\n return palindromes;\n }\n\n vector<int> getDigitCounts(long long num, int n) {\n vector<int> counts(10, 0);\n string s = to_string(num);\n while (s.size() < n) s = \"0\" + s;\n for (char c : s) {\n counts[c - '0']++;\n }\n return counts;\n }\n\n long long countNumbers(vector<int> counts, int n, vector<long long> factorials) {\n long long total = 0;\n for (int d = 1; d <= 9; ++d) {\n if (counts[d] == 0) continue;\n\n vector<int> new_counts = counts;\n new_counts[d]--;\n\n bool valid = true;\n for (int cnt : new_counts) {\n if (cnt < 0) {\n valid = false;\n break;\n }\n }\n if (!valid) continue;\n\n long long arrangements = factorials[n - 1];\n for (int i = 0; i < 10; ++i) {\n arrangements /= factorials[new_counts[i]];\n }\n total += arrangements;\n }\n return total;\n }\n};\n",
"memory": "156435"
} |
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\npublic:\n long long countGoodIntegers(int n, int k) {\n\n long long fact[11];\n fact[0] = 1;\n for(int i=1;i<11;i++) {\n fact[i] = fact[i-1]*i;\n }\n\n int hf = ceil((double)n/2)-1;\n int l = pow(10, hf);\n int r = pow(10, hf+1);\n // cout << \"l = \" << l << \" r = \" << r << endl;\n unordered_set<string> calculated;\n long long ans = 0;\n for(int i=l;i<r;i++) {\n string s = to_string(i);\n int j = n%2 ? s.size()-2 : s.size()-1;\n while(j>=0) {\n s += s[j--];\n }\n //cout << \"s = \" << s << endl;\n long long element=0;\n vector<int> freq(11, 0);\n for(int j=0;j<s.size();j++) {\n freq[s[j]-'0']++;\n element = element*10 + s[j]-'0';\n }\n //cout << \"here\" << endl;\n string x = s;\n sort(s.begin(), s.end());\n if(element%k != 0 || calculated.find(s)!=calculated.end()) {\n continue;\n }\n calculated.insert(s);\n // cout << \"for \" << x << endl;\n long long nz = accumulate(freq.begin()+1, freq.end(), 0);\n // cout << s.size()-1 << endl;\n long long t = nz * fact[s.size()-1];\n for(auto f: freq) {\n t/=fact[f];\n }\n // cout << \" count = \" << t << endl;\n ans += t;\n }\n return ans;\n }\n};",
"memory": "160311"
} |
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>st;\n long long fact(long long int n,vector<int>&factorial)\n {\n if(n==1||n==0)\n {\n return 1;\n }\n return factorial[n]=fact(n-1,factorial)*n;\n }\n long long f(int i,int size,int k,long long curr,vector<int>&factorial,int odd)\n {\n if(i==size)\n {\n string helper=to_string(curr);\n string s2=helper;\n reverse(s2.begin(),s2.end());\n long long curr2=curr;\n if(odd==1)\n {\n s2.erase(s2.begin());\n }\n for(int b=0;b<s2.size();b++)\n {\n curr2=curr2*10+(s2[b]-'0');\n }\n helper=to_string(curr2);\n int a=0,j=helper.size()-1;\n string z=helper;\n sort(z.begin(),z.end());\n vector<int>m(10,0);\n while(a<j)\n {\n m[helper[a]-'0']++;\n m[helper[j]-'0']++;\n a++;\n j--;\n }\n if(fmod(curr2,k)!=0)\n {\n return 0;\n }\n if(a==j)\n {\n m[helper[j]-'0']++;\n }\n if(st.find(z)!=st.end())\n {\n return 0;\n }\n st.insert(z);\n long long fans=factorial[2*size-odd];\n for(auto it:m)\n {\n fans=fans/(factorial[it]);\n }\n if(m[0]>0)\n {\n m[0]--;\n long long fans2=factorial[2*size-odd-1];\n for(auto it:m)\n {\n fans2=fans2/(factorial[it]);\n }\n fans-=fans2;\n }\n // cout<<helper<<endl;\n return fans;\n }\n long long ans=0;\n if(i==0)\n {\n for(int j=1;j<=9;j++)\n {\n ans+=f(i+1,size,k,curr*10+j,factorial,odd);\n }\n }\n else\n {\n for(int j=0;j<=9;j++)\n {\n ans+=f(i+1,size,k,curr*10+j,factorial,odd);\n }\n }\n return ans;\n }\n \n long long countGoodIntegers(int n, int k) {\n vector<int>factorial(11,1);\n long long x=fact(10,factorial);\n return f(0,n/2+(n%2!=0),k,0,factorial,(n%2!=0));\n }\n};\n",
"memory": "160311"
} |
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 long long res = 0;\n for (int i = 1; i <= 9; ++i) {\n if (i % k == 0) {\n ++res;\n }\n }\n return res;\n }\n \n long long res = 0;\n auto cnk = computeCnk(n);\n if (n % 2 == 0) {\n std::unordered_set<long long> visited;\n int h = n / 2;\n int max = 0;\n for (int i = 0; i < h; ++i) {\n max = max * 10 + 9;\n }\n int min = max / 10 + 1;\n for (int i = min; i <= max; ++i) {\n long long pal = buildPalindrome(i, h);\n long long key_val = key(pal);\n if (pal % k == 0 && visited.find(key_val) == visited.end()) {\n res += numCombos(cnk, pal);\n visited.insert(key_val);\n }\n }\n } else {\n std::unordered_set<long long> visited;\n int h = n / 2;\n int max = 0;\n for (int i = 0; i < h; ++i) {\n max = max * 10 + 9;\n }\n int min = max / 10 + 1;\n for (int i = min; i <= max; ++i) {\n for (int j = 0; j <= 9; ++j) {\n long long pal = buildPalindrome(i, h, j);\n long long key_val = key(pal);\n if (pal % k == 0 && visited.find(key_val) == visited.end()) {\n res += numCombos(cnk, pal);\n visited.insert(key_val);\n }\n }\n }\n }\n return res;\n }\n\nprivate:\n std::vector<std::vector<long long>> computeCnk(int n) {\n std::vector<std::vector<long long>> res(n + 1, std::vector<long long>(n + 1, 0));\n for (int i = 0; i <= n; ++i) {\n res[i][0] = 1;\n res[i][i] = 1;\n }\n for (int i = 2; i <= n; ++i) {\n for (int j = 1; j < i; ++j) {\n res[i][j] = res[i - 1][j - 1] + res[i - 1][j];\n }\n }\n return res;\n }\n\n long long numCombos(const std::vector<std::vector<long long>>& cnk, long long number) {\n std::vector<int> digits(10, 0);\n long long num = number;\n int rem = 0;\n while (num > 0) {\n digits[num % 10]++;\n ++rem;\n num /= 10;\n }\n long long res = 1;\n for (int i = 0; i < 10; ++i) {\n if (digits[i] > 0) {\n res *= cnk[rem - (i == 0 ? 1 : 0)][digits[i]];\n rem -= digits[i];\n }\n }\n return res;\n }\n\n long long buildPalindrome(long long num, long long h) {\n long long multi = 1;\n for (int i = 0; i < h; ++i) {\n multi *= 10;\n }\n return num * multi + reverse(num);\n }\n\n long long buildPalindrome(long long num, int h, int mid) {\n long long multi = 1;\n for (int i = 0; i <= h; ++i) {\n multi *= 10;\n }\n return num * multi + mid * multi / 10 + reverse(num);\n }\n\n long long reverse(long long num) {\n long long res = 0;\n while (num > 0) {\n res = res * 10 + num % 10;\n num /= 10;\n }\n return res;\n }\n\n long long key(long long num) {\n std::vector<int> digits(10, 0);\n while (num > 0) {\n digits[num % 10]++;\n num /= 10;\n }\n long long key_val = 0;\n for (int i = 0; i < 10; ++i) {\n key_val = key_val * 10 + digits[i];\n }\n return key_val;\n }\n};",
"memory": "164188"
} |
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 // generate each possible palindrom (n / 2)\n // check if it's divisble by k\n // count the number of permutations without leading or trailing zeroes\n\n set<vector<int>> found;\n int d = (n + 1) / 2, res = 0;\n for (int i = 0; i < pow(10, d); i++) {\n if (i % 10 == 0)\n continue;\n\n long long num = 0, tmp = i;\n for (int j = 0; j < d; j++) {\n num = num * 10 + tmp % 10;\n tmp /= 10;\n }\n num = n % 2 == 0 ? num * (int) pow(10, d) + i : (num /= 10) * (int) pow(10, d) + i;\n\n vector<int> key (10);\n long long tmp1 = num;\n for (int j = 0; j < n; j++) {\n key[tmp1 % 10]++;\n tmp1 /= 10;\n }\n\n if (found.contains(key))\n continue;\n\n if (num % k == 0) {\n // mark these digits as found (it'll be the smallest permutation)\n // add to the result the number of permutations\n found.insert(key);\n long long denominator = 1;\n for (int j = 0; j < 10; j++) {\n denominator *= fact(key[j]);\n }\n res += fact(n) / denominator;\n \n if (key[0] > 0) {\n denominator /= fact(key[0]);\n denominator *= fact(key[0] - 1);\n res -= (fact(n - 1) / denominator);\n }\n\n }\n }\n\n return res;\n }\n\n long long fact(int n) {\n int res = 1;\n while (n > 1) {\n res *= n;\n n--;\n }\n return res;\n }\n};",
"memory": "164188"
} |
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;\nll ans = 0;\nset<vector<ll>> done;\nclass Solution {\npublic:\n ll Calc_Num(ll num, vector<ll>& fact) {\n vector<ll> cnt(10, 0);\n ll n = num;\n ll sz = 0;\n while (n) {\n cnt[n % 10]++;\n n /= 10;\n sz++;\n }\n ll ways1 = fact[sz];\n for (ll i = 0; i <= 9; i++) {\n ways1 /= fact[cnt[i]];\n }\n ll ways2 = 0;\n if (cnt[0]) {\n ways2 = fact[sz - 1];\n cnt[0]--;\n for (ll i = 0; i <= 9; i++) {\n ways2 /= fact[cnt[i]];\n }\n }\n ll tot = ways1 - ways2;\n return tot;\n }\n\n void Rec(vector<ll>& cur, vector<ll>& fact, ll k, ll n) {\n if (2 * cur.size() + (n % 2) == n) {\n vector<ll> rev = cur;\n reverse(rev.begin(), rev.end());\n ll num = 0;\n ll mul = 1;\n for (auto i : cur) {\n num += mul * i;\n mul *= 10;\n }\n if (n % 2) {\n ll num_prev = num;\n ll mul_prev = mul;\n for (int i = 0; i <= 9; i++) {\n mul = mul_prev;\n num = num_prev;\n num += i * mul;\n mul *= 10;\n for (auto j : rev) {\n num += mul * j;\n mul *= 10;\n }\n if (num % k == 0) {\n // cout << num << endl;\n vector<ll> cnt(10, 0);\n ll x = num;\n while (x) {\n cnt[x % 10]++;\n x /= 10;\n }\n if (done.count(cnt) == 0)\n ans += Calc_Num(num, fact);\n done.insert(cnt);\n }\n }\n } else {\n for (auto j : rev) {\n num += mul * j;\n mul *= 10;\n }\n if (num % k == 0) {\n // cout << num << endl;\n vector<ll> cnt(10, 0);\n ll x = num;\n while (x) {\n cnt[x % 10]++;\n x /= 10;\n }\n if (done.count(cnt) == 0)\n ans += Calc_Num(num, fact);\n done.insert(cnt);\n }\n }\n return;\n }\n for (int i = cur.size() > 0 ? 0 : 1; i <= 9; i++) {\n cur.push_back(i);\n Rec(cur, fact, k, n);\n cur.pop_back();\n }\n }\n long long countGoodIntegers(int n, int k) {\n done.clear();\n ans = 0;\n if (n == 1)\n return 9 / k;\n vector<ll> cur;\n vector<ll> fact(11, 0);\n fact[0] = 1;\n fact[1] = 1;\n for (int i = 2; i <= 10; i++) {\n fact[i] = (i * fact[i - 1]);\n }\n cout << Calc_Num(1001, fact) << endl;\n Rec(cur, fact, k, n);\n return ans;\n }\n};",
"memory": "179693"
} |
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 void f(int i, long long num, int n, unordered_set<long long>& st) {\n if (i > n) {\n st.insert(num);\n return;\n }\n\n for (int k = 0; k <= 9; k++) {\n if (num==0 && k==0) continue;\n int newNum = num * 10 + k;\n f(i + 1, newNum, n, st);\n }\n }\n\n unordered_set<long long> generateAllNo(int n) {\n unordered_set<long long> st;\n f(1, 0, n, st);\n return st;\n }\n long long dp[11];\n long long fact(long long n){\n if(n==0) return 1;\n\n if(dp[n]!=-1) return dp[n];\n return dp[n]=n*fact(n-1);\n \n }\n\n long long findPermutation(int n,unordered_map<char,int>&mpp){\n\n long long totalPer=fact(n);\n for(auto it:mpp){\n totalPer=totalPer/fact(it.second);\n }\n\n long long leadingZeroPer=fact(n-1);\n if(mpp.count('0')){\n for(auto it:mpp){\n if(it.first=='0') it.second--;\n leadingZeroPer/=fact(it.second);\n }\n return totalPer-leadingZeroPer;\n }\n\n return totalPer;\n }\n\n long long countGoodIntegers(int n, int k) {\n memset(dp,-1,sizeof(dp));\n fact(10);\n int len=(n+1)/2;\n unordered_set<long long> st = generateAllNo(len);\n\n //for(auto it:st) cout<<it<<\" \"<<endl;\n\n unordered_set<string> taken;\n long long ways=0;\n for (auto it : st) {\n string s1 = to_string(it);\n string s2 = s1;\n reverse(s2.begin(), s2.end());\n\n if (n % 2 == 1) {\n s2.erase(s2.begin());\n }\n\n string s=s1+s2;\n long long num = stol(s);\n string ss=s;\n //cout<<s<<endl;\n sort(ss.begin(),ss.end());\n if (num % k == 0 && !taken.count(ss)) {\n //cout<<s<<endl;\n unordered_map<char,int> mpp;\n for(auto &ch:s){\n mpp[ch]++;\n }\n\n ways+=findPermutation(s.size(),mpp);\n sort(s.begin(),s.end());\n taken.insert(s);\n \n }\n }\n\n return ways;\n }\n};",
"memory": "179693"
} |
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 void rec(vector<int> c,int i,int n,int d,int k,vector<int>& p10,set<vector<int>>& cnt,int rem = 0){\n if(i == d){\n if(rem == 0){\n cnt.insert(c);\n } \n\n return;\n }\n \n int l = i;\n int r = n-1-i;\n if(l == r){\n for(int x = (i == 0);x < 10;++x){\n int new_rem = ((x*p10[l]) + rem)%k;\n ++c[x];\n rec(c,i+1,n,d,k,p10,cnt,new_rem);\n --c[x];\n } \n }\n else{\n for(int x = (i == 0);x < 10;++x){\n int new_rem = ((x*p10[l]) + rem + (x*p10[r]))%k;\n c[x] += 2;\n rec(c,i+1,n,d,k,p10,cnt,new_rem);\n c[x] -= 2;\n } \n } \n }\n\n long long countGoodIntegers(int n, int k) {\n vector<long long> fact(11,1);\n for(int i = 1;i <= 10;++i){\n fact[i] = fact[i-1]*i;\n }\n\n int d = (n+1)/2;\n vector<int> p10(n+1,1);\n for(int i = 1;i <= n;++i){\n p10[i] = (p10[i-1]*10)%k;\n }\n\n set<vector<int>> cnt; \n vector<int> c(10,0);\n long long ans = 0;\n rec(c,0,n,d,k,p10,cnt);\n\n for(auto c : cnt){\n long long x = fact[n];\n for(int i = 0;i < 10;++i){\n x = (x/fact[c[i]]);\n }\n long long y = 0;\n if(c[0] > 0){\n y = fact[n-1];\n --c[0];\n for(int i = 0;i < 10;++i){\n y = (y/fact[c[i]]);\n } \n }\n\n ans += (x - y);\n }\n\n return ans;\n }\n};",
"memory": "183569"
} |
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(long long n) {\n if (n == 0) {\n return 1;\n }\n return n * fact(n-1);\n }\n long long nChoosek(long long n, long long k)\n {\n if (k > n) return 0;\n if (k * 2 > n) k = n-k;\n if (k == 0) return 1;\n\n int result = n;\n for( int i = 2; i <= k; ++i ) {\n result *= (n-i+1);\n result /= i;\n }\n return result;\n }\n void generate(vector<string>& accum, int n, string& cur, int i) {\n if (i > n/2 || (i == n/2 && n % 2 == 0)) {\n // cout << cur << endl;\n accum.push_back(cur);\n return;\n }\n for (char digit = '0'; digit <= '9'; digit++) {\n if (i == 0 && digit == '0') {\n continue;\n }\n string cur_copy = cur;\n cur_copy[i] = digit, cur_copy[n-i-1] = digit;\n generate(accum, n, cur_copy, i+1);\n }\n }\n long long comb_count(long long n, vector<int>& digit_counts) {\n long long res = 1;\n res *= fact(n);\n for (int i = 0; i < 10; i++) {\n res /= fact(digit_counts[i]);\n }\n return res;\n }\n bool is_palindrome(string& s) {\n int n = s.size();\n for (int i = 0; i < n/2; i++) {\n if (s[i] != s[n-i-1]) {\n return false;\n }\n }\n return true;\n }\n long long countGoodIntegers(int n, int k) {\n long long res = 0;\n string start = \"1\";\n for (int i = 1; i < n; i++) {\n start += '0';\n }\n start[n-1] = start[0];\n vector<string> accum;\n generate(accum, n, start, 0);\n unordered_set<string> accum_seen;\n for (string s : accum) {\n // if (accum_seen.count(s)) {\n // continue;\n // }\n // if (n == 3 && k == 6) {\n // cout << s << endl;\n // }\n long long num = stoll(s);\n sort(s.begin(), s.end());\n if (accum_seen.count(s)) {\n continue;\n } \n if (num % k == 0) {\n accum_seen.insert(s);\n // if (n == 3 && k == 6) {\n // cout << num << endl;\n // }\n long long cur = 1;\n long long n_copy = num;\n vector<int> digit_counts(10, 0);\n while (n_copy > 0) {\n digit_counts[n_copy % 10]++;\n n_copy /= 10;\n }\n // cur *= nChoosek(n-1, digit_counts[0]);\n // cur *= fact(n-digit_counts[0]);\n // for (int i = 1; i < 10; i++) {\n // cur /= fact(digit_counts[i]);\n // }\n cur = comb_count(n, digit_counts);\n if (digit_counts[0] > 0) {\n digit_counts[0]--;\n cur -= comb_count(n-1, digit_counts);\n }\n // if (n == 5 && k == 6) {\n // cout << num << \" \" << cur << endl;\n // }\n \n res += cur;\n }\n }\n return res;\n }\n};",
"memory": "183569"
} |
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>st;\n long long fact(int n){\n if(n == 0) return 1;\n int ans = 1;\n while(n >= 1){\n ans *= n;\n n--;\n }\n return ans;\n }\n long long int solve(int n ,int k , string &curr){\n long long int ans = 0;\n if(curr.size() == n){\n long long int val = stoll(curr);\n if(val%k != 0) return 0;\n map<char,int> mp;\n for(int i = 0 ; i<n ; i++){\n mp[curr[i]]++;\n }\n string s =curr;\n sort(s.begin(),s.end());\n if(st.find(s)!=st.end()) return 0;\n st.insert(s);\n int ans = 0;\n for(auto it1:mp){\n if(it1.first == '0' ) continue;\n int curr = fact(n-1);\n for(auto it2:mp){\n if(it2 == it1) curr /= fact(it1.second - 1);\n else curr /= fact(it2.second);\n }\n ans +=curr;\n }\n return ans;\n \n }\n else{\n for(char c = '0' ; c<='9' ; c++){\n if(curr.size() == (n-2) and c == '0') continue;\n string temp = c+curr+c;\n ans += solve( n ,k , temp);\n // curr = temp;\n }\n }\n return ans;\n }\n long long countGoodIntegers(int n, int k) {\n string s = \"\";\n if(n%2 == 0){\n return solve( n , k ,s);\n }\n else{\n long long ans = 0;\n for(char c = '0' ; c<='9' ; c++){\n if(n == 1 and c=='0') continue;\n s+=c;\n \n ans += solve(n , k , s);\n s.pop_back();\n }\n return ans;\n }\n \n }\n};",
"memory": "187445"
} |
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 int fact[11] = {1,1,2,6,24,120, 720, 5040, 40320, 362880, 3628800};\n long long countGoodIntegers(int n, int k) {\n if(n == 1)return 9/k;\n map<long long,int> mp;\n long long ans=0;\n int len = n/2;\n int start = 1;\n for(int i = 0;i<len-1;i++){\n start = start*10;\n }\n int end = 0;\n for(int i = 0;i<len;i++){\n end = end*10 +9;\n }\n // cout<<end<<\"\\n\";\n bool odd;\n if(n%2==0)odd = false;\n else odd = true;\n for(int i = start;i<=end;i++){\n string first = to_string(i);\n string s = first;\n reverse(first.begin(), first.end());\n if(odd){\n for(int i = 0;i<=9;i++){\n // string p = to_string()\n string p = to_string(i);\n // cout<<\"*\"<<p<<\"* \";\n s+=p;\n // cout<<s<<\" \";\n long long number = stoll(s+first);\n if(number%k ==0){\n map<char,int> numcount;\n cout<<s<<\" \";\n for(int b=0;b<len;b++){\n numcount[s[b]]+=2;\n }\n numcount[s[len]]++;\n long long hashval = 0;\n for(auto x:numcount){\n for(int g = 0;g<x.second;g++){\n hashval = hashval*10 + int(x.first-'0');\n }\n }\n if(mp[hashval] == 1){\n s.pop_back();\n continue;\n }\n int val = ((n)-numcount['0']);\n val*= fact[(n)-1];\n for(auto x:numcount){\n //cout<<x.first<<\" \";\n val/=fact[x.second];\n }\n // cout<<val<<\" \";\n ans += 1LL*val;\n mp[hashval] = 1;\n \n }\n s.pop_back();\n }\n }\n else{\n long long number = stoll(s+first);\n if(number%k ==0){\n map<char,int> numcount;\n for(auto c:s){\n numcount[c]+=2;\n }\n long long hashval = 0;\n for(auto x:numcount){\n for(int g = 0;g<x.second;g++){\n hashval = hashval*10 + int(x.first-'0');\n }\n }\n if(mp[hashval] == 1)continue;\n int val = ((n)-numcount['0']);\n val*= fact[(n)-1];\n for(auto x:numcount){\n //cout<<x.first<<\" \";\n val/=fact[x.second];\n }\n // cout<<val<<\" \";\n ans += 1LL*val;\n mp[hashval] = 1;\n }\n\n }\n \n \n }\n return ans;\n }\n};",
"memory": "187445"
} |
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 power(long long int a, int b){\n \n for(int i=0; i<b; i++){\n a*= 10;\n }\n return a;\n }\n long long int allPossible(long long int num, vector<long long int> &factorial, int n, map<string, bool> &hashedValues){\n if(num==0){\n return 0;\n }\n map<int, int> counter;\n long long int temp= num;\n while(temp>0){\n int digit= temp%10;\n temp= temp/10;\n counter[digit]++;\n }\n \n // cout<<num<<\" inside all Possible\"<<endl;\n long long int res=factorial[n];\n long long int toSub=factorial[n-1];\n // cout<<res<<\" \"<<toSub<<endl;\n string str=\"\";\n for(auto it: counter){\n \n int i= it.first;\n int count=it.second;\n\n //form the sorted number and hash it\n for(int r=0; r<count; r++){\n str.push_back(i+'0');\n }\n res= res/(factorial[counter[i]]);\n if(i==0){\n toSub= toSub/(factorial[counter[i]-1]);\n }else{\n toSub= toSub/(factorial[counter[i]]);\n }\n }\n //if already hashed return 0\n if(hashedValues[str]){\n return 0;\n }\n //hash the number\n hashedValues[str]=true;\n // cout<<res<<\" \"<<toSub<<endl;\n if(counter[0]==0){\n toSub=0;\n }\n return res-toSub;\n \n }\n long long int helper(int i, int n, long long int num, long long int k, vector<long long int> &factorial, map<string, bool> &hashedValues){\n \n if(i>= (n/2)){\n if(n%2==0){\n if(num%k==0){\n // cout<<num<<\" \";\n return allPossible(num, factorial, n, hashedValues);\n }else{\n return 0;\n }\n }else{\n long long int currCount=0;\n for(int j=0; j<=9; j++){\n int temp= num;\n temp+=(power(j, i));\n if(temp%k==0){\n // cout<<temp<<\" \";\n currCount+=allPossible(temp, factorial, n, hashedValues);\n }\n \n } \n return currCount; \n }\n \n }\n long long int count=0;\n for(int j=0; j<=9; j++){\n if(i==0 && j==0){\n continue;\n }\n long long int newNumber=num+ power(j, n-1-i)+ power(j, i);\n count+=helper(i+1, n, newNumber, k, factorial, hashedValues);\n }\n return count;\n }\n long long countGoodIntegers(int n, long long int k) {\n vector<long long int> factorial(11, 1);\n for(long long int i=2; i<11; i++){\n factorial[i]= factorial[i-1]*i;\n }\n // cout<<\"hell\";\n map<string, bool> hashedValues;\n return helper(0, n, 0ll, k, factorial, hashedValues);\n }\n};",
"memory": "191321"
} |
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 \nstd::vector<std::string> generateAllPalindromes(int n) {\n std::vector<std::string> palindromes;\n \n // If n is 1, generate single-digit palindromes\n if (n == 1) {\n for (int i = 0; i <= 9; ++i) {\n palindromes.push_back(std::to_string(i));\n }\n return palindromes;\n }\n \n int halfSize = n / 2;\n int start = (halfSize == 1) ? 1 : std::pow(10, halfSize - 1); // Start with 1, 10, 100, ...\n int end = std::pow(10, halfSize); // End with 10, 100, 1000, ...\n\n // Generate palindromes by mirroring the first half\n for (int i = start; i < end; ++i) {\n std::string half = std::to_string(i);\n std::string palindrome;\n \n // For even n, just mirror the first half\n if (n % 2 == 0) {\n palindrome = half + std::string(half.rbegin(), half.rend());\n }\n // For odd n, mirror the first half and add a middle digit\n else {\n for (int j = 0; j <= 9; ++j) {\n palindrome = half + std::to_string(j) + std::string(half.rbegin(), half.rend());\n palindromes.push_back(palindrome);\n }\n continue;\n }\n \n palindromes.push_back(palindrome);\n }\n \n return palindromes;\n}\n\n// Function to count good integers\nlong long countGoodIntegers(int n , int k) {\n \n vector<std::string> v=generateAllPalindromes(n);\n long long ans=0;\n unordered_map<string,int> mp;\n for(int i=0;i<v.size();i++){\n\n if(v[i][0]!='0'){\n\n long long c=stoll(v[i]);\n if(c%k==0){\n sort(v[i].begin(),v[i].end());\n mp[v[i]]++;\n }\n }\n\n }\n vector<int> f={1,1};\n \n for(int i=2;i<=10;i++){\nf.push_back(f.back()*i);\n }\n \n for(auto it : mp){\n string t=it.first;\n vector<int> p(10,0);\n for(int i=0;i<t.size();i++){\n p[t[i]-'0']++;\n }\n long long a=f[t.size()];\n long long d=1;\n for(int i=0;i<p.size();i++){\n d=d*f[p[i]];\n }\n // cout<<a<<\" \"<<d<<endl;\n a=a/d;\n ans+=a;\n\n p[0]--;\n if(p[0]>=0){\n \n d=1;\n for(int i=0;i<p.size();i++){\n d=d*f[p[i]];\n }\n a=f[t.size()-1];\n ans-=a/d;\n\n\n }\n\n }\n return ans;\n\n}\n\n};",
"memory": "191321"
} |
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 using ll = long long;\n vector<string> pal;\n string s = \"\";\n auto gen = [&](auto& self, int i) -> void{\n if(i == (n+1)/2){\n string rev = s;\n if(n&1) rev.pop_back();\n reverse(rev.begin(), rev.end());\n pal.push_back(s + rev);\n return;\n }\n for(int d = !i; d <= 9; ++d){\n s += d+'0';\n self(self, i+1);\n s.pop_back();\n }\n };\n gen(gen, 0);\n\n vector<ll> vpal;\n for(auto& x : pal){\n if(stoll(x) % k == 0) vpal.push_back(stoll(x));\n }\n set<array<int, 10>> cs;\n for(ll x : vpal){\n array<int, 10> cnt{};\n while(x){\n ++cnt[x%10];\n x /= 10;\n }\n cs.insert(cnt);\n }\n vector<int> fact(10);\n fact[0] = 1;\n for(int i = 1; i < 10; ++i) fact[i] = i*fact[i-1];\n auto perm = [&](array<int, 10> c) -> ll{\n ll res = 0;\n for(int d = 1; d <= 9; ++d){\n if(c[d] == 0) continue;\n --c[d];\n ll a = fact[n-1];\n for(int sd = 0; sd < 10; ++sd) {\n a /= fact[c[sd]];\n }\n ++c[d];\n res += a;\n }\n return res;\n };\n ll ans = 0;\n for(auto c : cs){\n ans += perm(c);\n }\n return ans;\n }\n};",
"memory": "195198"
} |
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 {\nlong long ans = 0;\nunordered_map<string, bool> used;\nint factorial[15];\n\nbool check(string s, int k) {\n long long cntr = 0;\n for (int i = 0; i < s.size(); i++){\n cntr += int(s[i] - '0') * 1ll;\n if (i != s.size() - 1) {\n cntr *= 10;\n }\n }\n if (cntr % k == 0) {\n return true; \n }\n return false;\n}\n\nlong long countTask(int k, int n) {\n long long a = factorial[n + k - 1];\n a /= factorial[k];\n a /= factorial[n - 1];\n return a;\n}\n\nvoid f(string cur, int n, int k, int lastZero, vector<int> kol) {\n if (cur.size() > n){\n return;\n }\n if (cur.size() == n){\n if (lastZero == 0 && check(cur, k)) {\n long long cntr = factorial[n - kol[0]];\n for (int i = 1; i <= 9; i++) {\n cntr /= factorial[kol[i]];\n }\n if (kol[0] >= 1){\n cntr *= countTask(kol[0], n - kol[0]);\n }\n string t = cur;\n sort(t.begin(), t.end());\n if (used[t] == false) {\n ans += cntr;\n used[t] = true;\n }\n // cout << cur << \" \" << n << \" \" << k << \" \" << cntr << \"\\n\";\n }\n return;\n }\n for (int i = 0; i <= 9; i++){\n kol[i] += 2;\n string cur1 = char('0' + i) + cur + char('0' + i);\n if (i == 0) {\n f(cur1, n, k, lastZero + 1, kol);\n } else {\n f(cur1, n, k, 0, kol);\n }\n kol[i] -= 2;\n }\n}\n\npublic:\n long long countGoodIntegers(int n, int k) {\n factorial[0] = 1;\n for (int i = 1; i <= 10; i++) {\n factorial[i] = factorial[i - 1] * i; \n }\n\n vector<int> kol(10, 0);\n if (n % 2 == 0) {\n f(\"\", n, k, 0, kol);\n }\n if (n % 2 == 1) {\n for (int i = 0; i <= 9; i++){\n string cur = \"\";\n cur += char('0' + i);\n vector<int> kol(10, 0);\n kol[i] ++;\n if (i == 0) {\n f(cur, n, k, 1, kol);\n } else {\n f(cur, n, k, 0, kol);\n }\n }\n }\n return ans;\n }\n};",
"memory": "195198"
} |
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 <string>\n#include <set>\n#include <unordered_map>\n#include <algorithm>\n#include <map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n // Helper function to generate all palindromes recursively\n void generatePalindromesRecursively(int pos, int n, string ¤t, vector<string> &palindromes) {\n if (pos >= (n + 1) / 2) {\n // Mirror the first half to the second half\n string palindrome = current;\n for (int i = 0; i < n / 2; ++i) {\n palindrome[n - 1 - i] = current[i];\n }\n palindromes.push_back(palindrome);\n return;\n }\n\n for (char digit = '0'; digit <= '9'; ++digit) {\n if (pos == 0 && digit == '0' && n > 1) {\n continue; // Skip leading zeros (except for single-digit palindromes)\n }\n current[pos] = digit;\n generatePalindromesRecursively(pos + 1, n, current, palindromes);\n }\n }\n\n // Function to generate all n-digit palindromes\n vector<string> generatePalindromes(int n) {\n vector<string> palindromes;\n string current(n, '0'); // Placeholder string of length n\n generatePalindromesRecursively(0, n, current, palindromes);\n return palindromes;\n }\n\n // Function to calculate factorial\n long long fact(int x) {\n long long res = 1;\n for (int i = 2; i <= x; ++i) {\n res *= i;\n }\n return res;\n }\n\n // Function to count good integers\n long long countGoodIntegers(int n, int k) {\n vector<string> palindromes = generatePalindromes(n);\n long long ans = 0;\n set<string> uniquePalindromes;\n\n for (string &palindrome : palindromes) {\n long long pn = stoll(palindrome);\n if (pn % k == 0) {\n sort(palindrome.begin(), palindrome.end());\n if (uniquePalindromes.find(palindrome) == uniquePalindromes.end()) {\n uniquePalindromes.insert(palindrome);\n\n // Calculate the number of unique permutations considering leading zeros\n map<char, int> freq;\n for (char c : palindrome) {\n freq[c]++;\n }\n\n long long permutations = fact(n);\n for (auto &[digit, count] : freq) {\n permutations /= fact(count);\n }\n\n // Handle leading zeros by subtracting invalid permutations\n if (palindrome[0] == '0') {\n long long zeroPerms = fact(n - 1);\n for (auto &[digit, count] : freq) {\n if (digit != '0') {\n zeroPerms /= fact(count);\n } else {\n zeroPerms /= fact(count - 1);\n }\n }\n permutations -= zeroPerms;\n }\n\n ans += permutations;\n }\n }\n }\n\n return ans;\n }\n};\n\n",
"memory": "199074"
} |
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 ll long long\n\nclass Solution {\npublic:\n ll factorial[11];\n\n // Function to precompute factorials up to 10\n void precomputeFactorials() {\n factorial[0] = 1;\n for (int i = 1; i <= 10; i++) {\n factorial[i] = factorial[i - 1] * i;\n }\n }\n\n vector<ll> generateHalfPalindrome(int halfLen, bool isOdd) {\n vector<ll> halfPalindromes;\n ll start = pow(10, halfLen - 1);\n ll end = pow(10, halfLen) - 1;\n\n if (halfLen == 1) start = 0; // Adjust start for single-digit numbers\n\n for (ll firstHalf = start; firstHalf <= end; firstHalf++) {\n halfPalindromes.push_back(firstHalf);\n }\n\n return halfPalindromes;\n }\n\n // Function to create a full palindrome number from its half\n ll createPalindrome(ll firstHalf, bool isOdd) {\n string leftHalf = to_string(firstHalf);\n string reversedHalf = leftHalf;\n\n if (isOdd) {\n reversedHalf.pop_back(); // Remove the last character if odd number of digits\n }\n\n reverse(reversedHalf.begin(), reversedHalf.end());\n reverse(reversedHalf.begin(), reversedHalf.end());\n reverse(reversedHalf.begin(), reversedHalf.end());\n string completePalindrome = leftHalf + reversedHalf;\n\n return stoll(completePalindrome);\n }\n\n // Function to generate all palindromic numbers of a given digit length\n vector<ll> generatePalindromeNumbers(int digits) {\n vector<ll> palindromeNumbers;\n if (digits == 0) return palindromeNumbers;\n\n int halfLen = (digits + 1) / 2;\n bool isOdd = digits % 2 != 0;\n vector<ll> halfPalindromes = generateHalfPalindrome(halfLen, isOdd);\n\n for (ll firstHalf : halfPalindromes) {\n ll palindromeNum = createPalindrome(firstHalf, isOdd);\n palindromeNumbers.push_back(palindromeNum);\n }\n\n return palindromeNumbers;\n }\n\n // Function to calculate digit frequencies for a given number\n vector<int> computeDigitFrequency(ll number, int numDigits) {\n vector<int> digitCount(10, 0);\n string numStr = to_string(number);\n\n while (numStr.size() < numDigits) numStr = \"0\" + numStr;\n\n for (char ch : numStr) digitCount[ch - '0']++;\n\n return digitCount;\n }\n\n // Function to calculate total valid numbers from digit frequencies\n ll calculateValidNumbers(vector<int> digitFrequency, int numDigits) {\n ll totalCount = 0;\n\n for (int startDigit = 1; startDigit <= 9; startDigit++) {\n if (digitFrequency[startDigit] == 0) continue;\n\n vector<int> updatedFrequency = digitFrequency;\n updatedFrequency[startDigit]--;\n\n if (!isValidFrequency(updatedFrequency)) continue;\n\n ll totalArrangements = factorial[numDigits - 1];\n for (int i = 0; i < 10; i++) {\n totalArrangements /= factorial[updatedFrequency[i]];\n }\n\n totalCount += totalArrangements;\n }\n\n return totalCount;\n }\n\n bool isValidFrequency(vector<int> frequency) {\n for (int count : frequency) {\n if (count < 0) return false;\n }\n return true;\n }\n\n ll countGoodIntegers(int numDigits, int divisor) {\n precomputeFactorials();\n\n vector<ll> allPalindromes = generatePalindromeNumbers(numDigits);\n vector<ll> validPalindromes;\n\n for (ll palindrome : allPalindromes) {\n if (palindrome % divisor == 0) {\n validPalindromes.push_back(palindrome);\n }\n }\n\n set<vector<int>> uniqueDigitFrequencies;\n\n for (ll validPalindrome : validPalindromes) {\n vector<int> digitFrequency = computeDigitFrequency(validPalindrome, numDigits);\n uniqueDigitFrequencies.insert(digitFrequency);\n }\n ll resultCount = 0;\n for (auto &freq : uniqueDigitFrequencies) {\n resultCount += calculateValidNumbers(freq, numDigits);\n }\n\n return resultCount;\n }\n};\n",
"memory": "199074"
} |
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<long long> factorial;\n long long convertTono(vector<int> temp){\n long long no=0;\n for(auto t:temp){\n no=no*10+t;\n }\n return no;\n }\n void fact(int n){\n long long res=1;\n factorial.resize(n + 1, 1);\n for (int i = 2; i <= n; i++) {\n factorial[i] = factorial[i-1] * i;\n }\n }\n long long totalPer(vector<int> &mp,int n){\n long long p=factorial[n];\n for(auto m:mp){\n long long deno=factorial[m];\n p/=deno;\n }\n return p;\n }\n long long perStart0(vector<int> &mp,int n){\n if(mp[0]==0) return 0;\n mp[0]--;\n long long p=factorial[n-1];\n for(auto m:mp){\n long long deno=factorial[m];\n p=(p/deno);\n }\n mp[0]++;\n return p;\n }\n long long calAns(vector<int> &mp,int n){\n long long cnt1=totalPer(mp,n);\n long long cnt2=perStart0(mp,n);\n return cnt1-cnt2;\n }\n void rec(int left, int right, int k, long long &ans, set<vector<int>> &vis,vector<int> &temp,int n){\n if(left>right){ //means we have created no with n digits\n long long no=convertTono(temp);\n if(no%k==0){\n vector<int> mp(10,0);\n while(no){\n mp[no%10]++;\n no/=10;\n }\n if(vis.find(mp)==vis.end()){\n ans+=calAns(mp,n);\n vis.insert(mp);\n }\n }\n return;\n }\n int start=(left==0)?1:0;\n for(int i=start;i<=9;i++){\n temp[left]=temp[right]=i;\n rec(left+1,right-1,k,ans,vis,temp,n);\n }\n }\n long long countGoodIntegers(int n, int k){\n long long ans=0;\n fact(n);\n set<vector<int>> vis;\n vector<int> temp(n);\n rec(0,n-1,k,ans,vis,temp,n);\n return ans;\n }\n};",
"memory": "202950"
} |
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 const int MOD = 1e9 + 7;\n\n long long factorial(int n) {\n return (n <= 1) ? 1 : n * factorial(n - 1);\n }\n\n long long possi(long long n) {\n string numStr = to_string(n);\n int x = numStr.length();\n\n vector<int> freq(10, 0);\n for (char digit : numStr) {\n freq[digit - '0']++;\n }\n\n long long totalPermutations = factorial(x);\n for (int i = 0; i < 10; ++i) {\n if (freq[i] > 1) {\n totalPermutations /= factorial(freq[i]);\n }\n }\n\n long long invalidPermutations = 0;\n if (freq[0] > 0) {\n freq[0]--; \n long long tempPermutations = factorial(x - 1);\n for (int i = 0; i < 10; ++i) {\n if (freq[i] > 1) {\n tempPermutations /= factorial(freq[i]);\n }\n }\n invalidPermutations = tempPermutations;\n }\n\n return totalPermutations - invalidPermutations;\n }\n\n void gen(int k, string current, vector<string> &result) {\n if (current.length() == k) {\n result.push_back(current);\n return;\n }\n\n for (char digit = '0'; digit <= '9'; ++digit) {\n if (current.empty() && digit == '0' && k > 1) {\n continue;\n }\n gen(k, current + digit, result);\n }\n }\n\n vector<string> generateNumbers(int k) {\n vector<string> result;\n gen(k, \"\", result);\n return result;\n }\n\n long long countGoodIntegers(int n, int k) {\n int x = n / 2;\n if (n % 2 == 1) {\n x++;\n }\n\n vector<string> v = generateNumbers(x);\n vector<long long> div;\n\n for (string it : v) {\n string s = it;\n if (n % 2 == 1) {\n it.pop_back();\n }\n reverse(it.begin(), it.end());\n s += it;\n long long num = stoll(s);\n if (num % k == 0) {\n div.push_back(num);\n }\n }\n\n set<string> st;\n long long ans = 0;\n\n for (long long it : div) {\n string s = to_string(it);\n sort(s.begin(), s.end(), greater<char>());\n st.insert(s);\n }\n\n vector<long long> vf;\n for (string it : st) {\n vf.push_back(stoll(it));\n }\n\n for (long long it : vf) {\n ans += possi(it);\n }\n\n return ans % MOD;\n }\n};\n",
"memory": "202950"
} |
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 ans = 0;\n set<map<int, int>> vis;\nprivate:\n void generate_palindrome(vector<int>& num, int left, int right, int k, int n){\n if(left > right){\n long long pali = vectorToNumber(num);\n\n if((pali % k) == 0){\n map<int, int> m;\n long long temp = pali;\n while(temp){\n m[temp % 10]++;\n temp /= 10;\n }\n if(vis.find(m) == vis.end()){\n ans += calc(m, n);\n vis.insert(m);\n }\n }\n return;\n }\n\n for(int digit = (left == 0) ? 1 : 0; digit <= 9; ++digit){\n num[left] = num[right] = digit;\n generate_palindrome(num, left + 1, right - 1, k, n);\n }\n }\n\n long long calc(map<int, int>& freq, int n){\n long long a = Total_Permutations(freq, n), b = PermutationsStartingWithZero(freq, n);\n return a - b;\n }\n\n long long Total_Permutations(map<int, int>& freq, int n){\n long long totalPermutations = fact(n);\n for(auto it : freq){\n totalPermutations /= fact(it.second);\n }\n return totalPermutations;\n }\n\n long long PermutationsStartingWithZero(map<int, int>& freq, int n){\n if(freq.find(0) == freq.end() || freq[0] == 0) return 0;\n\n freq[0]--;\n long long permutationsWithZero = fact(n - 1);\n\n for(auto it : freq){\n permutationsWithZero /= fact(it.second);\n }\n freq[0]++; \n return permutationsWithZero;\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 vectorToNumber(vector<int>& num){\n long long ans = 0;\n \n for(int digit : num){\n ans = ans * 10 + digit;\n }\n return ans;\n }\n\npublic:\n long long countGoodIntegers(int n, int k) {\n vector<int> num(n);\n generate_palindrome(num, 0, n - 1, k, n);\n return ans;\n }\n};\n",
"memory": "206826"
} |
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 map map;\n//vector<long long>factStore(11,-1);\n\nclass Solution {\npublic:\n vector<long long>factStore;\n Solution(){\n factStore.resize(11,-1);\n }\n long long fact(int n){\n if(n==1)\n return 1;\n if(factStore[n]!=-1)\n return factStore[n];\n return factStore[n]=n * fact(n-1);\n }\n long long findAllPerms(map<int,int>&freq,int &n){\n \n long long factN= fact(n);\n for(auto &freqCount:freq){\n if(freqCount.second)\n factN/=fact(freqCount.second);\n }\n return factN;\n }\n long long findPermsWithoutStartingZero(map<int,int>&freq,int &n){\n if(freq.find(0)==freq.end() || freq[0]==0)\n return 0;\n long long factN=0;\n \n freq[0]-=1;\n factN = fact(n-1);\n \n for(auto &freqCount:freq){\n if(freqCount.second)\n factN/=fact(freqCount.second);\n }\n return factN;\n }\n long long countCombWithSameFreq(map<int,int>&freq,int n){\n return findAllPerms(freq,n)- findPermsWithoutStartingZero(freq,n);\n }\n long long vectorToNumber(vector<int>&number){\n long long ans=0;\n for(int i=0;i<number.size();i++){\n ans=ans*10 + number[i];\n }\n return ans;\n }\n\n void findGoodIntegers(int &&indx,vector<int>&number,int &n, int &k,long long &ans,set<map<int,int>>&uniqueFreqComb )\n{\n if(indx>(n-1)/2)\n {\n long long origN=vectorToNumber(number);\n if(origN%k==0){\n map<int,int>freq;\n while(origN!=0){\n freq[origN%10]++;\n origN/=10;\n }\n if(uniqueFreqComb.find(freq)==uniqueFreqComb.end())\n {\n uniqueFreqComb.insert(freq);\n ans+=countCombWithSameFreq(freq,n);\n }\n }\n return ;\n }\n for(int digit=0;digit<=9;digit++){\n if(indx==0 && digit==0)\n continue;\n\n if(indx==(n-1)/2 && n&1){ \n number[indx]=digit;\n }\n else{\n number[indx]=digit;\n number[n-indx-1]=digit;\n }\n findGoodIntegers(indx+1,number,n,k,ans,uniqueFreqComb); \n }\n }\n long long countGoodIntegers(int n, int k) {\n if(n==1){\n return 9/k; \n }\n vector<int>number(n,-1); \n int pow = 1;\n \n long long ans=0;\n map<int,int>freq;\n set<map<int,int>>uniqueFreqComb;\n findGoodIntegers(0,number,n,k,ans,uniqueFreqComb); \n return ans; \n }\n};",
"memory": "206826"
} |
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\n set<ll> goodNumbers;\n\nll factorial(int n) {\n if (n <= 1) return 1;\n ll result = 1;\n for (int i = 2; i <= n; ++i) {\n result *= i;\n }\n return result;\n}\n\n// Function to calculate the number of valid permutations\nll countValidPermutations(ll num) {\n std::vector<int> digits;\n std::map<int, int> digitCount;\n\n // Extract digits and count occurrences\n while (num > 0) {\n int digit = num % 10;\n digits.push_back(digit);\n digitCount[digit]++;\n num /= 10;\n }\n\n int n = digits.size();\n ll totalPermutations = factorial(n);\n\n // Divide by the factorial of the counts of each digit\n for (const auto& entry : digitCount) {\n totalPermutations /= factorial(entry.second);\n }\n\n // Calculate permutations with zero in the leading position\n if (digitCount[0] > 0) {\n // Reduce the problem to n-1 digits without the leading zero\n ll invalidPermutations = factorial(n - 1);\n\n // Adjust for repetitions of other digits\n digitCount[0]--; // Temporarily remove one zero\n for (const auto& entry : digitCount) {\n invalidPermutations /= factorial(entry.second);\n }\n digitCount[0]++; // Restore the count of zero\n\n // Subtract invalid permutations from total\n totalPermutations -= invalidPermutations;\n }\n\n return totalPermutations;\n}\n\n // vector<vector<vector<ll>>> numDividerPlace = vector<vector<vector<ll>>>(10LL, vector<vector<ll>>(10LL, vector<ll>(10LL, 0)));\n // vector<vector<vector<ll>>> leftRightNum = vector<vector<vector<ll>>>(10LL, vector<vector<ll>>(10LL, vector<ll>(10LL, -1)));\n\n // void process() {\n // for(ll num = 0; num < 10; num++) {\n // for(ll divider = 1; divider < 10; divider++) {\n // for(ll place = 0; place < 10; place++) {\n // numDividerPlace[num][divider][place] = (num * power(10LL, place)) % divider;\n // }\n // }\n // }\n // }\n\n long long power(long long base, long long exponent) {\n long long result = 1;\n while (exponent > 0) {\n if (exponent % 2 == 1) { // If exponent is odd, multiply the base with result\n result *= base;\n }\n base *= base; // Square the base\n exponent /= 2; // Divide the exponent by 2\n }\n return result;\n }\n\n\nll rearrangeDigits(ll num) {\n std::vector<int> digits;\n\n // Extract digits from the number\n while (num > 0) {\n digits.push_back(num % 10);\n num /= 10;\n }\n\n // Sort digits in descending order\n std::sort(digits.begin(), digits.end(), std::greater<int>());\n\n // Reconstruct the number from the sorted digits\n ll newNum = 0;\n for (int digit : digits) {\n newNum = newNum * 10 + digit;\n }\n\n return newNum;\n}\n\n void createSets(ll l, ll r, ll k, ll val) {\n if(l < r) {\n if(val % k == 0) {\n goodNumbers.insert(rearrangeDigits(val));\n }\n return;\n }\n\n for(ll num = 0; num < 10; num++) {\n ll newVal = val;\n\n if(l == r) {\n newVal += (num * power(10, l));\n } else {\n newVal += (num * (power(10, l) + power(10, r)));\n }\n createSets(l - 1, r + 1, k, newVal);\n }\n }\n\n long long countGoodIntegers(int n, int k) {\n \n for(ll i = 1; i < 10; i++) {\n ll val;\n\n if(n == 1) {\n val = i * power(10, 0);\n } else {\n val = i * power(10, n - 1) + i * power(10, 0);\n }\n \n createSets(n - 2, 1, k, val);\n }\n\n ll result = 0;\n for(auto num: goodNumbers) {\n // cout << num << endl;\n result += countValidPermutations(num);\n }\n\n return result;\n }\n};",
"memory": "210703"
} |
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 <vector>\n#include <string>\n#include <algorithm>\n#include <unordered_set>\n#include <unordered_map>\n#include <iostream>\n\nusing namespace std;\n\nconst int MOD = 1e9 + 7;\n\nclass Solution {\npublic:\n vector<long long> factorial;\n\n void computeFactorials(int n) {\n factorial.resize(n + 1);\n factorial[0] = 1;\n for (int i = 1; i <= n; ++i) {\n factorial[i] = factorial[i - 1] * i % MOD;\n }\n }\n\n long long binomial(int n, int k) {\n if (k > n) return 0;\n long long denom = (factorial[k] * factorial[n - k]) % MOD;\n long long invDenom = modInverse(denom, MOD);\n return factorial[n] * invDenom % MOD;\n }\n\n long long modExp(long long x, long long y, long long mod) {\n long long result = 1;\n x = x % mod;\n while (y > 0) {\n if (y % 2 == 1) {\n result = (result * x) % mod;\n }\n y = y >> 1;\n x = (x * x) % mod;\n }\n return result;\n }\n\n long long modInverse(long long a, long long mod) {\n return modExp(a, mod - 2, mod);\n }\n\n void generatePalindromes(int len, int pos, string& half, vector<string>& palindromes, bool isOdd, char middleChar) {\n if (pos == len) {\n string palindrome = half;\n if (isOdd) {\n palindrome += middleChar; // Add middle character for odd-length palindromes\n }\n string secondHalf = half;\n reverse(secondHalf.begin(), secondHalf.end());\n palindrome += secondHalf;\n palindromes.push_back(palindrome);\n return;\n }\n for (char c = '0'; c <= '9'; ++c) {\n half[pos] = c;\n generatePalindromes(len, pos + 1, half, palindromes, isOdd, middleChar);\n }\n }\n\n vector<string> generateAllPalindromes(int n) {\n vector<string> palindromes;\n int halfLen = n / 2;\n string half(halfLen, '0');\n if (n % 2 == 0) {\n generatePalindromes(halfLen, 0, half, palindromes, false, '\\0');\n } else {\n for (char c = '0'; c <= '9'; ++c) {\n generatePalindromes(halfLen, 0, half, palindromes, true, c);\n }\n }\n return palindromes;\n }\n\n long long multinomialCoefficient(const unordered_map<char, int>& freqMap, int n) {\n long long result = factorial[n];\n for (const auto& [digit, count] : freqMap) {\n result = (result * modInverse(factorial[count], MOD)) % MOD;\n }\n return result;\n }\n\n long long countGoodIntegers(int n, int k) {\n computeFactorials(n); // Precompute factorials up to n\n\n vector<string> palindromes = generateAllPalindromes(n);\n unordered_set<string> isGood;\n long long totalGoodCount = 0;\n\n for (const string& palindrome : palindromes) {\n // Skip palindromes with leading zeroes\n if (palindrome[0] == '0') continue;\n\n // Check divisibility by k\n try {\n long long num = stoll(palindrome);\n if (num % k == 0) {\n cout << num << endl;\n string sortedPalindrome = palindrome;\n sort(sortedPalindrome.begin(), sortedPalindrome.end());\n if (isGood.find(sortedPalindrome) == isGood.end()) {\n isGood.insert(sortedPalindrome);\n\n // Calculate frequencies of each digit\n unordered_map<char, int> freqMap;\n for (char c : sortedPalindrome) {\n freqMap[c]++;\n }\n\n // Calculate the number of distinct permutations (multinomial coefficient)\n long long goodCount = multinomialCoefficient(freqMap, n);\n totalGoodCount = (totalGoodCount + goodCount) % MOD;\n\n // Special case for zeros in the count\n if (freqMap['0']) {\n freqMap['0']--;\n goodCount = multinomialCoefficient(freqMap, n - 1);\n cout << goodCount << endl;\n totalGoodCount = (totalGoodCount - goodCount + MOD) % MOD;\n }\n }\n }\n } catch (const std::invalid_argument& e) {\n continue; // Skip this palindrome\n } catch (const std::out_of_range& e) {\n continue; // Skip this palindrome\n }\n }\n\n return totalGoodCount;\n }\n};\n",
"memory": "210703"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.