CF 1977 C. Nikita and LCM (*1900) 数论

CF 1977 C. Nikita and LCM (*1900) 数论

题目链接

题意

给你一个长度为 \(n(n\le2000)\) 的数组 \(a\) , 如果 \(a\) 的子序列满足子序列的 \(LCM\) 不包含在 \(a\) 中,那么这个子序列是特殊子序列。求特殊子序列的最长长度?

思路

首先,如果所有数的 \(LCM\)\(x\) ,不包含在数组中,那么显然答案是 \(n\)

我们考虑枚举最后的\(LCM\), 显然一定是 \(x\) 的因子。然后暴力判断这个 \(LCM\) 是否符合条件即可。

代码

#include<bits/stdc++.h>

using namespace std;

#define ff first
#define ss second
#define pb push_back
#define all(u) u.begin(), u.end()
#define endl '\n'
#define debug(x) cout<<#x<<":"<<x<<endl;

typedef pair<int, int> PII;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 105;
const int mod = 1e9 + 7;
const int cases = 1;

void Showball(){
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    LL x=1;
    for(int i=0;i<n;i++){
      x=lcm(x,1LL*a[i]);
      if(x>1E9) break;
    } 
    if(find(all(a),x)==a.end()) return cout<<n<<endl,void();

    int ans=0;
    auto calc=[&](int d){
      if(find(all(a),d)!=a.end()) return;
      int x=1,cnt=0;
      for(int i=0;i<n;i++){
         if(d%a[i]==0){
            x=lcm(x,a[i]);
            cnt++;
         }
      }
      if(x==d) ans=max(ans,cnt);
    };

    for(int i=1;i<=x/i;i++){
       if(x%i==0){
         calc(i);
         calc(x/i);
       }
    }
    cout<<ans<<endl;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T=1;
    if(cases) cin>>T;
    while(T--)
    Showball();
    return 0;
}
posted @ 2024-06-22 01:52  Showball  阅读(2)  评论(0编辑  收藏  举报