QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#731129#9485. (mod N² + 1)TheZoneCompile Error//C++1420.7kb2024-11-10 00:02:062024-11-10 00:02:07

Judging History

你现在查看的是最新测评结果

  • [2024-11-10 00:02:07]
  • 评测
  • [2024-11-10 00:02:06]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
int mod;
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

/*

*/

int n,r;
vi o={2,8,9,15};
int g=3;
int lg[maxn],pw[maxn];

bool isp(int p){
    For(i,2,p-1) if(p%i==0) return 0;
    return 1;
}
void sols(){
    if(n==2){
        if(r==4) {
            puts("Yes");
            puts("1 2");
            puts("3 4");
            return;
        }
        puts("No");
        return;
    }

    if(n==3){
        if(r==0){
            puts("Yes");
            int idx=0;
            For(i,1,3)For(j,1,3)cout<<(++idx)<<" \n"[j==3];
            return;
        }
    }

    puts("No");
    return;
}

int mat[405][405]={

{16 ,14 ,1 ,11},
{5,15,4,2},
{8,6,9,3},
{13,7,12,10}
};

int a[405][405];

void work()
{
    n=read();
    mod=n*n+1;
    r=read();
    int p=n*n+1;
    if(!isp(p) || n==2){
        sols();
        return;
    }

    bool qwq=0;
    for(g=2;g<n*n;++g){
        int t=1;
        For(i,0,n*n) lg[i]=0;
        lg[1]=0;
        For(j,1,n*n-1){
            t=t*g%mod;
            lg[t]=j;
        }
    //    For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
        bool ok=1;
        For(i,2,n*n) if(!lg[i]) ok=0;
        if(ok){

pw[0]=1;
        For(i,1,n*n) pw[i]=pw[i-1]*g%mod;
        qwq=1;            break;
        }
    }
    cerr<<"qwq "<<qwq<<"\n";
    cerr<<"g: "<<g<<"\n";
  //  For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
   // For(i,0,n*n) cout<<pw[i]<<" "; cout<<"\n";
    assert(qwq);
    {
        int idx=-1;
        For(i,1,n)For(j,1,n){
            if(i%2==1 && j%2==1){
                a[i][j]=++idx;
                a[i][j+1]=n*n-idx-1;
            }
            if(i%2==0 && j%2==0){
                a[i][j]=++idx;
                a[i][j-1]=n*n-idx-1;
            }
        }
        r=lg[r];

        if(r%4!=2){
            puts("No");
            return;
        }

        int add=(r-(n*n-2)+n*n*2)%(n*n);
        add/=4;
        For(i,1,n)For(j,1,n){
            a[i][j]+=add;
            a[i][j]%=(n*n);
        }
        
        puts("Yes");
        For(i,1,n)For(j,1,n) cout<<pw[a[i][j]]<<" \n"[j==n];
    }
}


signed main()
{
    int T=read();
    while(T--)work();
	return 0;
}
/*#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
int mod;
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

/*

*/

int n,r;
vi o={2,8,9,15};
int g=3;
int lg[maxn],pw[maxn];

bool isp(int p){
    For(i,2,p-1) if(p%i==0) return 0;
    return 1;
}
void sols(){
    if(n==2){
        if(r==4) {
            puts("Yes");
            puts("1 2");
            puts("3 4");
            return;
        }
        puts("No");
        return;
    }

    if(n==3){
        if(r==0){
            puts("Yes");
            int idx=0;
            For(i,1,3)For(j,1,3)cout<<(++idx)<<" \n"[j==3];
            return;
        }
    }

    puts("No");
    return;
}

int mat[405][405]={

{16 ,14 ,1 ,11},
{5,15,4,2},
{8,6,9,3},
{13,7,12,10}
};

int a[405][405];

