cf 628 E. Ehab's REAL Number Theory Problem

The meaning of problems

Given sequence.
The number of not more than 7 Divisor
seeking a minimal sequence such that the product is a perfect square

answer

No more than 7 divisor equivalent to a maximum of two prime factors.
And we only care about the odd power of prime factors.
So we remove all the items square, leaving a strange power.
As long as this number is odd power of two, that is a squared term.

We consider, for a number of built an edge, the edge of even two prime factors.
If is a prime number, 1, and even themselves.

Is not a prime number is not required, because the answer does not contribute to
such 10 = 2 5 10=2*5 , if the connection 1 and 10, for example, that in order to have two 10 2 10*2 , then there must also be a qualitative factor 2 2 and 5 5 .
So directly 2 2 and 5 5 can be.

Find the smallest ring, so for all prime factors of the above have two numbers have, then that is even the power.

Find the smallest rings b f s bfs , to enumerate the points b f s bfs , because you do not know at what point in the smallest ring.
However, to ensure that there is always a point of less than the smallest ring 1 e 3 1e3 , otherwise there is the number of > 1 e 6 >1e6 a.
enumerate 1 e 3 1e3 for b f s bfs

b f s bfs determination part is to record from this edge to the next point if not visited, and the next point has been visited, so that ring.
Carried out on the side label v i s vis record can, so that it is equal to the direct current source, so to avoid the complexity emptied.

#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

const int maxn = 1e6+50;

typedef long long ll;

int cnt=0;
vector<pair<int,int> >G[maxn];
int vis[maxn],len[maxn],ans=0x3f3f3f3f;

void bfs(int i){
    queue<int>q;
    q.push(i);
    memset(len,0x3f,sizeof(len));
    len[i]=0;
    while(!q.empty()){
        int u=q.front();q.pop();
//        cout<<u<<":"<<" ";
        for(auto it:G[u]){
            int v=it.first,id=it.second;
            if(vis[id]==i)continue;
//            cout<<v<<" ";
            vis[id]=i;
            if(len[v]!=0x3f3f3f3f)ans=min(ans,len[u]+len[v]+1);
            else len[v]=len[u]+1,q.push(v);
        }
//        puts("");
    }
}

int main(){
    int n,x;cin>>n;
    FOR(i,1,n){
        scanf("%d",&x);
        for(int j=2;j<=sqrt(x);j++){
            while(x%(j*j)==0)x/=j*j;
        }
        if(x==1){
            puts("1");
            return 0;
        }
        vector<int>tmp;
        for(int j=2;j<=sqrt(x);j++){
            if(x%j==0){
                tmp.push_back(j);
                while(x%j==0)x/=j;
            }
        }
        if(x!=1)tmp.push_back(x);
        if(tmp.size()==1){
            G[1].push_back(make_pair(x,++cnt));
            G[x].push_back(make_pair(1,cnt));
        }
        else{
            G[tmp[0]].push_back(make_pair(tmp[1],++cnt));
            G[tmp[1]].push_back(make_pair(tmp[0],cnt));
        }
    }
    FOR(i,1,sqrt(maxn-1))bfs(i);
    if(ans==0x3f3f3f3f)puts("-1");
    else cout<<ans<<endl;
}

In fact, this question I began to see that the problem will not be the solution, but many people use the Internet to build divisor side, I think the constant bit large, with a prime factor, then less able 7 7 fold edges. Finally, run 1300 m s 1300ms

Here Insert Picture Description

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104964928
Recommended