Advertisement
Guest User

Untitled

a guest
Feb 26th, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. // Приношу свои извинения за плохо оформленный код.
  2. // Он был написан впопыхах в некомфортных условиях...
  3. // Да еще и руками из жопы растущими.
  4.  
  5. #define _CRT_SECURE_NO_DEPRECATE
  6. #define _USE_MATH_DEFINES
  7.  
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <algorithm>
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <vector>
  14. #include <map>
  15. #include <set>
  16. #include <sstream>
  17. #include <cstring>
  18. #include <string>
  19. #include <queue>
  20.  
  21. #pragma comment(linker, "/STACK:64000000")
  22.  
  23. using namespace std;
  24.  
  25. typedef long long int64;
  26. typedef pair<int,int> pii;
  27. typedef pair<int,double> pid;
  28. typedef pair<double, double> pdd;
  29. typedef pair<int,pii> piii;
  30. typedef vector<int> vi;
  31. typedef vector<vi> vvi;
  32. typedef vector<pii> vpii;
  33. typedef vector<vpii> vvpii;
  34.  
  35. #define xn _dsfhsdfsj
  36. #define yn _dthsdfshj
  37.  
  38. #define problem "c"
  39.  
  40. int64 d;
  41. int n;
  42. int64 a[1 << 20];
  43. int c[1 << 20][3];
  44. int64 cur;
  45. int64 cnt1[1 << 7][1 << 7];
  46. int64 cnt2[1 << 7][1 << 7][1 << 7];
  47. int64 mods[1 << 7];
  48. vi b;
  49. int was[1 << 7];
  50.  
  51. void rec(int x)
  52. {
  53.     if (x == 3)
  54.     {
  55.         int cm = (int)(cur % d);
  56.         for (int i = 0; i < 3; ++i)
  57.         {
  58.             ++cnt1[cm][b[i]];
  59.             for (int j = 0; j < 3; ++j)
  60.             {
  61.                 if (i == j) continue;
  62.                 ++cnt2[cm][b[i]][b[j]];
  63.             }
  64.             c[n][i] = b[i];
  65.         }
  66.         a[n++] = cur;
  67.         return;
  68.     }
  69.     int64 tcur = cur;
  70.     for (int i = 10; i < 100; ++i)
  71.     {
  72.         if (was[i]) continue;
  73.         was[i] = 1;
  74.         b.push_back(i);
  75.         cur = tcur * 100 + i;
  76.         rec(x + 1);
  77.         was[i] = 0;
  78.         b.pop_back();
  79.     }
  80.     cur = tcur;
  81. }
  82.  
  83. inline int64 getCnt(int x, int y, int z, int m)
  84. {
  85.     int a[3];
  86.     a[0] = x;
  87.     a[1] = y;
  88.     a[2] = z;
  89.     int64 res = 0;
  90.     sort(a, a + 3);
  91.     do
  92.     {
  93.         int64 cur = a[0] * 10000 + a[1] * 100 + a[2];
  94.         if (cur % d == m) ++res;
  95.     } while (next_permutation(a, a + 3));
  96.     return res;
  97. }
  98.  
  99. int main()
  100. {
  101.     freopen(problem ".in", "r", stdin); freopen(problem ".out", "w", stdout);
  102.  
  103.     memset(cnt1, 0, sizeof cnt1);
  104.     memset(cnt2, 0, sizeof cnt2);
  105.     cin >> d;
  106.  
  107.     memset(was, 0, sizeof was);
  108.     cur = 0;
  109.     n = 0;
  110.     rec(0);
  111.  
  112.     memset(mods, 0, sizeof mods);
  113.     for (int i = 0; i < n; ++i)
  114.     {
  115.         int64 x = a[i] % d;
  116.         ++mods[x];
  117.     }
  118.     int64 res = 0;
  119.     for (int i = 0; i < n; ++i)
  120.     {
  121.         int64 x = (a[i] * 1000000LL) % d;
  122.         int y = (int)((d - x) % d);
  123.         int64 cur = mods[y];
  124.         cur -= cnt1[y][c[i][0]];
  125.         cur -= cnt1[y][c[i][1]];
  126.         cur -= cnt1[y][c[i][2]];
  127.         cur += cnt2[y][c[i][0]][c[i][1]];
  128.         cur += cnt2[y][c[i][0]][c[i][2]];
  129.         cur += cnt2[y][c[i][1]][c[i][2]];
  130.         cur -= getCnt(c[i][0], c[i][1], c[i][2], y);
  131.         res += cur;
  132.     }
  133.  
  134.     cout << res << endl;
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement