QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672267#9485. (mod N² + 1)masterhuangWA 0ms3624kbC++231.5kb2024-10-24 16:14:072024-10-24 16:14:07

Judging History

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

  • [2024-10-24 16:14:07]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3624kb
  • [2024-10-24 16:14:07]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=55;
int T,n,r,p,lg[N*N],pw[N*N],a[N][N];
namespace GG
{
	int n;vector<int>g;
	inline int ksm(int x,int p){int s=1;for(;p;(p&1)&&(s=1ll*s*x%n),x=1ll*x*x%n,p>>=1);return s;}
	inline bool isy(int x){if(__gcd(x,n)>1) return 0;for(int i:g) if(ksm(x,i)==1) return 0;return 1;}
	inline int gg(int x)
	{
		g.clear();int t=(n=x)-1,y=t;
		for(int i=2;i*i<=y;i++) if(y%i==0){while(y%i==0) y/=i;g.push_back(t/i);}(y^1)&&(g.push_back(t/y),1);
		for(int i=1;i<x;i++) if(isy(i)) return i;
	}
}
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>T;
	while(T--)
	{
		cin>>n>>r;
		if(n==1){cout<<"Yes\n1\n";continue;}
		if(n==3&&!r){cout<<"Yes\n1 2 3\n4 5 6\n7 8 9\n";continue;}
		p=n*n+1;bool o=1;
		for(int i=2;i*i<=p;i++) if(!(p%i)){o=0;break;}
		if(!o||!r){cout<<"No\n";continue;}int g=GG::gg(p);
		for(int i=0,s=1;i<p-1;i++,s=1ll*s*g%p) lg[pw[i]=s]=i;
		r=lg[r];int cnt=0;
		if((r&3)!=2){cout<<"No\n";continue;}
		for(int i=1;i<=n;i+=2) for(int j=1;j<=n;j+=2) a[i][j]=++cnt;
		for(int i=n-1;i>0;i-=2) for(int j=2;j<=n;j+=2) a[i][j]=++cnt;
		for(int i=2;i<=n;i+=2) for(int j=n-1;j>0;j-=2) a[i][j]=++cnt;
		for(int i=n;i>0;i-=2) for(int j=n;j>0;j-=2) a[i][j]=++cnt;
		int k=(lg[r]-2)/4;cout<<"Yes\n";
		for(int i=1;i<=n;i++,cout<<"\n") for(int j=1;j<=n;j++)
			cout<<pw[(a[i][j]+k)%(p-1)]<<" ";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3624kb

input:

3
2 4
3 3
4 2

output:

Yes
2 4 
3 1 
No
Yes
9 16 10 14 
7 3 8 1 
13 15 5 11 
12 6 4 2 

result:

wrong answer There exists some invalid submatrix.