1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include<cstdio> #include<algorithm> using namespace std; inline char nc(){ static char buf[100000],*l=buf,*r=buf; return l==r&&(r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++; } inline int red(){ int res=0,f=1;char ch=nc(); while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();} while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc(); return res*f; } inline void put(int x){ static int stk[11];stk[0]=0; do stk[++stk[0]]=x%10,x/=10; while (x); while (stk[0]) putchar(stk[stk[0]--]+48); }
const int maxn=1000005,maxa=10000005; int n,a[maxn],L[maxn],R[maxn]; int gcd(int x,int y) {return y==0?x:gcd(y,x%y);} int p[maxa],pre[maxa],lst[maxa]; bool vis[maxa]; void makep(int n){ for (int i=2;i<=n;i++){ if (!vis[i]) p[++p[0]]=i,pre[i]=i; for (int j=1;j<=p[0]&&i*p[j]<=n;j++){ vis[i*p[j]]=1; pre[i*p[j]]=p[j]; if (i%p[j]==0) break; } } } int fa[maxn]; bool solve(int l,int r,int f){ if (l>r) return 1; for (int i=l,j=r;i<=j;i++,j--){ if (L[i]<l&&r<R[i]){ fa[i]=f;return solve(l,i-1,i)&&solve(i+1,r,i); } if (L[j]<l&&r<R[j]){ fa[j]=f;return solve(l,j-1,j)&&solve(j+1,r,j); } } return 0; } int main(){ n=red(); for (int i=1;i<=n;i++) a[i]=red(); makep(10000000); for (int i=1;i<=n;i++){ int x=a[i]; while (x!=1){ int p=pre[x]; L[i]=max(L[i],lst[p]); lst[p]=i; while (x%p==0) x/=p; } } for (int i=1;i<=p[0];i++) lst[p[i]]=n+1; for (int i=n;i;i--){ int x=a[i];R[i]=n+1; while (x!=1){ int p=pre[x]; R[i]=min(R[i],lst[p]); lst[p]=i; while (x%p==0) x/=p; } } if (solve(1,n,0)) for (int i=1;i<=n;i++) put(fa[i]),putchar(' '); else puts("impossible"); return 0; }
|