Advertisement
Mike4235

LAH20_CIFRE

Mar 23rd, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. /*#pragma GCC optimize("Ofast")
  2. #pragma GCC optimize("unroll-loops")*/
  3. // only when really needed
  4.  
  5. /* GNU G++17 7.3.0: No long long for faster code
  6.    GNU G++17 9.2.0 (64 bit, msys 2): Long long only for faster code */
  7.  
  8. #include <bits/stdc++.h>
  9.  
  10. #define for1(i,a,b) for (int i = a; i <= b; i++)
  11. #define for2(i,a,b) for (int i = a; i >= b; i--)
  12. #define int long long
  13.  
  14. #define sz(a) (int)a.size()
  15. #define pii pair<int,int>
  16.  
  17. /*
  18. __builtin_popcountll(x) : Number of 1-bit
  19. __builtin_ctzll(x) : Number of trailing 0
  20. */
  21.  
  22. #define PI 3.1415926535897932384626433832795
  23. #define INF 1000000000000000000
  24. #define MOD 1000000007
  25. #define MOD2 1000000009
  26. #define EPS 1e-6
  27.  
  28. using namespace std;
  29.  
  30. int d[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  31. vector<int> v[10] = {{0, 8}, {0, 1, 3, 4, 7, 8, 9}, {2, 8}, {3, 8, 9}, {4, 8, 9}, {5, 6, 8, 9}, {6, 8}, {7, 0, 3, 8, 9}, {8}, {8, 9}};
  32. int t, ans, cnt;
  33. string n, s = "";
  34. bool flag = false;
  35.  
  36. void back(int pos) {
  37.    
  38.     if (pos == sz(n)) {
  39.         if (cnt && s > n) ans++;
  40.         return;
  41.     }
  42.    
  43.     for (auto f : v[n[pos] - '0']) {
  44.        
  45.         if (pos > 0 && !flag && f < n[pos] - '0') continue;
  46.         if (pos == 0 && f < n[pos] - '0') continue;
  47.        
  48.         bool oflag = flag;
  49.        
  50.         s += char(f + '0');
  51.         if (f != n[pos] - '0') cnt++;
  52.         if (f > n[pos] - '0') flag = true;
  53.        
  54.         back(pos + 1);
  55.        
  56.         s.erase(s.end() - 1);
  57.         if (f != n[pos] - '0') cnt--;
  58.         flag = oflag;
  59.        
  60.     }
  61.    
  62. }
  63.  
  64. signed main() {
  65.    
  66.     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  67.    
  68.     freopen("CIFRE.inp", "r", stdin);
  69.     freopen("CIFRE.out", "w", stdout);
  70.    
  71.     cin >> t >> n;
  72.    
  73.     if (t == 1) {
  74.         for (auto i : n) ans += d[i - '0'];
  75.         cout << ans;
  76.     }
  77.    
  78.     else {
  79.         back(0);
  80.         cout << ans;
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement