Not Adding - 题解【数学,枚举】

题面

原题链接(CF1627D)

You have an array a1,a2,,an consisting of n distinct integers. You are allowed to perform the following operation on it:

Choose two elements from the array ai and aj (ij) such that gcd(ai,aj) is not present in the array, and add gcd(ai,aj) to the end of the array. Here gcd(x,y) denotes greatest common divisor (GCD) of integers x and y.
Note that the array changes after each operation, and the subsequent operations are performed on the new array.

What is the maximum number of times you can perform the operation on the array?

Input
The first line consists of a single integer n (2n106).

The second line consists of n integers a1,a2,,an(1ai106). All ai are distinct.

Output
Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.

Examples
input

5
4 20 1 25 30

output

3

input

3
6 10 15

output

4

Note
In the first example, one of the ways to perform maximum number of operations on the array is:

Pick i=1,j=5 and add gcd(a1,a5)=gcd(4,30)=2 to the array.
Pick i=2,j=4 and add gcd(a2,a4)=gcd(20,25)=5 to the array.
Pick i=2,j=5 and add gcd(a2,a5)=gcd(20,30)=10 to the array.
It can be proved that there is no way to perform more than 3 operations on the original array.

In the second example one can add 3, then 1, then 5, and 2.

大意

给出一个整数的不可重集合,可以对其重复执行以下操作:对于集合内的任意两个数,计算它们的最大公约数(GCD),如果这个GCD不存在于原集合中,就把它加入这个集合。新加入的数也可以在之后的操作中参与计算。重复此操作,求最多能进行的操作次数。

题解

题面的数据达到了1e6,很显然不能枚举每一对数来检查。但是,集合内的元素大小也在1e6范围内,因此可以开一个大小1e6的数组记录这个数有没有出现过。同时GCD还有一个很有用的性质:两个数的GCD不大于两个数的最小值,也就是gcd(ai,aj)min(ai,aj)。所以,如果一个数能够被加入这个集合,它一定比原集合的最大元素还要小。所以我们考虑对答案进行枚举,从1到这个最大值的所有整数全部判断一遍。

怎么判断这个数能否被加入集合呢?首先,如果已经存在于集合中的数肯定不能被加入,因此可以跳过。然后,对于不在集合中的数i,要想让它被加入集合中,必须存在两个数使其最大公约数为i。也就是说,集合中存在两个数ap=pi,aq=qi,其中p,q互质。由此,我们可以考虑对每一个没有出现的数i,检查所有的2i,3i,,kimax(a)是否出现在集合中。但是,判断互质是一件非常麻烦的事情。所以联想到GCD的另一个重要性质:三个数的GCD等于其中两个数的GCD和另外一个数的GCD,也就是说gcd(ai,aj,ak)=gcd(gcd(ai,aj),ak),这个性质对n个数也成立。假设对所有的ki,有三个数k1i,k2i,k3i出现在了集合中,其中kn两两不互质,但是k1,k2,k3互质,那么我们可以把k4i=gcd(k1i,k2i)加入到集合中(如果它已经在集合中那么就不用理睬了),然后计算gcd(k4i,k3i)=i,将其加入集合。对于n个元素也同理。据此,我们可以得出,对于任意一个未出现在集合内的数i,它能加入集合的充要条件是集合内所有i的倍数的数的GCD等于i

枚举每一个可能的i,对每一个i作判断计算次数为ni,对此求和(调和级数)得到线性乘以对数的复杂度,求GCD的复杂度大约为对数级别,因此总体复杂度为O(nlog2n)

代码如下:

#include <bits/stdc++.h>
#define GRP   \
    int T;    \
    cin >> T; \
    rep(C, 1, T)
#define FAST                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define elif else if
#define mem(arr, val) memset(arr, val, sizeof(arr))
typedef long long ll;
typedef unsigned long long ull;

int n;
int a[1000010];
bool vis[1000010];
int maxx, cnt, num;
int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}

int main()
{
    FAST;
    cin >> n;
    mem(vis, 0);
    cnt = 0;
    cin >> a[1];
    maxx = a[1];
    vis[a[1]] = true;
    rep(i, 2, n)
    {
        cin >> a[i];
        maxx = max(maxx, a[i]);
        vis[a[i]] = true;
    }
    rep(i, 1, maxx)	//进行枚举
    {
        if (vis[i])
        {
            continue;
        }
        num = 0;	//令一个数和0的GCD等于该数本身,可以简化代码
        for (int j = i * 2; j <= maxx; j += i)	//枚举所有的倍数
        {
            if (!vis[j])
            {
                continue;
            }
            num = gcd(num, j);
        }
        if (num == i)
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}
posted @   AlexHoring  阅读(113)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示