// #include "stdafx.h" #include <stdio.h> #include <algorithm> using namespace std; typedef long long int lli; static lli k, a, b; const int MAX_TAB_INDEX = 1000 * 1000 - 1; static short int aTab[MAX_TAB_INDEX + 1] = { -1, }; // suma kwadratow cyfr indeksu static int HowManyDigits(lli x) { int iCnt = 0; while (x) { x /= 10; iCnt++; } return iCnt; } static lli PowerOfTen(int x) { lli iRet = 1; for (int i = 0; i < x; ++i) { iRet *= 10; } return iRet; } static inline bool CrossRanges(lli & iMinA, lli & iMaxA, lli iMinB, lli iMaxB) { if (iMinA > iMaxB || iMinB > iMaxA) return false; iMinA = max(iMinA, iMinB); iMaxA = min(iMaxA, iMaxB); return true; } static inline int GetSumOf3(int x) { short int s = aTab[x]; if (s != -1) return s; int iStartX = x; for (s = 0; x; x /= 10) { int r = x % 10; s += (r * r); } aTab[iStartX] = s; return s; } static inline int GetSumOf6(int x) { short int s = aTab[x]; if (s != -1) return s; s = GetSumOf3(x % 1000); s += GetSumOf3(x / 1000); aTab[x] = s; return s; } static inline int GetSumOfBig(lli x) { int iRet = 0; while (x) { int r = x % 1000000; iRet += GetSumOf6(r); x /= 1000000; } return iRet; } static bool IsOK(lli x) { return k * GetSumOfBig(x) == x; } static lli CountByDigits(int iDigits) { lli iMinByK = k; lli iMaxByK = k * (iDigits * 81); if (!CrossRanges(iMinByK, iMaxByK, a, b)) return 0; lli iMinByDigits = PowerOfTen(iDigits - 1); lli iMaxByDigits = PowerOfTen(iDigits) - 1; if (!CrossRanges(iMinByK, iMaxByK, iMinByDigits, iMaxByDigits)) return 0; lli iCnt = 0; lli iMinRest = iMinByK % k; lli iMin = iMinRest ? (iMinByK - iMinRest + k) : iMinByK; lli iMax = iMaxByK - (iMaxByK % k); for (lli i = iMin; i <= iMax; i += k) if (IsOK(i)) { // printf(" - %lld\n", i); iCnt++; } return iCnt; } static void Solve() { lli iCnt = 0; int iMinDigits = HowManyDigits(a); int iMaxDigits = HowManyDigits(b); for (int iDigits = iMinDigits; iDigits <= iMaxDigits; ++iDigits) { iCnt += CountByDigits(iDigits); } printf("%lld", iCnt); } int main(int argc, char * argv[]) { for(int i = 0; i <= MAX_TAB_INDEX; ++i) aTab[i] = -1; // freopen("sample_input.txt", "r", stdin); // freopen("sample_output.txt", "w", stdout); scanf("%lld", &k); scanf("%lld", &a); scanf("%lld", &b); Solve(); 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | // #include "stdafx.h" #include <stdio.h> #include <algorithm> using namespace std; typedef long long int lli; static lli k, a, b; const int MAX_TAB_INDEX = 1000 * 1000 - 1; static short int aTab[MAX_TAB_INDEX + 1] = { -1, }; // suma kwadratow cyfr indeksu static int HowManyDigits(lli x) { int iCnt = 0; while (x) { x /= 10; iCnt++; } return iCnt; } static lli PowerOfTen(int x) { lli iRet = 1; for (int i = 0; i < x; ++i) { iRet *= 10; } return iRet; } static inline bool CrossRanges(lli & iMinA, lli & iMaxA, lli iMinB, lli iMaxB) { if (iMinA > iMaxB || iMinB > iMaxA) return false; iMinA = max(iMinA, iMinB); iMaxA = min(iMaxA, iMaxB); return true; } static inline int GetSumOf3(int x) { short int s = aTab[x]; if (s != -1) return s; int iStartX = x; for (s = 0; x; x /= 10) { int r = x % 10; s += (r * r); } aTab[iStartX] = s; return s; } static inline int GetSumOf6(int x) { short int s = aTab[x]; if (s != -1) return s; s = GetSumOf3(x % 1000); s += GetSumOf3(x / 1000); aTab[x] = s; return s; } static inline int GetSumOfBig(lli x) { int iRet = 0; while (x) { int r = x % 1000000; iRet += GetSumOf6(r); x /= 1000000; } return iRet; } static bool IsOK(lli x) { return k * GetSumOfBig(x) == x; } static lli CountByDigits(int iDigits) { lli iMinByK = k; lli iMaxByK = k * (iDigits * 81); if (!CrossRanges(iMinByK, iMaxByK, a, b)) return 0; lli iMinByDigits = PowerOfTen(iDigits - 1); lli iMaxByDigits = PowerOfTen(iDigits) - 1; if (!CrossRanges(iMinByK, iMaxByK, iMinByDigits, iMaxByDigits)) return 0; lli iCnt = 0; lli iMinRest = iMinByK % k; lli iMin = iMinRest ? (iMinByK - iMinRest + k) : iMinByK; lli iMax = iMaxByK - (iMaxByK % k); for (lli i = iMin; i <= iMax; i += k) if (IsOK(i)) { // printf(" - %lld\n", i); iCnt++; } return iCnt; } static void Solve() { lli iCnt = 0; int iMinDigits = HowManyDigits(a); int iMaxDigits = HowManyDigits(b); for (int iDigits = iMinDigits; iDigits <= iMaxDigits; ++iDigits) { iCnt += CountByDigits(iDigits); } printf("%lld", iCnt); } int main(int argc, char * argv[]) { for(int i = 0; i <= MAX_TAB_INDEX; ++i) aTab[i] = -1; // freopen("sample_input.txt", "r", stdin); // freopen("sample_output.txt", "w", stdout); scanf("%lld", &k); scanf("%lld", &a); scanf("%lld", &b); Solve(); return 0; } |