void work()
{
    n=read();
    mod=n*n+1;
    r=read();
    int p=n*n+1;
    if(!isp(p) || n==2){
        sols();
        return;
    }

    bool qwq=0;
    for(g=2;g<n*n;++g){
        int t=1;
        For(i,0,n*n) lg[i]=0;
        lg[1]=0;
        For(j,1,n*n-1){
            t=t*g%mod;
            lg[t]=j;
        }
    //    For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
        bool ok=1;
        For(i,2,n*n) if(!lg[i]) ok=0;
        if(ok){

pw[0]=1;
        For(i,1,n*n) pw[i]=pw[i-1]*g%mod;
        qwq=1;            break;
        }
    }
    cerr<<"qwq "<<qwq<<"\n";
    cerr<<"g: "<<g<<"\n";
  //  For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
   // For(i,0,n*n) cout<<pw[i]<<" "; cout<<"\n";
    assert(qwq);
    {
        int idx=-1;
        For(i,1,n)For(j,1,n){
            if(i%2==1 && j%2==1){
                a[i][j]=++idx;
                a[i][j+1]=n*n-idx-1;
            }
            if(i%2==0 && j%2==0){
                a[i][j]=++idx;
                a[i][j-1]=n*n-idx-1;
            }
        }
        r=lg[r];

        if(r%4!=2){
            puts("No");
            return;
        }

        int add=(r-(n*n-2)+n*n*2)%(n*n);
        add/=4;
        For(i,1,n)For(j,1,n){
            a[i][j]+=add;
            a[i][j]%=(n*n);
        }
        
        puts("Yes");
        For(i,1,n)For(j,1,n) cout<<pw[a[i][j]]<<" \n"[j==n];
    }
}


signed main()
{
    int T=read();
    while(T--)work();
	return 0;
}#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
int mod;
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

/*

*/

int n,r;
vi o={2,8,9,15};
int g=3;
int lg[maxn],pw[maxn];

bool isp(int p){
    For(i,2,p-1) if(p%i==0) return 0;
    return 1;
}
void sols(){
    if(n==2){
        if(r==4) {
            puts("Yes");
            puts("1 2");
            puts("3 4");
            return;
        }
        puts("No");
        return;
    }

    if(n==3){
        if(r==0){
            puts("Yes");
            int idx=0;
            For(i,1,3)For(j,1,3)cout<<(++idx)<<" \n"[j==3];
            return;
        }
    }

    puts("No");
    return;
}

int mat[405][405]={

{16 ,14 ,1 ,11},
{5,15,4,2},
{8,6,9,3},
{13,7,12,10}
};

int a[405][405];

void work()
{
    n=read();
    mod=n*n+1;
    r=read();
    int p=n*n+1;
    if(!isp(p) || n==2){
        sols();
        return;
    }

    bool qwq=0;
    for(g=2;g<n*n;++g){
        int t=1;
        For(i,0,n*n) lg[i]=0;
        lg[1]=0;
        For(j,1,n*n-1){
            t=t*g%mod;
            lg[t]=j;
        }
    //    For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
        bool ok=1;
        For(i,2,n*n) if(!lg[i]) ok=0;
        if(ok){

pw[0]=1;
        For(i,1,n*n) pw[i]=pw[i-1]*g%mod;
        qwq=1;            break;
        }
    }
    cerr<<"qwq "<<qwq<<"\n";
    cerr<<"g: "<<g<<"\n";
  //  For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
   // For(i,0,n*n) cout<<pw[i]<<" "; cout<<"\n";
    assert(qwq);
    {
        int idx=-1;
        For(i,1,n)For(j,1,n){
            if(i%2==1 && j%2==1){
                a[i][j]=++idx;
                a[i][j+1]=n*n-idx-1;
            }
            if(i%2==0 && j%2==0){
                a[i][j]=++idx;
                a[i][j-1]=n*n-idx-1;
            }
        }
        r=lg[r];

        if(r%4!=2){
            puts("No");
            return;
        }

        int add=(r-(n*n-2)+n*n*2)%(n*n);
        add/=4;
        For(i,1,n)For(j,1,n){
            a[i][j]+=add;
            a[i][j]%=(n*n);
        }
        
        puts("Yes");
        For(i,1,n)For(j,1,n) cout<<pw[a[i][j]]<<" \n"[j==n];
    }
}


