跳到正文

题解

题解:B4398 [蓝桥杯青少年组国赛 2025] 第三题

cpc_pd_prc约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年8月29日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 1 分钟
题目
B4398

版本

  1. v1当前

分享一下赛时的思路

解题思路

可以发现,如果一个子序列满足条件,那么子序列中的所有数字除以他们的最大公因数后全都为完全平方数。

所以我们只需要计算出每一个数字至少需要除以几才能变成完全平方数,然后用一个类似于埃氏筛的东西来做就行了。

作者描述的可能不是很清楚,详细方法见程序。

程序

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int A=1e7+5;
int n,a[N],res[N],sum[A],ans;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		res[i]=1;
		int x=a[i];
		for(int j=2;j*j<=a[i];j++)
		{
			if(x%j==0)
			{
				int tot=0;
				while(x%j==0)
				{
					x/=j;
					tot++;
				}
				if(tot%2==1) res[i]*=j;
			}
		}
		res[i]*=x;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j*j*res[i]<=1e7;j++)
		sum[j*j*res[i]]++;
	}
	for(int i=1;i<=1e7;i++) ans=max(ans,sum[i]);
	cout<<ans;
	return 0;
}
原文