Untitled
unknown
plain_text
8 months ago
2.7 kB
6
Indexable
#include <iostream>
#include <cstdio>
#include <vector>
#include <unordered_set>
#include <algorithm>
using std::sort;
using std::next_permutation;
using std::unordered_set;
typedef long long ll;
class Solution {
public:
bool judgek(int arr[], int n, int k){
bool flg;
do{
flg = true;
if(arr[0] == 0) continue;
for(int i = 0; i < n / 2; i++){
if(arr[i] != arr[n - i - 1]) flg = false;
}
if(!flg) continue;
ll tmp = 0;
for(int i = 0; i < n; i++){
tmp = tmp * 10 + arr[i];
}
if(tmp % k == 0) return true;
}while(next_permutation(arr, arr + n));
return false;
}
int countk(int arr[], int n, int k){
int res = 0;
do{
if(arr[0] == 0) continue;
ll tmp = 0;
for(int i = 0; i < n; i++){
tmp = tmp * 10 + arr[i];
}
res++;
}while(next_permutation(arr, arr + n));
return res;
}
long long countGoodIntegers(int n, int k){
long long res = 0;
unordered_set<long long> st;
int flg = 1, lens = n / 2;
for(int i = 0; i < lens; i++){
flg *= 10;
}
int arr[11];
for(int i = 0; i < flg; i++){
int tmp = i;
ll hsn = 0;
for(int j = 0; j < lens; j++){
arr[2 * j + 1] = arr[2 * j] = tmp % 10;
tmp /= 10;
}
if(n % 2 == 0){
std::sort(arr, arr + n);
for(int j = 0; j < n; j++){
hsn = hsn * 10 + arr[j];
}
if(st.find(hsn) != st.end()) continue;
st.insert(hsn);
if(!judgek(arr, n, k)) continue;
std::sort(arr, arr + n);
res += countk(arr, n, k);
}else{
int arrb[11];
for(int j = 0; j < 10; j++, hsn = 0){
memcpy(arrb, arr, sizeof(arr));
arrb[n - 1] = j;
std::sort(arrb, arrb + n);
for(int ii = 0; ii < n; ii++){
hsn = hsn * 10 + arrb[ii];
}
if(st.find(hsn) != st.end()) continue;
st.insert(hsn);
if(!judgek(arrb, n, k)) continue;
std::sort(arrb, arrb + n);
res += countk(arrb, n, k);
}
}
}
return res;
}
};
Editor is loading...
Leave a Comment