CSP - CCF 2023 - 12 因子化简(暴力80分)

题目

  • 暴力求解
    思路是将一个数进行质数分解,遇到可以分解的质数进入循环,记录次方count,若大于等于k则保留,并且将该次方结果记录到vector数组之中
    最后将数组之中的值相乘输出即可

#include
#include
#include
using namespace std;

bool isprime(long long x)
{

	for (long long i = 2; i < x / 2; i++)
	{
		if (x % i == 0)
		{
			return false;
		}
	}
	return true;
}


void Print(long long x, int k)
{
	vector<long long>a;
	for (long long i = 2; i < x / 2; i++)
	{
	    int count = 0;
		if (isprime(i))
		{
			while (x % i == 0)
			{
				count++;
				x = x / i;
			}
		}
		if (count >= k)
		{
			a.push_back((long long)(pow((double)i,(double)count)));
		}
	}
	long long result = 1;
	for (int i = 0; i < a.size(); i++)
	{
		result = result * a[i];
	}
	cout << result << endl;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		long long x;
		int k;
		cin >> x>>k;
		Print(x, k);
	}
	return 0;
}

你可能感兴趣的:(csp,-,ccf,算法,c++)