//#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /*#pragma GCC optimize("Ofast") */ #pragma GCC optimize("O3") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #define endl '\n' #define X first #define Y second #define MAXN 32 #define MAXV 1048576 #define INF 1000010000 #define LLINF 1000000010000000000 #define ULLINF 10000000100000000000 #define MOD 1000000007 #define MAZNA 5030000 #define control cout<<" passed "<<'\n'; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vi; typedef vector vll; typedef vector vull; typedef vector vs; typedef pair pii; typedef pair pll; typedef pair pss; void speed() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } int rows[MAXN]; int cols[MAXN]; int n; ll next_block(ll c,ll a,ll b) { return ((c^a)+b); } ll next_rotation(ll f,ll d,ll e) { return ((f^d)+e); } vector ans; bool a[MAXN][MAXN]; pii shape[8][8][4]; void fill_shape() { shape[0][0][0]={0,0}; shape[0][0][1]={0,-1}; shape[0][0][2]={-1,0}; shape[0][0][3]={-1,-1}; shape[0][1][0]={0,0}; shape[0][1][1]={-1,0}; shape[0][1][2]={-1,1}; shape[0][1][3]={0,1}; shape[0][2][0]={0,0}; shape[0][2][1]={1,0}; shape[0][2][2]={0,1}; shape[0][2][3]={1,1}; shape[0][3][0]={0,0}; shape[0][3][1]={0,-1}; shape[0][3][2]={1,-1}; shape[0][3][3]={1,0}; shape[1][0][0]={0,0}; shape[1][0][1]={0,-1}; shape[1][0][2]={-1,0}; shape[1][0][3]={0,0}; shape[1][1][0]={0,0}; shape[1][1][1]={0,1}; shape[1][1][2]={-1,0}; shape[1][1][3]={0,0}; shape[1][2][0]={0,0}; shape[1][2][1]={1,0}; shape[1][2][2]={0,1}; shape[1][2][3]={0,0}; shape[1][3][0]={0,0}; shape[1][3][1]={0,-1}; shape[1][3][2]={1,0}; shape[1][3][3]={0,0}; shape[2][0][0]={0,0}; shape[2][0][1]={0,-1}; shape[2][0][2]={0,-2}; shape[2][0][3]={-1,0}; shape[2][1][0]={0,0}; shape[2][1][1]={-1,0}; shape[2][1][2]={-2,0}; shape[2][1][3]={0,1}; shape[2][2][0]={0,0}; shape[2][2][1]={1,0}; shape[2][2][2]={0,1}; shape[2][2][3]={0,2}; shape[2][3][0]={0,0}; shape[2][3][1]={1,0}; shape[2][3][2]={2,0}; shape[2][3][3]={0,-1}; shape[3][0][0]={0,0}; shape[3][0][1]={0,-1}; shape[3][0][2]={0,-2}; shape[3][0][3]={0,-3}; shape[3][1][0]={0,0}; shape[3][1][1]={-1,0}; shape[3][1][2]={-2,0}; shape[3][1][3]={-3,0}; shape[3][2][0]={0,0}; shape[3][2][1]={0,1}; shape[3][2][2]={0,2}; shape[3][2][3]={0,3}; shape[3][3][0]={0,0}; shape[3][3][1]={1,0}; shape[3][3][2]={2,0}; shape[3][3][3]={3,0}; shape[4][0][0]={0,0}; shape[4][0][1]={-1,0}; shape[4][0][2]={-2,0}; shape[4][0][3]={-1,-1}; shape[4][1][0]={0,0}; shape[4][1][1]={0,1}; shape[4][1][2]={0,2}; shape[4][1][3]={-1,1}; shape[4][2][0]={0,0}; shape[4][2][1]={1,0}; shape[4][2][2]={2,0}; shape[4][2][3]={1,1}; shape[4][3][0]={0,0}; shape[4][3][1]={0,-1}; shape[4][3][2]={0,-2}; shape[4][3][3]={1,-1}; } bool is_valid(ll x,ll y,int block,int rotation) { for(int i=0;i<=3;i++) { int r1,r2; r1=shape[block][rotation][i].X+x; r2=shape[block][rotation][i].Y+y;//cout<<"r1 = "<n)rows[r1]=n; if(cols[r2]>n)cols[r2]=n;*/ } /*for(int i=1;i<=n;i++) { if(rows[i]==n)brrow++; if(cols[i]==n)brcol++; }*/ br2+=brrow*n; br2=br2+(brcol*n)-brrow*brcol; for(int i=0;i<=3;i++) { int r1,r2; r1=shape[block][rotation][i].X+x; r2=shape[block][rotation][i].Y+y; if(a[r1][r2]) { a[r1][r2]=0; rows[r1]--; cols[r2]--; } /*if(rows[r1]<0)rows[r1]=0; if(cols[r2]<0)cols[r2]=0;*/ } return; } int used[MAXN][MAXN]; void fill_a() { for(int i=0;in||j<1||j>n) { a[i][j]=1; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { a[i][j]=0; } } } /// <--------------------------------------------------------------> bool nz[MAXN][MAXN]; ll onecnt(ll x,ll y,int block,int rotation) { int br=0; for(int i=0;i<=3;i++) { int r1,r2; r1=shape[block][rotation][i].X+x; r2=shape[block][rotation][i].Y+y; if(a[r1-1][r2]) { //nz[r1-1][r2]=1; br++; } if(a[r1][r2+1]) { //nz[r1][r2+1]=1; br++; } if(a[r1][r2-1]) { br++; } if(a[r1+1][r2]) { br++; } } if(n>8) { if(block==1) { if(rotation==0) { if(a[x-1][y-1]/*||a[x+1][y]==1&&a[x+1][y-1]==1&&a[x][y+1]==1&&a[x-1][y+1]==1*/)br+=20; //else if(a[x+1][y+1]&&a[x+1][y]&&a[x][y+1])br+=10; } else if(rotation==1) { if(a[x-1][y+1]/*||a[x+1][y]==1&&a[x+1][y+1]==1&&a[x][y-1]==1&&a[x-1][y+1]==1*/)br+=20; //else if(a[x+1][y-1]==1&&a[x][y-1]==1&&a[x+1][y]==1)br+=10; } else if(rotation==2) { if(a[x+1][y+1]/*||a[x-1][y]==1&&a[x-1][y+1]==1&&a[x][y-1]==1&&a[x+1][y-1]==1*/)br+=20; //else if(a[x-1][y-1]==1&&a[x][y-1]==1&&a[x-1][y]==1)br+=10; } else { if(a[x+1][y-1]/*||a[x-1][y]==1&&a[x-1][y-1]==1&&a[x][y+1]==1&&a[x+1][y+1]==1*/)br+=20; //else if(a[x-1][y+1]==1&&a[x][y+1]==1&&a[x-1][y]==1)br+=10; } } else if(block==4) { if(rotation==0) { if(a[x][y-1]&&a[x-2][y-1])br+=20; //else if(a[x+1][y+1]==1&&a[x+1][y]==1&&a[x][y+1]==1)br+=10; } else if(rotation==1) { if(a[x-1][y]&&a[x-1][y+2])br+=20; //else if(a[x+1][y-1]==1&&a[x][y-1]==1&&a[x+1][y]==1)br+=10; } else if(rotation==2) { if(a[x][y+1]&&a[x+2][y+1])br+=20; //else if(a[x-1][y-1]==1&&a[x][y-1]==1&&a[x-1][y]==1)br+=10; } else { if(a[x+1][y]&&a[x+1][y-2])br+=20; //else if(a[x-1][y+1]==1&&a[x][y+1]==1&&a[x-1][y]==1)br+=10; } } return br; } /*if(block==0) { int brup,brdwn,brl,brr; brup=brdwn=brl=brr=0; for(int i=0;i<=3;i++) { int r1,r2; r1=shape[block][rotation][i].X+x; r2=shape[block][rotation][i].Y+y; if(a[r1-1][r2]) { brup++; } if(a[r1][r2+1]) { brr++; } if(a[r1][r2-1]) { brl++; } if(a[r1+1][r2]) { brdwn++; } } if(brdwn>=2)br+=10; if(brr>=2)br+=10; if(brdwn>=2)br+=10; if(brl>=2)br+=10; }*/ if(block==2) { if(rotation==0) { if(a[x-1][y-1]==1&&a[x-1][y-2])br+=20; else if(a[x+1][y]&&a[x+1][y-1]&&a[x+1][y-2])br+=10; } else if(rotation==1) { if(a[x-1][y+1]==1&&a[x-2][y+1])br+=20; else if(a[x-1][y+1]&&a[x-2][y+1]&&a[x-1][y+1])br+=10; //else if(a[x+1][y-1]&&a[x][y-1]&&a[x+1][y])br+=10; } else if(rotation==2) { if(a[x+1][y+1]==1&&a[x+1][y+2])br+=20; if(a[x][y-1]&&a[x-1][y-1]&&a[x-2][y-1])br+=10; //else if(a[x-1][y-1]&&a[x][y-1]&&a[x-1][y])br+=10; } else { if(a[x+1][y-1]==1&&a[x+2][y-1])br+=20; else if(a[x][y+1]&&a[x+1][y+1]&&a[x+2][y+1])br+=10; //else if(a[x-1][y+1]&&a[x][y+1]&&a[x-1][y])br+=10; } } if(block==3) { if(rotation==0) { if(a[x-1][y-1]&&a[x-1][y-2]||a[x+1][y-1]&&a[x+1][y-2])br+=20; //if(a[x][y+1])br+=5; //if(a[x][y-4])br+=5; //else if(a[x+1][y+1]&&a[x+1][y]&&a[x][y+1])br+=10; } else if(rotation==1) { if(a[x-1][y+1]&&a[x-2][y+1]||a[x-1][y-1]&&a[x-2][y-1])br+=20; //if(a[x+1][y])br+=5; //if(a[x-4][y])br+=5; //else if(a[x+1][y-1]&&a[x][y-1]&&a[x+1][y])br+=10; } else if(rotation==2) { if(a[x+1][y+1]&&a[x+1][y+2]||a[x-1][y+1]&&a[x-1][y+2])br+=20; //if(a[x-1][y])br+=5; //if(a[x+4][y])br+=5; //else if(a[x-1][y-1]&&a[x][y-1]&&a[x-1][y])br+=10; } else { if(a[x+1][y-1]&&a[x+2][y-1]||a[x+1][y+1]&&a[x+2][y+1])br+=20; //if(a[x][y-1])br+=5; //if(a[x][y+4])br+=5; //else if(a[x-1][y+1]&&a[x][y+1]&&a[x-1][y])br+=10; } } else if(block==1) { if(rotation==0) { if(a[x-1][y-1]/*||a[x+1][y]==1&&a[x+1][y-1]==1&&a[x][y+1]==1&&a[x-1][y+1]==1*/)br+=20; //else if(a[x+1][y+1]&&a[x+1][y]&&a[x][y+1])br+=10; } else if(rotation==1) { if(a[x-1][y+1]/*||a[x+1][y]==1&&a[x+1][y+1]==1&&a[x][y-1]==1&&a[x-1][y+1]==1*/)br+=20; //else if(a[x+1][y-1]==1&&a[x][y-1]==1&&a[x+1][y]==1)br+=10; } else if(rotation==2) { if(a[x+1][y+1]/*||a[x-1][y]==1&&a[x-1][y+1]==1&&a[x][y-1]==1&&a[x+1][y-1]==1*/)br+=20; //else if(a[x-1][y-1]==1&&a[x][y-1]==1&&a[x-1][y]==1)br+=10; } else { if(a[x+1][y-1]/*||a[x-1][y]==1&&a[x-1][y-1]==1&&a[x][y+1]==1&&a[x+1][y+1]==1*/)br+=20; //else if(a[x-1][y+1]==1&&a[x][y+1]==1&&a[x-1][y]==1)br+=10; } } else if(block==4) { if(rotation==0) { if(a[x][y-1]&&a[x-2][y-1])br+=20; //else if(a[x+1][y+1]==1&&a[x+1][y]==1&&a[x][y+1]==1)br+=10; } else if(rotation==1) { if(a[x-1][y]&&a[x-1][y+2])br+=20; //else if(a[x+1][y-1]==1&&a[x][y-1]==1&&a[x+1][y]==1)br+=10; } else if(rotation==2) { if(a[x][y+1]&&a[x+2][y+1])br+=20; //else if(a[x-1][y-1]==1&&a[x][y-1]==1&&a[x-1][y]==1)br+=10; } else { if(a[x+1][y]&&a[x+1][y-2])br+=20; //else if(a[x-1][y+1]==1&&a[x][y+1]==1&&a[x-1][y]==1)br+=10; } } return br; } bool speciale_valid(ll x,ll y,ll ar,ll b,ll c,ll d,ll e,ll f) { int i,j; i=x; j=y; if(!is_valid(i,j,c%5,f%5))return false; c=next_block(c,ar,b); f=next_rotation(f,d,e); ll cc=next_block(c,ar,b); ll cf=next_rotation(f,d,e); ll ccc=next_block(cc,ar,b); ll ccf=next_rotation(cf,d,e); fill_move(i,j,c%5,f%5); bool used1[MAXN][MAXN]; bool used2[MAXN][MAXN]; memset(used1,0,sizeof(used1)); memset(used2,0,sizeof(used2)); for(int i1=1;i1<=n;i1++) { for(int j1=1;j1<=n;j1++) { if(!is_valid(i1,j1,cc%5,cf%5))continue; fill_move(i1,j1,cc%5,cf%5); for(int i2=1;i2<=n;i2++) { for(int j2=1;j2<=n;j2++) { if(is_valid(i2,j2,ccc%5,ccf%5)==1) { unfill_move(i1,j1,cc%5,cf%5); unfill_move(i,j,c%5,f%5); return true; } } } //unfill_move(i1,j1,cc%5,cf%5);*/ } } //unfill_move(i,j,c%5,f%5); return false; } void solve() { fill_a(); fill_shape(); ll ar,b,c,d,e,f; cin>>n>>ar>>b>>c>>d>>e>>f;int start=clock(); int br1=0; for(int i=1;i<=n;i++) { rows[i]=0; cols[i]=0; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { a[i][j]=0; } } /*if(n<=8){ while(1) { broi++; cleartable(); c=next_block(c,ar,b); f=next_rotation(f,d,e); bool lamp=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { used[i][j]=speciale_valid(i,j,ar,b,c,d,e,f); //cout<mar) { // br4=br3; mar=r; p={i,j}; } else if(br>ma&&mar==-1) { ma=br; p={i,j}; } if(c%5==1) { br3=3; } else { br1-=4; } } } /*for(int i=1;i<=n;i++) { /*if(rows[i]==n)brrow++; if(cols[i]==n)brcol++; if(broi) { cout<=MAZNA)break; /**for(int i=0;i<=n+1;i++) { for(int j=0;j<=n+1;j++) { cout<> nuli; bool lamp=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { used[i][j]=is_valid(i,j,c%5,f%4); //cout<mar) { // br4=br3; mar=r; p={i,j}; } else if(br>ma&&mar==-1) { ma=br; p={i,j}; } if(c%5==1) { br3=3; } else { br1-=4; } } } /*for(int i=1;i<=n;i++) { /*if(rows[i]==n)brrow++; if(cols[i]==n)brcol++; if(broi) { cout<=MAZNA)break; /**for(int i=0;i<=n+1;i++) { for(int j=0;j<=n+1;j++) { cout<