Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- /// Typedef
- typedef long long LL;
- typedef vector<int> vi;
- typedef pair<int,int> pii;
- #define pb push_back
- #define ppb pop_back
- #define MP make_pair
- #define ff first
- #define ss second
- #define sf scanf
- #define pf printf
- #define SQR(x) ((x)*(x))
- #define loop(i, y) for(int i=0; i<int(y); i++)
- #define FOR(i, x, y) for(int i=int(x); i<=int(y); i++)
- #define ROF(i, x, y) for(int i=int(x); i>=int(y); i--)
- #define all(c) c.begin(), c.end()
- #define sz(c) int(c.size())
- #define clr(x, y) memset(x, y, sizeof(x))
- #define si(x) scanf("%d", &x)
- #define sii(x, y) scanf("%d %d", &x, &y)
- #define siii(x, y, z) scanf("%d %d %d", &x, &y, &z)
- #define sl(x) scanf("%lld", &x)
- #define sll(x, y) scanf("%lld %lld", &x, &y)
- #define slll(x, y, z) scanf("%lld %lld %lld", &x, &y, &z)
- #ifdef VAMP
- #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
- template < typename Arg1 >
- void __f(const char* name, Arg1&& arg1){
- cout << name << " = " << arg1 << std::endl;
- }
- template < typename Arg1, typename... Args>
- void __f(const char* names, Arg1&& arg1, Args&&... args){
- const char* comma = strchr(names+1, ',');
- cout.write(names, comma - names) << " = " << arg1 <<" | ";
- __f(comma+1, args...);
- }
- #else
- #define debug(...)
- #endif
- ///******************************************START******************************************
- // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
- // #define new_ran(a, b) uniform_int_distribution<LL> (a, b)(rng)
- // #define ran_shuffle(x) shuffle(x.begin(), x.end(), rng)
- 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;}
- /// Constants
- #define MAX 200005
- #define MOD 1000000007
- #define eps 1e-9
- #define INF 0x3f3f3f3f3f3f3f3f // 4,557,430,888,798,830,399
- #define inf 0x3f3f3f3f // 1,061,109,567
- #define PI acos(-1.0) // 3.1415926535897932
- #define VT long long
- bool bad[5][5];
- int g[5][5];
- VT DP[4][3][51][1<<12];
- bool ok(int r, int c, int rr, int cc, int mask){
- int dr = rr - r;
- int dc = cc - c;
- int gc = g[abs(dr)][abs(dc)];
- dr /= gc;
- dc /= gc;
- for(int i = 1; r + i * dr != rr or c + i * dc != cc; i++){
- int nr = r + i * dr;
- int nc = c + i * dc;
- if(bad[nr][nc] or !(mask & (1<<(nr * 3 + nc)))) return false;
- }
- return true;
- }
- VT solve(int r, int c, int lft, int mask){
- if(lft == 0) return 1;
- auto &ret = DP[r][c][lft][mask];
- if(~ret) return ret;
- ret = 0;
- FOR(i, 0, 11){
- int rr = i/3;
- int cc = i%3;
- if((mask>>i)&1) continue;
- if(bad[rr][cc]) continue;
- if(lft - abs(rr - r) - abs(cc - c) < 0) continue;
- if(ok(r, c, rr, cc, mask)) ret += solve(rr, cc, lft - abs(rr - r) - abs(cc - c), mask | (1<<(rr*3+cc)));
- }
- return ret;
- }
- int main(){
- #ifdef VAMP
- clock_t tStart = clock();
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- clr(DP, -1);
- FOR(i, 0, 4) FOR(j, 0, 4){
- g[i][j] = __gcd(i, j);
- }
- int l, n; sii(l, n);
- if(l>50) printf("BAD MEMORY\n"), exit(0);
- FOR(i, 0, n-1){
- int x, y; sii(y, x);
- x--, y--;
- bad[x][y] = 1;
- }
- VT ans = 0;
- FOR(i, 0, 3) FOR(j, 0, 2){
- if(!bad[i][j]){
- auto res = solve(i, j, l, 1<<(i*3+j));
- ans += res;
- }
- }
- if(!ans) printf("BAD MEMORY\n");
- else printf("%lld\n", ans);
Advertisement
Add Comment
Please, Sign In to add comment