AhmedAshraff

Untitled

Nov 25th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. /// Typedef
  4. typedef long long           LL;
  5. typedef vector<int>         vi;
  6. typedef pair<int,int>       pii;
  7.  
  8. #define pb                  push_back
  9. #define ppb                 pop_back
  10. #define MP                  make_pair
  11. #define ff                  first
  12. #define ss                  second
  13. #define sf                  scanf
  14. #define pf                  printf
  15. #define SQR(x)              ((x)*(x))
  16. #define loop(i, y)          for(int i=0; i<int(y); i++)
  17. #define FOR(i, x, y)        for(int i=int(x); i<=int(y); i++)
  18. #define ROF(i, x, y)        for(int i=int(x); i>=int(y); i--)
  19. #define all(c)              c.begin(), c.end()
  20. #define sz(c)               int(c.size())
  21. #define clr(x, y)           memset(x, y, sizeof(x))
  22. #define si(x)               scanf("%d", &x)
  23. #define sii(x, y)           scanf("%d %d", &x, &y)
  24. #define siii(x, y, z)       scanf("%d %d %d", &x, &y, &z)
  25. #define sl(x)               scanf("%lld", &x)
  26. #define sll(x, y)           scanf("%lld %lld", &x, &y)
  27. #define slll(x, y, z)       scanf("%lld %lld %lld", &x, &y, &z)
  28.  
  29. #ifdef VAMP
  30.      #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
  31.     template < typename Arg1 >
  32.     void __f(const char* name, Arg1&& arg1){
  33.         cout << name << " = " << arg1 << std::endl;
  34.     }
  35.     template < typename Arg1, typename... Args>
  36.     void __f(const char* names, Arg1&& arg1, Args&&... args){
  37.         const char* comma = strchr(names+1, ',');
  38.         cout.write(names, comma - names) << " = " << arg1 <<" | ";
  39.         __f(comma+1, args...);
  40.     }
  41. #else
  42.     #define debug(...)
  43. #endif
  44. ///******************************************START******************************************
  45.  
  46. // mt19937_64                  rng(chrono::steady_clock::now().time_since_epoch().count());
  47. // #define new_ran(a, b)       uniform_int_distribution<LL> (a, b)(rng)
  48. // #define ran_shuffle(x)      shuffle(x.begin(), x.end(), rng)
  49. template <class T, class L> inline T bigMod(T p,T e,L M){ LL ret = 1; for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = ((LL)p * p) % M; } return (L) ret;}
  50.  
  51. /// Constants
  52. #define MAX                 200005
  53. #define MOD                 1000000007
  54. #define eps                 1e-9
  55. #define INF                 0x3f3f3f3f3f3f3f3f // 4,557,430,888,798,830,399
  56. #define inf                 0x3f3f3f3f // 1,061,109,567
  57. #define PI                  acos(-1.0)  // 3.1415926535897932
  58. #define VT                  long long
  59.  
  60. bool bad[5][5];
  61. int g[5][5];
  62.  
  63. VT DP[4][3][51][1<<12];
  64.  
  65. bool ok(int r, int c, int rr, int cc, int mask){
  66.     int dr = rr - r;
  67.     int dc = cc - c;
  68.     int gc = g[abs(dr)][abs(dc)];
  69.     dr /= gc;
  70.     dc /= gc;
  71.  
  72.     for(int i = 1; r + i * dr != rr or c + i * dc != cc; i++){
  73.         int nr = r + i * dr;
  74.         int nc = c + i * dc;
  75.         if(bad[nr][nc] or !(mask & (1<<(nr * 3 + nc)))) return false;
  76.     }
  77.     return true;
  78. }
  79.  
  80. VT solve(int r, int c, int lft, int mask){
  81.     if(lft == 0) return 1;
  82.  
  83.     auto &ret = DP[r][c][lft][mask];
  84.     if(~ret) return ret;
  85.     ret = 0;
  86.  
  87.     FOR(i, 0, 11){
  88.         int rr = i/3;
  89.         int cc = i%3;
  90.         if((mask>>i)&1) continue;
  91.         if(bad[rr][cc]) continue;
  92.         if(lft - abs(rr - r) - abs(cc - c) < 0) continue;
  93.         if(ok(r, c, rr, cc, mask)) ret += solve(rr, cc, lft - abs(rr - r) - abs(cc - c), mask | (1<<(rr*3+cc)));
  94.     }
  95.     return ret;
  96. }
  97.  
  98. int main(){
  99.     #ifdef VAMP
  100.         clock_t tStart = clock();
  101.         freopen("input.txt", "r", stdin);
  102.         freopen("output.txt", "w", stdout);
  103.     #endif
  104.     clr(DP, -1);
  105.  
  106.     FOR(i, 0, 4) FOR(j, 0, 4){
  107.         g[i][j] = __gcd(i, j);
  108.     }
  109.  
  110.     int l, n; sii(l, n);
  111.     if(l>50) printf("BAD MEMORY\n"), exit(0);
  112.     FOR(i, 0, n-1){
  113.         int x, y; sii(y, x);
  114.         x--, y--;
  115.         bad[x][y] = 1;
  116.     }
  117.  
  118.     VT ans = 0;
  119.     FOR(i, 0, 3) FOR(j, 0, 2){
  120.         if(!bad[i][j]){
  121.             auto res = solve(i, j, l, 1<<(i*3+j));
  122.             ans += res;
  123.         }
  124.     }
  125.     if(!ans) printf("BAD MEMORY\n");
  126.     else printf("%lld\n", ans);
  127.  
Advertisement
Add Comment
Please, Sign In to add comment