#include <iostream> #include <vector> #include <math.h> long int perm(int ns, int np) { int n = ns+np; long int u = 1; long int l = 1; for (int i=ns+1; i<=n; i++) u *= i; for (int i=2; i<=np; i++) l *= i; return u/l; } long int calculateOnlyOnes(int ones) { long int ret = pow(2,ones); for (int i=1; i <= ones/2; i++) { int s = ones-i*2; ret += perm(s, i)*pow(2, s)*pow(8,i); } return ret; } long int solveOne(int ones, int r) { long int ret = 1; // std::cout << std::endl << "solveOne for: " << ones << " and r " << r << std::endl; if (r==10) { // ones++; ret = calculateOnlyOnes(ones); } else { ret = calculateOnlyOnes(ones)*(r+1) + calculateOnlyOnes(ones-1)*(9-r); } return ret; } int solve(long int n) { long int c = n; int sol = 1; int ones = 0; int previous = 10; int lastStr = 10; while (c > 0) { if (c%10 == 1 && (previous != 9)) { if (ones==0) lastStr = previous; ones++; } else { if (ones > 0) { sol *= solveOne(ones, lastStr); ones = 0; previous = 10; } else if(previous != 10) { sol *= (previous+1); } } previous = (c%10); c /= 10; } if (ones > 0) { sol *= solveOne(ones,lastStr); } else { sol *= (previous+1); } return sol; } int main() { long int n; std::cin >> n; std::cout << solve(n) << std::endl; /* std::vector<long int> test = {112, 34512, 211121314, 2323, 22, 23, 19, 11, 211}; for (auto& x : test) { std::cout << "For " << x << ": " << solve(x) << 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 | #include <iostream> #include <vector> #include <math.h> long int perm(int ns, int np) { int n = ns+np; long int u = 1; long int l = 1; for (int i=ns+1; i<=n; i++) u *= i; for (int i=2; i<=np; i++) l *= i; return u/l; } long int calculateOnlyOnes(int ones) { long int ret = pow(2,ones); for (int i=1; i <= ones/2; i++) { int s = ones-i*2; ret += perm(s, i)*pow(2, s)*pow(8,i); } return ret; } long int solveOne(int ones, int r) { long int ret = 1; // std::cout << std::endl << "solveOne for: " << ones << " and r " << r << std::endl; if (r==10) { // ones++; ret = calculateOnlyOnes(ones); } else { ret = calculateOnlyOnes(ones)*(r+1) + calculateOnlyOnes(ones-1)*(9-r); } return ret; } int solve(long int n) { long int c = n; int sol = 1; int ones = 0; int previous = 10; int lastStr = 10; while (c > 0) { if (c%10 == 1 && (previous != 9)) { if (ones==0) lastStr = previous; ones++; } else { if (ones > 0) { sol *= solveOne(ones, lastStr); ones = 0; previous = 10; } else if(previous != 10) { sol *= (previous+1); } } previous = (c%10); c /= 10; } if (ones > 0) { sol *= solveOne(ones,lastStr); } else { sol *= (previous+1); } return sol; } int main() { long int n; std::cin >> n; std::cout << solve(n) << std::endl; /* std::vector<long int> test = {112, 34512, 211121314, 2323, 22, 23, 19, 11, 211}; for (auto& x : test) { std::cout << "For " << x << ": " << solve(x) << std::endl; } */ return 0; } |