1034A - Enlarge GCD - CodeForces Solution


number theory *1800

Please click on ads to support us..

C++ Code:

#include <cstdio>
#include <iostream>
const int N	= 1.5e7+10;
using namespace std;

int GCD(int a,int b)
{
	if(a%b==0)return b;
	return GCD(b, a%b);
}

int prime[N],tot = 0;
bool isnp[N];
int minp[N];
void Init()
{
	for(int i = 2;i < N ;i ++)
	{
		if(!isnp[i])
		{
			prime[tot++] = i;
			minp[i] = i;
		}
		for(int j = 0;j < tot;j++)
		{
			int p = prime[j];
			if(p * i >= N)	break;
			isnp[p * i] = 1;
			minp[p * i] = p;
			if(i % p == 0)	break;
		}
	}
}


int cnt[N];

void Spilt(int x)
{
	if(x == 1)
		return;
	int cur = x;
	int last = 0;
	while (cur)
	{
		if(minp[cur] != last)
			 ++cnt[minp[cur]],last = minp[cur];
//		printf("%d\n",minp[cur]);
		if(minp[cur] == cur)
			break;
		cur /= minp[cur];
	}
}

int arr[N];
int n;

void Sol()
{
	for (int i=2; i<=n; i++)
	{
		if(arr[i]!=arr[i-1])
			break;
		else if(i==n)
		{
			printf("-1\n");
			return;
		}
	}
	
	int gcd = arr[1];
	for (int i=1; i<=n; i++)
	{
		gcd = GCD(gcd, arr[i]);
	}
	for (int i=1; i<=n; i++)
	{
		arr[i] /= gcd;
	}
	
	for (int i=1; i<=n; i++)
	{
		if(arr[i] != 1)
			Spilt(arr[i]);
	}
	int maxx = 0;
	for (int i=1; i<N; i++)
	{
		if(cnt[i]<n)
			maxx = max(maxx, cnt[i]);
	}
	int ans = 0;
	if(maxx == 0)
	{
		int minn = 1e9, tot=0;
		for (int i=1; i<=n; i++)
		{
			if(arr[i] < minn)
				minn = arr[i], tot=1;
			else if(arr[i]==minn)
				tot++;
		}
		ans = tot;
	}
	else ans = n-maxx;
	
	printf("%d\n",ans);
}


int main()
{
	Init();
	scanf("%d",&n);
	for (int i=1; i<=n; i++) 
	{
		scanf("%d",&arr[i]);
	}
	Sol();
	
	return 0;
}

 	 		     		   	 	 	  		  			 	


Comments

Submit
0 Comments
More Questions

938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths