Blue Bridge Cup Daily Question 2023.9.21

3491. Perfect Square Numbers - AcWing Question Bank

Question description

analyze 

A characteristic of perfect square numbers:

All prime factors have an even number. The prime factor of eg1.9 is 3, the number of 3 is 2, and we get 9 (3*3=9)

                                               The prime factor of eg2.81 is 3, the number of 3 is 4, and we get 81 (3*3*3*3=81)

We multiply the answer by a prime factor that is not an even number, that is, add one to the number of prime factors to make it an even number. Finally, all of these prime factors become even numbers, so that it is a perfect square number.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
unordered_map<ll, ll> primes;
ll n;
int main()
{
	cin >> n;
	for(int i = 2; i <= n / i; i ++)
	{
		while(n % i == 0)
		{
			n /= i;
			primes[i] ++;
		}
	}
	if(n > 1)primes[n] ++;
	ll ans = 1;
	for(auto i : primes)
	{
		if(i.second % 2 != 0)ans *= i.first;
	}
	cout << ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_75087931/article/details/133135737