signed main()
{
    int T=read();
    while(T--)work();
	return 0;
}#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
int mod;
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

/*

*/

int n,r;
vi o={2,8,9,15};
int g=3;
int lg[maxn],pw[maxn];

bool isp(int p){
    For(i,2,p-1) if(p%i==0) return 0;
    return 1;
}
void sols(){
    if(n==2){
        if(r==4) {
            puts("Yes");
            puts("1 2");
            puts("3 4");
            return;
        }
        puts("No");
        return;
    }

    if(n==3){
        if(r==0){
            puts("Yes");
            int idx=0;
            For(i,1,3)For(j,1,3)cout<<(++idx)<<" \n"[j==3];
            return;
        }
    }

    puts("No");
    return;
}

int mat[405][405]={

{16 ,14 ,1 ,11},
{5,15,4,2},
{8,6,9,3},
{13,7,12,10}
};

int a[405][405];

void work()
{
    n=read();
    mod=n*n+1;
    r=read();
    int p=n*n+1;
    if(!isp(p) || n==2){
        sols();
        return;
    }

    bool qwq=0;
    for(g=2;g<n*n;++g){
        int t=1;
        For(i,0,n*n) lg[i]=0;
        lg[1]=0;
        For(j,1,n*n-1){
            t=t*g%mod;
            lg[t]=j;
        }
    //    For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
        bool ok=1;
        For(i,2,n*n) if(!lg[i]) ok=0;
        if(ok){

pw[0]=1;
        For(i,1,n*n) pw[i]=pw[i-1]*g%mod;
        qwq=1;            break;
        }
    }
    cerr<<"qwq "<<qwq<<"\n";
    cerr<<"g: "<<g<<"\n";
  //  For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
   // For(i,0,n*n) cout<<pw[i]<<" "; cout<<"\n";
    assert(qwq);
    {
        int idx=-1;
        For(i,1,n)For(j,1,n){
            if(i%2==1 && j%2==1){
                a[i][j]=++idx;
                a[i][j+1]=n*n-idx-1;
            }
            if(i%2==0 && j%2==0){
                a[i][j]=++idx;
                a[i][j-1]=n*n-idx-1;
            }
        }
        r=lg[r];

        if(r%4!=2){
            puts("No");
            return;
        }

        int add=(r-(n*n-2)+n*n*2)%(n*n);
        add/=4;
        For(i,1,n)For(j,1,n){
            a[i][j]+=add;
            a[i][j]%=(n*n);
        }
        
        puts("Yes");
        For(i,1,n)For(j,1,n) cout<<pw[a[i][j]]<<" \n"[j==n];
    }
}


