Advertisement
Guest User

Untitled

a guest
Dec 17th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4. using namespace std;
  5. #define f first
  6. #define s second
  7. #define mp make_pair
  8. #define pb push_back
  9. #define INF (1<<30)
  10. #define ll long long
  11. const int N = 25;
  12. ll f(ll x, int k)
  13. {
  14.   if (x == 0) return 0;
  15.   int c[N] = {0}, len = 0;
  16.   ll ans = 0, dp[4][N][11][2] = {0};
  17.   while (x)
  18.   {
  19.     c[++len] = x % 10;
  20.     x /= 10;
  21.   }
  22.   for (int i = 1; i <= len / 2; i++) swap(c[i], c[len - i + 1]);
  23.  
  24.   for (int l = 1; l <= len; l++)
  25.     for (int cf = 1; cf < 10; cf++)
  26.     {
  27.       int v = 1, p;
  28.       while (v <= l && cf == c[v]) v++;
  29.       if (v == l + 1) p = 1;
  30.       else if (cf > c[v]) p = 2;
  31.       else p = 0;
  32.       dp[p][l][cf][l >= k] = 1;
  33.     }
  34.   for (int l = 1; l <= len; l++)
  35.     for (int p = 0; p < 3; p++)
  36.       for (int cf = 0; cf < 10; cf++)
  37.         for (int g = 0; g < 2; g++)
  38.           for (int nc = 0; nc < 10; nc++)
  39.             if (nc != cf)
  40.             {
  41.               int top = p;
  42.               for (int add = 1; add <= len - l; add++)
  43.               {
  44.                 if (top == 1 && nc > c[l + add]) top = 2;
  45.                 else if (top == 1 && nc < c[l + add]) top = 0;
  46.                 dp[top][l + add][nc][g | (add >= k)] += dp[p][l][cf][g];
  47.               }
  48.             }
  49.   for (int l = 1; l <= len; l++)
  50.     for (int p = 0; p < (l == len ? 2: 3); p++)
  51.       for (int cf = 0; cf < 10; cf++) ans += dp[p][l][cf][1];
  52.   return ans;
  53. }
  54.  
  55. int main()
  56. {
  57.   //freopen("interesting.in", "r", stdin);
  58.   //freopen("interesting.out", "w", stdout);
  59.   ll a, b;
  60.   int k;
  61.   cin >> a >> b >> k;
  62.   cout << f(b, k) - f(a - 1, k);
  63.   return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement