QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111794#6399. Classic: Classical Problemdo_while_trueWA 3ms7516kbC++144.3kb2023-06-08 20:22:052023-06-08 20:22:09

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-08 20:22:09]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:7516kb
  • [2023-06-08 20:22:05]
  • 提交

answer

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
int qpow(int x,int y,int mod){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return s;
}
namespace MyPoly{
	const int N=4000010;
	const int mod=998244353;
	inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
	inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
	inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
	inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
	int qpow(int x,int y){
		int s=1;
		while(y){
			if(y&1)s=1ll*s*x%mod;
			x=1ll*x*x%mod;
			y>>=1;
		}
		return s;
	}
	int *getw(int n, int type) {
		static int w[N/2];
		w[0]=1;w[1]=qpow(type==1?3:332748118,(mod-1)/n);
		for(int i=2;i<n/2;i++)w[i]=1ll*w[i-1]*w[1]%mod;
		return w;
	}
	struct Poly{
		vi a;
		Poly(){vi().swap(a);}
		int size(){return a.size();}
		void resize(int x){a.resize(x);}
		int &operator[](int i){return a[i];}
	};
	void DFT(Poly &a){//转置 
		int n=a.size();
		for(int i=n/2;i;i>>=1){
			int *w=getw(i<<1,1);
			for(int j=0;j<n;j+=i<<1){
				for(int k=0;k<i;++k){
					int u=a[j+k],v=a[j+i+k];
					a[j+k]=add(u,v);
					a[j+i+k]=del(1ll*u*w[k]%mod,1ll*v*w[k]%mod);
				}
			}
		}
	}
	void IDFT(Poly &a){
		int n=a.size();
		for(int i=1;i<n;i<<=1){
			int *w=getw(i<<1,-1);
			for(int j=0;j<n;j+=i<<1){
				for(int k=0;k<i;++k){
					int v=1ll*a[j+i+k]*w[k]%mod;
					a[j+i+k]=del(a[j+k],v);
					cadd(a[j+k],v);
				}
			}
		}
		int inv=qpow(n,mod-2);
		for(int i=0;i<n;i++)a[i]=1ll*a[i]*inv%mod;
	}
	Poly operator*(Poly f,Poly g){
		int n=f.size(),m=g.size();
		int len=1,ct=0;
		while(len<=n+m)len<<=1,++ct;
		f.resize(len);g.resize(len);
		DFT(f);
		DFT(g);
		for(int i=0;i<len;i++)f[i]=1ll*f[i]*g[i]%mod;
		IDFT(f);
		f.resize(n+m-1);
		return f;
	}
}
using namespace MyPoly; 
int n,p;
int to[N],ys[N];
void solve(){
	read(n,p);vi pr;
	{
		int m=p-1;
		for(int i=2;i*i<=m;i++)
			if(m%i==0){
				while(m%i==0)m/=i;
				pr.pb(i);
			}
		if(m>1)pr.pb(m);
	}
	int g=0;
	for(int i=1;i<p;i++){
		int fl=0;
		for(auto j:pr)
			if(qpow(i,(p-1)/j,p)==1){
				fl=1;
				break;
			}
		if(!fl){
			g=i;
			break;
		}
	}
	int s=1;
	for(int i=0;i<p-1;i++)to[s]=i,ys[i]=s,s=1ll*s*g%p;
	int q=p-1;
	int ok=0;
	vi a;
	for(int i=1;i<=n;i++){
		int x;read(x);
		if(x==0)ok=1;
		else{
			x=(-to[x]+q)%q;
			a.pb(x);
		}
	}
	if(!ok){
		puts("1 1");
		puts("0");
		return ;
	}
	vi ans;
	auto check=[&](int mid){
		Poly f,g;f.resize(p);g.resize(p);
		for(int i=1;i<mid;i++)f[to[i]]=1;
		for(auto i:a)g[i]=1;
		f=f*g;
		for(int i=p-1;i<f.size();i++)f[i%(p-1)]+=f[i];
		vi().swap(ans);
		for(int i=0;i<p-1;i++)
			if(f[i]==mid-1)
				ans.pb(ys[i]);
		return !ans.empty();
	};
	int l=1,r=n,mid,as=0;
	while(l<=r){
		mid=(l+r)>>1;
		if(check(mid))as=mid,l=mid+1;
		else r=mid-1;
	}
	check(as);
	if(as==1)ans.insert(ans.begin(),0);
	cout << ans.size() << ' ' << as << '\n';
	for(auto i:ans)cout << i << ' ';
	puts("");
}
signed main(){
	#ifdef do_while_true
		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	int T;read(T);while(T--)solve();
    #ifdef do_while_true
		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 7516kb

input:

3
2 3
0 2
3 5
2 3 4
3 5
0 2 3

output:

1 2
2 
1 1
0
2 2
2 3 

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 7460kb

input:

3
1 2
0
1 2
1
2 2
1 0

output:

2 1
0 1 
1 1
0
1 2
1 

result:

ok 6 lines

Test #3:

score: 0
Accepted
time: 1ms
memory: 7416kb

input:

7
1 3
0
1 3
1
2 3
1 0
1 3
2
2 3
2 0
2 3
1 2
3 3
0 1 2

output:

3 1
0 1 2 
1 1
0
1 2
1 
1 1
0
1 2
2 
1 1
0
2 3
1 2 

result:

ok 14 lines

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 7484kb

input:

31
1 5
0
1 5
1
2 5
1 0
1 5
2
2 5
0 2
2 5
2 1
3 5
1 0 2
1 5
3
2 5
0 3
2 5
1 3
3 5
0 1 3
2 5
3 2
3 5
0 2 3
3 5
2 1 3
4 5
2 0 1 3
1 5
4
2 5
4 0
2 5
1 4
3 5
1 4 0
2 5
2 4
3 5
2 4 0
3 5
4 2 1
4 5
1 0 4 2
2 5
4 3
3 5
0 4 3
3 5
3 1 4
4 5
1 4 3 0
3 5
4 3 2
4 5
2 4 0 3
4 5
2 1 4 3
5 5
1 3 0 2 4

output:

5 1
0 1 2 4 3 
1 1
0
1 2
1 
1 1
0
1 2
3 
1 1
0
1 3
1 
1 1
0
1 2
2 
1 1
0
1 3
2 
1 1
0
2 2
2 3 
1 1
0
1 4
1 
1 1
0
1 2
4 
1 1
0
2 2
1 4 
1 1
0
1 3
3 
1 1
0
1 4
3 
1 1
0
1 3
4 
1 1
0
1 4
2 
1 1
0
1 4
4 
1 1
0
4 5
1 2 4 3 

result:

wrong answer 2nd lines differ - expected: '0 1 2 3 4', found: '0 1 2 4 3 '