signed main()
{
    int T=read();
    while(T--)work();
	return 0;
}#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	return f?-x:x;
}
int mod;
struct modint{
	unsigned int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 1000005
#define inf 0x3f3f3f3f

/*

*/

int n,r;
vi o={2,8,9,15};
int g=3;
int lg[maxn],pw[maxn];

bool isp(int p){
    For(i,2,p-1) if(p%i==0) return 0;
    return 1;
}
void sols(){
    if(n==2){
        if(r==4) {
            puts("Yes");
            puts("1 2");
            puts("3 4");
            return;
        }
        puts("No");
        return;
    }

    if(n==3){
        if(r==0){
            puts("Yes");
            int idx=0;
            For(i,1,3)For(j,1,3)cout<<(++idx)<<" \n"[j==3];
            return;
        }
    }

    puts("No");
    return;
}

int mat[405][405]={

{16 ,14 ,1 ,11},
{5,15,4,2},
{8,6,9,3},
{13,7,12,10}
};

int a[405][405];

void work()
{
    n=read();
    mod=n*n+1;
    r=read();
    int p=n*n+1;
    if(!isp(p) || n==2){
        sols();
        return;
    }

    bool qwq=0;
    for(g=2;g<n*n;++g){
        int t=1;
        For(i,0,n*n) lg[i]=0;
        lg[1]=0;
        For(j,1,n*n-1){
            t=t*g%mod;
            lg[t]=j;
        }
    //    For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
        bool ok=1;
        For(i,2,n*n) if(!lg[i]) ok=0;
        if(ok){

pw[0]=1;
        For(i,1,n*n) pw[i]=pw[i-1]*g%mod;
        qwq=1;            break;
        }
    }
    cerr<<"qwq "<<qwq<<"\n";
    cerr<<"g: "<<g<<"\n";
  //  For(i,1,n*n) cout<<lg[i]<<" "; cout<<"\n";
   // For(i,0,n*n) cout<<pw[i]<<" "; cout<<"\n";
    assert(qwq);
    {
        int idx=-1;
        For(i,1,n)For(j,1,n){
            if(i%2==1 && j%2==1){
                a[i][j]=++idx;
                a[i][j+1]=n*n-idx-1;
            }
            if(i%2==0 && j%2==0){
                a[i][j]=++idx;
                a[i][j-1]=n*n-idx-1;
            }
        }
        r=lg[r];

        if(r%4!=2){
            puts("No");
            return;
        }

        int add=(r-(n*n-2)+n*n*2)%(n*n);
        add/=4;
        For(i,1,n)For(j,1,n){
            a[i][j]+=add;
            a[i][j]%=(n*n);
        }
        
        puts("Yes");
        For(i,1,n)For(j,1,n) cout<<pw[a[i][j]]<<" \n"[j==n];
    }
}


signed main()
{
    int T=read();
    while(T--)work();
	return 0;
}*/

Details

answer.code:374:2: error: stray ‘#’ in program
  374 | }#include<bits/stdc++.h>
      |  ^
answer.code:560:2: error: stray ‘#’ in program
  560 | }#include<bits/stdc++.h>
      |  ^
answer.code:746:2: error: stray ‘#’ in program
  746 | }#include<bits/stdc++.h>
      |  ^
answer.code:258:5: error: redefinition of ‘int n’
  258 | int n,r;
      |     ^
answer.code:71:5: note: ‘int n’ previously declared here
   71 | int n,r;
      |     ^
answer.code:258:7: error: redefinition of ‘int r’
  258 | int n,r;
      |       ^
answer.code:71:7: note: ‘int r’ previously declared here
   71 | int n,r;
      |       ^
answer.code:259:4: error: redefinition of ‘vi o’
  259 | vi o={2,8,9,15};
      |    ^
answer.code:72:4: note: ‘vi o’ previously declared here
   72 | vi o={2,8,9,15};
      |    ^
answer.code:260:5: error: redefinition of ‘int g’
  260 | int g=3;
      |     ^
answer.code:73:5: note: ‘int g’ previously defined here
   73 | int g=3;
      |     ^
answer.code:261:5: error: redefinition of ‘int lg [1000005]’
  261 | int lg[maxn],pw[maxn];
      |     ^~
answer.code:74:5: note: ‘int lg [1000005]’ previously declared here
   74 | int lg[maxn],pw[maxn];
      |     ^~
answer.code:261:14: error: redefinition of ‘int pw [1000005]’
  261 | int lg[maxn],pw[maxn];
      |              ^~
answer.code:74:14: note: ‘int pw [1000005]’ previously declared here
   74 | int lg[maxn],pw[maxn];
      |              ^~
