1 solutions

  • 0
    @ 2026-7-29 10:16:14
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<"\n";return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int pt3[15][15][15][15]=
    {
    {
    {{0,0,0},{0,0,0},{0,0,0}},
    {{1,1,3,3},{1,2,3,4},{2,2,4,4}},
    {{1,1,4,3,3},{1,2,4,4,3},{1,2,2,2,3}},
    {{1,1,3,3,4,5},{1,2,3,4,4,5},{1,2,2,2,5,5}},
    {{1,1,3,3,3,6,6},{1,2,3,4,4,5,6},{1,2,2,2,4,5,5}},
    {{1,1,5,5,5,6,4,4},{1,2,5,6,6,6,3,4},{1,2,2,2,3,3,3,4}},
    {{1,1,5,5,5,6,7,7,4},{1,2,5,6,6,6,3,7,4},{1,2,2,2,3,3,3,4,4}},
    {{1,1,3,3,7,7,7,8,6,6},{1,2,3,4,7,8,8,8,5,6},{2,2,3,4,4,4,5,5,5,6}},
    {{1,1,5,5,5,6,7,7,4,8,8},{1,2,5,6,6,6,3,7,4,8,9},{1,2,2,2,3,3,3,4,4,9,9}}
    },
    {
    {{1,1,3,3},{1,2,3,4},{1,2,3,4},{2,2,4,4}},
    {{1,1,2,5,5},{1,2,2,5,6},{3,3,4,5,6},{3,4,4,6,6}},
    {{1,1,3,3,5,5},{1,2,3,4,5,6},{1,2,3,4,5,6},{2,2,4,4,6,6}},
    {{1,1,2,5,5,7,7},{1,2,2,5,6,7,8},{3,3,4,5,6,7,8},{3,4,4,6,6,8,8}},
    {{1,1,1,2,3,3,3,4},{1,2,2,2,3,4,4,4},{5,5,5,6,7,7,7,8},{5,6,6,6,7,8,8,8}},
    {{1,1,2,5,5,7,7,9,9},{1,2,2,5,6,7,8,9,10},{3,3,4,5,6,7,8,9,10},{3,4,4,6,6,8,8,10,10}},
    {{1,1,3,3,3,4,7,7,7,8},{1,2,3,4,4,4,7,8,8,8},{1,2,5,5,5,6,9,9,9,10},{2,2,5,6,6,6,9,10,10,10}},
    {{1,1,2,5,5,5,6,7,7,7,8},{1,2,2,5,6,6,6,7,8,8,8},{3,3,4,9,9,9,10,11,11,11,12},{3,4,4,9,10,10,10,11,12,12,12}}
    },
    {
    {{1,1,1,2,2},{1,3,3,3,2},{4,4,4,3,2},{4,5,6,7,7},{5,5,6,6,7}},
    {{1,1,4,4,7,7},{1,3,4,5,7,8},{1,3,4,5,8,8},{2,3,3,5,5,6},{2,2,2,6,6,6}},
    {{1,1,1,2,3,3,3},{1,2,2,2,3,4,4},{6,6,8,9,9,5,4},{6,7,8,8,9,5,4},{6,7,7,7,9,5,5}},
    {{1,1,5,5,5,6,4,4},{1,2,5,6,6,6,3,4},{1,2,2,2,3,3,3,4},{7,7,7,8,9,9,9,10},{7,8,8,8,9,10,10,10}},
    {{1,1,4,4,7,7,12,12,12},{1,2,4,5,5,7,12,11,11},{1,2,4,5,6,7,10,10,11},{2,2,3,5,6,8,10,9,9},{3,3,3,6,6,8,8,8,9}},
    {{1,1,4,4,7,8,8,8,10,10},{1,2,4,5,7,8,9,9,9,10},{1,2,4,5,7,7,11,12,9,10},{2,2,3,5,5,6,11,12,12,13},{3,3,3,6,6,6,11,11,13,13}},
    {{1,1,1,5,5,9,9,9,10,8,8},{1,2,2,5,6,9,10,10,10,7,8},{4,4,2,5,6,6,6,7,7,7,8},{3,4,2,11,11,11,12,13,13,13,14},{3,3,3,11,12,12,12,13,14,14,14}}
    },
    {
    {{1,1,2,3,3,4},{1,2,2,3,4,4},{5,5,7,7,9,9},{5,6,7,8,9,10},{5,6,7,8,9,10},{6,6,8,8,10,10}},
    {{1,1,1,6,6,6,7},{1,2,2,6,7,7,7},{3,3,2,8,8,8,9},{3,4,2,8,9,9,9},{4,4,5,10,10,10,11},{5,5,5,10,11,11,11}},
    {{1,1,1,2,3,3,3,4},{1,2,2,2,3,4,4,4},{5,5,5,6,7,7,7,8},{5,6,6,6,7,8,8,8},{9,9,9,10,11,11,11,12},{9,10,10,10,11,12,12,12}},
    {{1,1,1,2,2,9,9,9,10},{1,3,3,3,2,9,10,10,10},{4,4,4,3,2,11,11,11,12},{4,5,5,5,6,11,12,12,12},{7,7,8,5,6,13,13,13,14},{7,8,8,6,6,13,14,14,14}},
    {{1,1,5,5,5,6,11,11,11,12},{1,2,5,6,6,6,11,12,12,12},{2,2,7,7,7,8,13,13,13,14},{3,3,7,8,8,8,13,14,14,14},{3,4,9,9,9,10,15,15,15,16},{4,4,9,10,10,10,15,16,16,16}},
    {{1,1,1,6,6,6,7,8,8,8,9},{1,2,2,6,7,7,7,8,9,9,9},{3,3,2,10,10,10,11,12,12,12,13},{3,4,2,10,11,11,11,12,13,13,13},{4,4,5,14,14,14,15,16,16,16,17},{5,5,5,14,15,15,15,16,17,17,17}}
    },
    {
    {{1,1,1,2,3,3,3},{1,2,2,2,3,4,4},{7,7,6,5,5,5,4},{7,8,6,6,6,5,4},{7,8,10,10,12,12,13},{8,8,9,10,11,12,13},{9,9,9,11,11,13,13}},
    {{1,1,5,5,5,6,4,4},{1,2,5,6,6,6,3,4},{1,2,2,2,3,3,3,4},{7,7,7,8,9,9,9,10},{7,8,8,8,9,10,10,10},{11,11,11,12,13,13,13,14},{11,12,12,12,13,14,14,14}},
    {{1,1,1,2,3,3,3,4,5},{1,2,2,2,3,4,4,4,5},{7,7,7,8,9,10,6,5,5},{7,8,8,8,9,10,6,6,6},{16,16,12,9,9,10,10,11,11},{16,15,12,12,14,13,13,13,11},{16,15,15,15,14,14,14,13,11}},
    {{1,1,5,5,9,9,9,10,8,8},{1,2,5,6,9,10,10,10,7,8},{2,2,5,6,6,6,7,7,7,8},{3,3,11,11,11,12,15,15,15,16},{3,4,11,12,12,12,15,16,16,16},{3,4,13,13,13,14,17,17,17,18},{4,4,13,14,14,14,17,18,18,18}},
    {{1,1,1,7,7,11,11,11,12,10,10},{1,2,2,7,8,11,12,12,12,9,10},{3,3,2,7,8,8,8,9,9,9,10},{3,4,2,13,13,13,14,17,17,17,18},{3,4,4,13,14,14,14,17,18,18,18},{6,5,5,15,15,15,16,19,19,19,20},{6,6,5,15,16,16,16,19,20,20,20}}
    },
    {
    {{1,1,5,5,9,9,13,13},{1,2,5,6,9,10,13,14},{1,2,5,6,9,10,13,14},{2,2,6,6,10,10,14,14},{3,3,7,7,11,11,15,15},{3,4,7,8,11,12,15,16},{3,4,7,8,11,12,15,16},{4,4,8,8,12,12,16,16}},
    {{1,1,1,7,7,11,11,15,15},{1,2,2,7,8,11,12,15,16},{5,5,2,7,8,11,12,15,16},{5,6,2,8,8,12,12,16,16},{5,6,3,9,9,13,13,17,17},{6,6,3,9,10,13,14,17,18},{4,3,3,9,10,13,14,17,18},{4,4,4,10,10,14,14,18,18}},
    {{1,1,5,5,9,9,13,13,17,17},{1,2,5,6,9,10,13,14,17,18},{1,2,5,6,9,10,13,14,17,18},{2,2,6,6,10,10,14,14,18,18},{3,3,7,7,11,11,15,15,19,19},{3,4,7,8,11,12,15,16,19,20},{3,4,7,8,11,12,15,16,19,20},{4,4,8,8,12,12,16,16,20,20}},
    {{1,1,1,7,7,11,11,15,15,19,19},{1,2,2,7,8,11,12,15,16,19,20},{5,5,2,7,8,11,12,15,16,19,20},{5,6,2,8,8,12,12,16,16,20,20},{5,6,3,9,9,13,13,17,17,21,21},{6,6,3,9,10,13,14,17,18,21,22},{4,3,3,9,10,13,14,17,18,21,22},{4,4,4,10,10,14,14,18,18,22,22}}
    },
    {
    {{1,1,1,2,3,3,3,4,5},{1,2,2,2,3,4,4,4,5},{7,7,7,8,6,6,6,5,5},{7,8,8,8,6,9,9,9,10},{13,13,13,12,11,11,11,9,10},{14,14,13,12,12,12,11,10,10},{14,15,16,17,17,17,19,20,20},{14,15,16,17,18,19,19,21,20},{15,15,16,16,18,18,18,21,21}},
    {{1,1,4,4,7,8,8,8,10,10},{1,2,4,5,7,8,9,9,9,10},{1,2,4,5,7,7,11,12,9,10},{2,2,3,5,5,6,11,12,12,13},{3,3,3,6,6,6,11,11,13,13},{15,15,17,17,19,19,21,21,23,23},{15,16,17,18,19,20,21,22,23,14},{15,16,17,18,19,20,21,22,23,14},{16,16,18,18,20,20,22,22,14,14}},
    {{1,1,1,8,8,12,12,12,13,11,11},{1,2,2,8,9,12,13,13,13,10,11},{5,5,2,8,9,9,9,10,10,10,11},{5,6,2,14,14,14,15,20,20,20,21},{5,6,3,14,15,15,15,20,21,21,21},{6,6,3,16,16,16,17,22,22,22,23},{7,3,3,16,17,17,17,22,23,23,23},{7,7,4,18,18,18,19,24,24,24,25},{4,4,4,18,19,19,19,24,25,25,25}}
    },
    {
    {{1,1,2,3,3,4,17,17,17,18},{1,2,2,3,4,4,17,18,18,18},{5,5,7,7,9,9,19,19,19,20},{5,6,7,8,9,10,19,20,20,20},{5,6,7,8,9,10,21,21,21,22},{6,6,8,8,10,10,21,22,22,22},{11,11,13,13,15,15,23,23,23,24},{11,12,13,14,15,16,23,24,24,24},{11,12,13,14,15,16,25,25,25,26},{12,12,14,14,16,16,25,26,26,26}},
    {{1,1,2,3,3,3,4,5,5,5,6},{1,2,2,3,4,4,4,5,6,6,6},{7,7,7,13,13,15,15,17,17,19,19},{7,8,8,13,14,15,16,17,18,19,20},{11,11,8,13,14,15,16,17,18,19,20},{11,12,8,14,14,16,16,18,18,20,20},{11,12,9,21,21,23,23,25,25,27,27},{12,12,9,21,22,23,24,25,26,27,28},{10,9,9,21,22,23,24,25,26,27,28},{10,10,10,22,22,24,24,26,26,28,28}}
    },
    {
    {{1,1,5,5,5,6,7,7,4,8,8},{1,2,5,6,6,6,3,7,4,8,9},{1,2,2,2,3,3,3,4,4,9,9},{10,10,10,16,16,18,18,20,20,22,22},{10,11,11,16,17,18,19,20,21,22,23},{14,14,11,16,17,18,19,20,21,22,23},{14,15,11,17,17,19,19,21,21,23,23},{14,15,12,24,24,26,26,28,28,30,30},{15,15,12,24,25,26,27,28,29,30,31},{13,12,12,24,25,26,27,28,29,30,31},{13,13,13,25,25,27,27,29,29,31,31}}
    }
    }
    ;
    int ok5[6]={1,0,0,1,1,0};
    int n,m;
    int a[505][505];
    int pt1[2][8]={
    {0,0,0,1,2,2,2,3},
    {0,1,1,1,2,3,3,3}
    };
    int pt2[3][8]={
    {0,1,1,1,4,4,4,5},
    {0,1,2,3,3,3,4,5},
    {0,0,2,2,2,3,5,5}
    };
    void fill1(int x,int y,int t1,int t2,int &t){
    	if(t1==2){
    		REP(i,0,2)REP(j,0,8)a[i+x][j+y]=pt1[i][j]+t;
    		t+=4;
    	}else if(t1==3){
    		REP(i,0,3)REP(j,0,8)a[i+x][j+y]=pt2[i][j]+t;
    		t+=6;
    	}else if(t2==2){
    		REP(i,0,2)REP(j,0,8)a[j+x][i+y]=pt1[i][j]+t;
    		t+=4;
    	}else if(t2==3){
    		REP(i,0,3)REP(j,0,8)a[j+x][i+y]=pt2[i][j]+t;
    		t+=6;
    	}
    }
    void fill2(int x,int y,int t1,int t2,int &t){
    	if(t1==8){
    		REP(i,0,t2/2+1)if((t2-i*2)%3==0){
    			REP(j,0,i)fill1(x,y,8,2,t),y+=2;
    			REP(j,0,(t2-i*2)/3)fill1(x,y,8,3,t),y+=3;
    			break;
    		}
    	}else{
    		REP(i,0,t1/2+1)if((t1-i*2)%3==0){
    			REP(j,0,i)fill1(x,y,2,8,t),x+=2;
    			REP(j,0,(t1-i*2)/3)fill1(x,y,3,8,t),x+=3;
    			break;
    		}
    	}
    }
    void Main() {
    	cin>>n>>m;
    	if(n*m<=5&&!ok5[n*m])over("No")
    	if(n==1||m==1)over("No")
    	if(n==2){
    		if(m<=5&&!ok5[m])over("No")
    		cout<<"Yes\n";
    		for(int i=m/4;i>=0 ;--i)if((m-i*4)%3==0){
    			int t=1;
    			REP(j,0,i)cout<<t<<' '<<t<<' '<<t<<' '<<t+1<<' ',t+=2;
    			REP(j,0,(m-i*4)/3)cout<<t<<' '<<t<<' '<<t+1<<' ',t+=2;
    			t=1;cout<<'\n';
    			REP(j,0,i)cout<<t<<' '<<t+1<<' '<<t+1<<' '<<t+1<<' ',t+=2;
    			REP(j,0,(m-i*4)/3)cout<<t<<' '<<t+1<<' '<<t+1<<' ',t+=2;
    			t=0;cout<<'\n';
    			return;
    		}
    	}
    	if(m==2){
    		swap(n,m);
    		if(m<=5&&!ok5[m])over("No")
    		cout<<"Yes\n";
    		for(int i=m/4;i>=0 ;--i)if((m-i*4)%3==0){
    			int t=1;
    			REP(j,0,i)cout<<t<<' '<<t<<'\n'<<t<<' '<<t+1<<'\n'
    			<<t<<' '<<t+1<<'\n'<<t+1<<' '<<t+1<<'\n',t+=2;
    			REP(j,0,(m-i*4)/3)cout<<t<<' '<<t<<'\n'<<t<<' '<<t+1<<'\n'<<t+1<<' '<<t+1<<'\n',t+=2;
    			return;
    		}
    	}
    	if(n==3&&m==3)over("No")
    	bool fl=n>m;
    	if(fl)swap(n,m);
    	int N=n,M=m;
    	int t=1;
    	while(n>11){
    		fill2(n-8,0,8,m,t);
    		n-=8;
    	}
    	while(m>11){
    		fill2(0,m-8,n,8,t);
    		m-=8;
    	}
    	if(n<=m){
    		REP(i,0,n){
    			REP(j,0,m)a[i][j]=pt3[n-3][m-n][i][j]+t-1;
    		}
    	}else{
    		REP(i,0,n){
    			REP(j,0,m)a[i][j]=pt3[m-3][n-m][j][i]+t-1;
    		}
    	}
    	n=N;m=M;
    	cout<<"Yes"<<endl;
    	if(fl){
    		REP(i,0,m){REP(j,0,n)cout<<a[j][i]<<' ';cout<<'\n';}
    	}else{
    		REP(i,0,n){REP(j,0,m)cout<<a[i][j]<<' ';cout<<'\n';}
    	}
    }
    void TC() {
        int tc=1;
        cin>>tc;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("deepseek.in","r",stdin);
    	freopen("deepseek.out","w",stdout);
    //	freopen("1.ans","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    3055
    Time
    1000ms
    Memory
    1024MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By