CF 1165D Almost All Divisors 水

http://codeforces.com/contest/1165/problem/D

main idea:

Input frequency for each input n, followed by the number n, find the least common multiple of them, then the number n is determined before it is not all multipliers

Ideas:

1. The least common multiple of the minimum multiplier is maximum (sorting, ans = a [0] * a [n-1])

2. remaining a [2] to a [n-2], and with their modulus is determined ans a [] ans multiplier whether, if there is not the multiplier, the output of -1

3. There are several requirements ans multiplier if the multiplier is not equal to the number n, the output of -1

 

Avoid the pit: Note the use of the int and long long (painful lesson)

With source code

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	long long a[310];
	int times,n;
	cin>>times;
	while(times--)
	{
		long long ans;
		int flag=0;
		cin>>n;
		//in put 
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
		}
		//get ans
		sort(a,a+n);
		ans=a[0]*a[n-1];
		//check 1
		for(int i=1;i<n-1;i++)
		{
			if(ans%a[i]!=0)
			{
				flag=1;
				break;
			}
		}
		//check2
		int num=0; 
		long long lim=sqrt(ans);//!!!!  long long !!!!
		if(flag==0)
		{
			for(int i=2;i<=lim;i++)
			{
				if(ans%i==0)
				{
					num=num+2;
				}
			}
			if(lim*lim==ans)    
			{
				num--;
			}
			if(num!=n)
			{
				flag=1;
			}
		}
		if(flag==1)cout<<"-1"<<endl;
		else cout<<ans<<endl;
	}
	
	
}

 

Guess you like

Origin blog.csdn.net/recluse_e/article/details/90812578