[CSP-S Simulation Test]: number of squares (mathematical hash +)

Topic Portal (internal title 137)


Input Format

  The first line, a positive integer $ n $.
  The second line of $ n $ positive integers $ a_1 \ sim a_n $.


Output Format

  Outputs an integer number of tuples satisfying the condition.


Sample

Sample input:

5
1 2 3 4 12

Sample output:

2


Data range and tips

  Data for $ 20 \% $ satisfy $ n \ leqslant 3,000 $.
  Data for $ 50 \% $ satisfy $ n \ leqslant 50,000 $.
  For another $ 20 \% $ of data to meet the $ a_i \ leqslant 1,000 $.
  For $ 100 \% $ data satisfies $ 1 \ leqslant n \ leqslant 300,000,1 \ leqslant a_i \ leqslant 10 ^ 9 $.


answer

Multiplying the square of the number two which is the same number that is the number of odd prime factor decomposition.

The hash can be used to maintain, with $ unordered \ text {_} map $ records just fine.

Prime number more, but we can only use the first few prime numbers just fine.

Time complexity: $ \ (the number of prime numbers is utilized $ k $) $ Theta (kn).

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int>mp;
int n;
int pri[170],cnt;
bool vis[1000];
long long ans;
void pre_work()
{
	for(int i=2;i<1000;i++)
	{
		if(vis[i])continue;pri[++cnt]=i;
		for(int j=i;j<1000;j+=i)vis[j]=1;
	}
}
int main()
{
	pre_work();
	scanf("%d",&n);
	while(n--)
	{
		int x,res=1;
		scanf("%d",&x);
		for(int i=1;i<=cnt;i++)
		{
			while(!(x%(pri[i]*pri[i])))x/=pri[i]*pri[i];
			if(!(x%pri[i])){x/=pri[i];res*=pri[i];}
		}
		if((int)(sqrt(x))*(int)(sqrt(x))!=x)res*=x;
		ans+=mp[res];mp[res]++;
	}
	printf("%lld",ans);
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11830142.html