「杂题乱刷」CF1977C

题目链接

CF1977C (luogu)

CF1977C (codeforces)

解题思路

首先这题有一个简单的思路,就是当这个序列的 LCM 大于 \(10^9\) 时,显然取所有数字数字是合法的。

然后我们接下来考虑 LCM 小于等于 \(10^9\) 的情况。

发现这种情况 LCM 很小,且有一个简单的结论,就是你在序列中任选一个子序列,这个子序列的 LCM 一定是整个序列的 LCM 的因数,因此我们直接查询整个序列的 LCM 的所有因数可达成的最大答案即可,总时间复杂度 \(O(n \sqrt{V}\log_2(V))\),其中 \(V\) 为值域。

参考代码

/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!

记住,rating 是身外之物。

该冲正解时冲正解!

Problem:

算法:

思路:

*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
ll n,a[2010],ans;
ll LCM;
map<ll,ll>mp;
ll query(ll x)
{
    ll LCM=1,sum=0;
	forl(i,1,n)
        if(x%a[i]==0)
        	LCM=lcm(LCM,a[i]),sum++;
    if(mp[LCM]==0 && LCM==x)
    	return sum;
    return 0;
}
void solve()
{
	ans=0,LCM=1;
	cin>>n;
	forl(i,1,n)
		cin>>a[i];
    sort(a+1,a+n+1);
    forl(i,1,n)
    {
        LCM=lcm(a[i],LCM);
        if(LCM>a[n])
        {
        	cout<<n<<endl;
        	return ;
		}
    }
	forl(i,1,n)
		mp[a[i]]=1;
    forl(i,1,sqrt(LCM))
    	if(LCM%i==0)
    		ans=max({ans,query(i),query(LCM/i)});
    cout<<ans<<endl;
	mp.clear();
}
int main()
{
	IOS;
	t=1;
	cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}
posted @ 2024-05-28 21:15  wangmarui  阅读(5)  评论(0编辑  收藏  举报