// 'Hello World!' program #include <iostream> #include <string> #include <map> int _checkedCounter = 0; const int NUMBERSCOUNT = 10; int result = 0; long long MINVALUE; long long MAXVALUE; bool Checked(int aNumberCount[NUMBERSCOUNT], long long aK, int aAllNrCount) { _checkedCounter++; if (aNumberCount[0] == aAllNrCount) return false; long long res = 0; for (int i = 0; i <= 9; i++) res += aNumberCount[i] * i * i; long long ourNumber = res * aK; std::string str = std::to_string(ourNumber); int strLEngth = str.length(); if (strLEngth != aAllNrCount) return false; int dupa[NUMBERSCOUNT]; for (int i = 0; i < NUMBERSCOUNT; i++) dupa[i] = 0; for (int i = 0; i < strLEngth; i++) dupa[str[i] - '0']++; for (int i = 0; i < NUMBERSCOUNT; i++) if (aNumberCount[i] != dupa[i]) return false; if (ourNumber >= MINVALUE && ourNumber <= MAXVALUE) result++; //TODO trzeba zapewnic ze liczby sa w ramach MIN/MAX return true; } void Dupa(int aNumberCount[NUMBERSCOUNT], int aNumberLeft, int aPos, long long aK, int aAllNrCount) { if (aPos == NUMBERSCOUNT - 1) { aNumberCount[aPos] = aNumberLeft; Checked(aNumberCount, aK, aAllNrCount); } else { aNumberCount[aPos] = aNumberLeft; Checked(aNumberCount, aK, aAllNrCount); int nrCountHere = aNumberLeft - 1; while (nrCountHere >= 0) { aNumberCount[aPos] = nrCountHere; Dupa(aNumberCount, aNumberLeft - nrCountHere, aPos + 1, aK, aAllNrCount); nrCountHere--; } } aNumberCount[aPos] = 0; } int main() { const long long TOOBIG = 1000000000000000000; long long k; //long long x, y, z; std::cin >> k >> MINVALUE >> MAXVALUE; //walnac petle na poczatek //upewnic sie jak z zerami std::string strMin = std::to_string(MINVALUE); int minNrValue = strMin.length(); std::string strMax = std::to_string(MAXVALUE); int maxNrValue = strMax.length(); if (MAXVALUE == TOOBIG) { maxNrValue--; if (k == MAXVALUE) { std::cout << 1 << std::endl; return 0; } } int allNr; int arrayNumber[10]; for (allNr = minNrValue; allNr <= maxNrValue; allNr++) { for (int i = 0; i < 10; i++) arrayNumber[i] = 0; Dupa(arrayNumber, allNr, 0, k, allNr); } std::cout << result << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | // 'Hello World!' program #include <iostream> #include <string> #include <map> int _checkedCounter = 0; const int NUMBERSCOUNT = 10; int result = 0; long long MINVALUE; long long MAXVALUE; bool Checked(int aNumberCount[NUMBERSCOUNT], long long aK, int aAllNrCount) { _checkedCounter++; if (aNumberCount[0] == aAllNrCount) return false; long long res = 0; for (int i = 0; i <= 9; i++) res += aNumberCount[i] * i * i; long long ourNumber = res * aK; std::string str = std::to_string(ourNumber); int strLEngth = str.length(); if (strLEngth != aAllNrCount) return false; int dupa[NUMBERSCOUNT]; for (int i = 0; i < NUMBERSCOUNT; i++) dupa[i] = 0; for (int i = 0; i < strLEngth; i++) dupa[str[i] - '0']++; for (int i = 0; i < NUMBERSCOUNT; i++) if (aNumberCount[i] != dupa[i]) return false; if (ourNumber >= MINVALUE && ourNumber <= MAXVALUE) result++; //TODO trzeba zapewnic ze liczby sa w ramach MIN/MAX return true; } void Dupa(int aNumberCount[NUMBERSCOUNT], int aNumberLeft, int aPos, long long aK, int aAllNrCount) { if (aPos == NUMBERSCOUNT - 1) { aNumberCount[aPos] = aNumberLeft; Checked(aNumberCount, aK, aAllNrCount); } else { aNumberCount[aPos] = aNumberLeft; Checked(aNumberCount, aK, aAllNrCount); int nrCountHere = aNumberLeft - 1; while (nrCountHere >= 0) { aNumberCount[aPos] = nrCountHere; Dupa(aNumberCount, aNumberLeft - nrCountHere, aPos + 1, aK, aAllNrCount); nrCountHere--; } } aNumberCount[aPos] = 0; } int main() { const long long TOOBIG = 1000000000000000000; long long k; //long long x, y, z; std::cin >> k >> MINVALUE >> MAXVALUE; //walnac petle na poczatek //upewnic sie jak z zerami std::string strMin = std::to_string(MINVALUE); int minNrValue = strMin.length(); std::string strMax = std::to_string(MAXVALUE); int maxNrValue = strMax.length(); if (MAXVALUE == TOOBIG) { maxNrValue--; if (k == MAXVALUE) { std::cout << 1 << std::endl; return 0; } } int allNr; int arrayNumber[10]; for (allNr = minNrValue; allNr <= maxNrValue; allNr++) { for (int i = 0; i < 10; i++) arrayNumber[i] = 0; Dupa(arrayNumber, allNr, 0, k, allNr); } std::cout << result << std::endl; return 0; } |