zoukankan      html  css  js  c++  java
  • CF1325F Ehab's REAL Number Theory Problem

    Ehab's REAL Number Theory Problem

    You are given an array (a) of length (n) that has a special condition: every element in this array has at most (7) divisors. Find the length of the shortest non-empty subsequence of this array product of whose elements is a perfect square.

    (1 le n le 10^5,1 le a_i le 10^6)

    题解

    https://codeforces.ml/blog/entry/74235

    Notice that for each element in the array, if some perfect square divides it, you can divide it by that perfect square, and the problem won't change. Let's define normalizing a number as dividing it by perfect squares until it doesn't contain any. Notice than any number that has (3) different prime divisors has at least (8) divisors, so after normalizing any element in the array, it will be (1), (p), or (p*q) for some primes (p) and (q). Let's create a graph where the vertices are the prime numbers (and (1),) and the edges are the elements of the array. For each element, we'll connect (p) and (q) or (p) and (1) if it's a prime after normalizing (只有 (1) 说明答案就是它一个数). What's the significance of this graph? Well, if you take any walk from node (p) to node (q), multiply the elements on the edges you took, and normalize, the product you get will be (p*q)! That's because every node in the path will be visited an even number of times, except p and q. So the shortest subsequence whose product is a perfect square is just the shortest cycle in this graph!

    这题的主要难点在于如何找环。

    The shortest cycle in an arbitrary graph takes (O(n^2)) to compute: you take every node as a source and calculate the bfs tree, then you look at the edges the go back to the root to close the cycle. That only finds the shortest cycle if the bfs source is contained in one. The graph in this problem has a special condition: you can't connect 2 nodes with indices greater than (sqrt{max A_i}). That's because their product would be greater than (max A_i). So that means ANY walk in this graph has a node with index (lesqrt{max A_i}). You can only try these nodes as sources for your bfs.

    时间复杂度 (O(Aln A+sqrt{A}n))

    CO int N=1e6;
    vector<int> pr,d[N+10];
    int lp[N+10];
    array<int,2> e[N];
    vector<int> to[N+10];
    int dis[N+10];
    
    int main(){
    	pr.push_back(1);
    	for(int i=2;i<=N;++i){ // prime factors
    		if(!lp[i]){
    			pr.push_back(i);
    			for(int j=i;j<=N;j+=i) lp[j]=i; // last prime
    		}
    		d[i]=d[i/lp[i]];
    		if(d[i].size() and d[i].back()==lp[i])
    			d[i].pop_back(); // p^2 => 1
    		else d[i].push_back(lp[i]);
    	}
    	int n=read<int>();
    	for(int i=1;i<=n;++i){
    		int a=read<int>();
    		if(d[a].empty()){
    			puts("1");
    			return 0;
    		}
    		if(d[a].size()==1) d[a].push_back(1);
    		e[i]={d[a][0],d[a][1]};
    		to[d[a][0]].push_back(i),to[d[a][1]].push_back(i);
    	}
    	int ans=1e9;
    	for(int i:pr){
    		if(i*i>N) break;
    		for(int j:pr) dis[j]=0;
    		deque<pair<int,int> > Q;
    		for(int j:to[i]){
    			Q.push_back({j,e[j][0]==i});
    			dis[e[j][0]^e[j][1]^i]=1;
    		}
    		while(Q.size()){
    			pair<int,int> p=Q.front();Q.pop_front();
    			int x=e[p.first][p.second];
    			for(int u:to[x])if(u!=p.first){
    				pair<int,int> q={u,e[u][0]==x};
    				int y=e[q.first][q.second];
    				if(!dis[y] and y!=i){
    					dis[y]=dis[x]+1;
    					Q.push_back(q);
    				}
    				else ans=min(ans,dis[x]+dis[y]+1);
    			}
    		}
    	}
    	printf("%d
    ",ans==1e9?-1:ans);
    	return 0;
    }
    
  • 相关阅读:
    修改版的jsonView,加入了PHP的反序列化
    CSBlogV2公测发布,欢迎大家下载试用体验.
    C#里使用Oracle提供的Oracle.DataAccess 返回包里的记录集游标,含Oralce里的分页包代码
    用一个示例方法来初始理解ManualResetEvent的用法。
    Head First 设计模式阅读所得:策略模式(Strategy Pattern) 接口的用处(之一)
    延时至调用时获取被反序列化数据的类型的实现
    权限设计中的"依赖颠倒"
    CSCMSV1终于上线内测了[广告贴]
    Windows Service 使用参数安装DEMO,可使用控制台启动方式进行调试,服务安装完后立即启动
    使用assembly.GetExportedTypes();方法时引发动:态程序集中不支持已调用的成员的异常
  • 原文地址:https://www.cnblogs.com/autoint/p/12586353.html
Copyright © 2011-2022 走看看