#include <stdint.h> #include <cstring> #include <iostream> #include <stack> #include <utility> #include <vector> uint64_t gMin = 0; uint64_t gMax = 0; uint64_t gSum = 0; std::vector<std::pair<bool, bool> (*)(uint64_t)> gFuncs; std::pair<bool, bool> div2(uint64_t v) { return {v % 2 == 0, true}; } std::pair<bool, bool> div3(uint64_t v) { return {v % 3 == 0, false}; } std::pair<bool, bool> div4(uint64_t v) { return {v % 4 == 0, v > 100}; } std::pair<bool, bool> div5(uint64_t v) { return {v % 5 == 0, true}; } std::pair<bool, bool> div6(uint64_t v) { return {v % 6 == 0, v % 2 != 0}; } std::pair<bool, bool> div7(uint64_t v) { return {v % 7 == 0, false}; } std::pair<bool, bool> div8(uint64_t v) { return {v % 8 == 0, v > 1000}; } std::pair<bool, bool> div9(uint64_t v) { return {v % 9 == 0, false}; } struct Data { Data(int depth, uint64_t val, uint64_t digit, bool* a) : depth(depth), val(val), digit(digit) { memcpy(this->a, a, 8 * sizeof(bool)); } int depth; uint64_t val; uint64_t digit; bool a[8]; }; uint64_t pow(uint64_t v, uint64_t exp) { if (exp == 0) return v; uint64_t res = v; for (uint64_t i = 0; i < exp; ++i) { res *= 10; } return res; } void recursion(std::stack<Data>& stack, int depth, uint64_t val, uint64_t digit, bool* digits) { // val = val + pow(digit, depth); if (digit > 1) { digits[digit - 2] = true; } if (val > gMax) { return; } bool divisible = true; for (int i = 0; i < 8; ++i) { if (digits[i] == false) { continue; } auto p = gFuncs[i](val); if (p.first == false) { if (p.second == true) { return; } divisible = false; break; } } if (divisible && val >= gMin) { // DEBUG // std::cerr << val << std::endl; gSum++; } for (int i = 1; i < 10; ++i) { bool a[8]; memcpy(a, digits, 8 * sizeof(bool)); stack.push(Data(depth + 1, val, i, a)); } } int main() { gFuncs.push_back(&div2); gFuncs.push_back(&div3); gFuncs.push_back(&div4); gFuncs.push_back(&div5); gFuncs.push_back(&div6); gFuncs.push_back(&div7); gFuncs.push_back(&div8); gFuncs.push_back(&div9); std::cin >> gMin; std::cin >> gMax; // DEBUG // std::cerr << gMin << " " << gMax << std::endl; // std::stack<Data> stack; for (int i = 1; i <= 9; ++i) { bool a[8] = {}; stack.push(Data(0, 0, i, a)); } while (stack.empty() == false) { Data d = stack.top(); stack.pop(); recursion(stack, d.depth, d.val, d.digit, d.a); } // std::cerr << sum << std::endl; std::cout << gSum << 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 111 112 113 114 115 116 117 118 119 120 | #include <stdint.h> #include <cstring> #include <iostream> #include <stack> #include <utility> #include <vector> uint64_t gMin = 0; uint64_t gMax = 0; uint64_t gSum = 0; std::vector<std::pair<bool, bool> (*)(uint64_t)> gFuncs; std::pair<bool, bool> div2(uint64_t v) { return {v % 2 == 0, true}; } std::pair<bool, bool> div3(uint64_t v) { return {v % 3 == 0, false}; } std::pair<bool, bool> div4(uint64_t v) { return {v % 4 == 0, v > 100}; } std::pair<bool, bool> div5(uint64_t v) { return {v % 5 == 0, true}; } std::pair<bool, bool> div6(uint64_t v) { return {v % 6 == 0, v % 2 != 0}; } std::pair<bool, bool> div7(uint64_t v) { return {v % 7 == 0, false}; } std::pair<bool, bool> div8(uint64_t v) { return {v % 8 == 0, v > 1000}; } std::pair<bool, bool> div9(uint64_t v) { return {v % 9 == 0, false}; } struct Data { Data(int depth, uint64_t val, uint64_t digit, bool* a) : depth(depth), val(val), digit(digit) { memcpy(this->a, a, 8 * sizeof(bool)); } int depth; uint64_t val; uint64_t digit; bool a[8]; }; uint64_t pow(uint64_t v, uint64_t exp) { if (exp == 0) return v; uint64_t res = v; for (uint64_t i = 0; i < exp; ++i) { res *= 10; } return res; } void recursion(std::stack<Data>& stack, int depth, uint64_t val, uint64_t digit, bool* digits) { // val = val + pow(digit, depth); if (digit > 1) { digits[digit - 2] = true; } if (val > gMax) { return; } bool divisible = true; for (int i = 0; i < 8; ++i) { if (digits[i] == false) { continue; } auto p = gFuncs[i](val); if (p.first == false) { if (p.second == true) { return; } divisible = false; break; } } if (divisible && val >= gMin) { // DEBUG // std::cerr << val << std::endl; gSum++; } for (int i = 1; i < 10; ++i) { bool a[8]; memcpy(a, digits, 8 * sizeof(bool)); stack.push(Data(depth + 1, val, i, a)); } } int main() { gFuncs.push_back(&div2); gFuncs.push_back(&div3); gFuncs.push_back(&div4); gFuncs.push_back(&div5); gFuncs.push_back(&div6); gFuncs.push_back(&div7); gFuncs.push_back(&div8); gFuncs.push_back(&div9); std::cin >> gMin; std::cin >> gMax; // DEBUG // std::cerr << gMin << " " << gMax << std::endl; // std::stack<Data> stack; for (int i = 1; i <= 9; ++i) { bool a[8] = {}; stack.push(Data(0, 0, i, a)); } while (stack.empty() == false) { Data d = stack.top(); stack.pop(); recursion(stack, d.depth, d.val, d.digit, d.a); } // std::cerr << sum << std::endl; std::cout << gSum << std::endl; return 0; } |