answer.code:263:6: error: redefinition of ‘bool isp(int)’
  263 | bool isp(int p){
      |      ^~~
answer.code:76:6: note: ‘bool isp(int)’ previously defined here
   76 | bool isp(int p){
      |      ^~~
answer.code:267:6: error: redefinition of ‘void sols()’
  267 | void sols(){
      |      ^~~~
answer.code:80:6: note: ‘void sols()’ previously defined here
   80 | void sols(){
      |      ^~~~
answer.code:292:5: error: redefinition of ‘int mat [405][405]’
  292 | int mat[405][405]={
      |     ^~~
answer.code:105:5: note: ‘int mat [405][405]’ previously defined here
  105 | int mat[405][405]={
      |     ^~~
answer.code:300:5: error: redefinition of ‘int a [405][405]’
  300 | int a[405][405];
      |     ^
answer.code:113:5: note: ‘int a [405][405]’ previously declared here
  113 | int a[405][405];
      |     ^
answer.code:302:6: error: redefinition of ‘void work()’
  302 | void work()
      |      ^~~~
answer.code:115:6: note: ‘void work()’ previously defined here
  115 | void work()
      |      ^~~~
answer.code:369:8: error: redefinition of ‘int main()’
  369 | signed main()
      |        ^~~~
answer.code:182:8: note: ‘int main()’ previously defined here
  182 | signed main()
      |        ^~~~
answer.code:374:3: error: ‘include’ does not name a type
  374 | }#include<bits/stdc++.h>
      |   ^~~~~~~
answer.code:378:12: error: redefinition of ‘int read()’
  378 | inline int read()
      |            ^~~~
answer.code:5:12: note: ‘int read()’ previously defined here
    5 | inline int read()
      |            ^~~~
answer.code:385:5: error: redefinition of ‘int mod’
  385 | int mod;
      |     ^~~
answer.code:12:5: note: ‘int mod’ previously declared here
   12 | int mod;
      |     ^~~
answer.code:386:8: error: redefinition of ‘struct modint’
  386 | struct modint{
      |        ^~~~~~
answer.code:13:8: note: previous definition of ‘struct modint’
   13 | struct modint{
      |        ^~~~~~
answer.code:410:15: error: redefinition of ‘modint qpow(modint, int)’
  410 | inline modint qpow(modint x,int y){return x^y;}
      |               ^~~~
answer.code:37:15: note: ‘modint qpow(modint, int)’ previously defined here
   37 | inline modint qpow(modint x,int y){return x^y;}
      |               ^~~~
answer.code:412:16: error: redefinition of ‘std::vector<modint> fac’
  412 | vector<modint> fac,ifac,iv;
      |                ^~~
answer.code:39:16: note: ‘std::vector<modint> fac’ previously declared here
   39 | vector<modint> fac,ifac,iv;
      |                ^~~
answer.code:412:20: error: redefinition of ‘std::vector<modint> ifac’
  412 | vector<modint> fac,ifac,iv;
      |                    ^~~~
answer.code:39:20: note: ‘std::vector<modint> ifac’ previously declared here
   39 | vector<modint> fac,ifac,iv;
      |                    ^~~~
answer.code:412:25: error: redefinition of ‘std::vector<modint> iv’
  412 | vector<modint> fac,ifac,iv;
      |                         ^~
answer.code:39:25: note: ‘std::vector<modint> iv’ previously declared here
   39 | vector<modint> fac,ifac,iv;
      |                         ^~
answer.code:413:13: error: redefinition of ‘void initC(int)’
  413 | inline void initC(int n)
      |             ^~~~~
answer.code:40:13: note: ‘void initC(int)’ previously defined here
   40 | inline void initC(int n)
      |             ^~~~~
answer.code:424:15: error: redefinition of ‘modint C(int, int)’
  424 | inline modint C(int n,int m){
      |               ^
answer.code:51:15: note: ‘modint C(int, int)’ previously defined here
   51 | inline modint C(int n,int m){
   ...