D. Not Adding(思维)

Codeforces Round #766 (Div. 2)

题意

给出含有n个数的序列a。可以对序列a进行以下操作:每次选择任意两个数,若这两个数的gcd不在这个序列中,则将这两个数的gcd加入该序列中。
问:最多可以进行多少次该操作。
2 ≤ n ≤ 1 0 6 1 ≤ a [ i ] ≤ 1 0 6 2 \leq n \leq 10^6 \\ 1\leq a[i] \leq 10^6 2n1061a[i]106

思路

数论

  • 由于数列中的数范围较小,可以枚举所有的数。对于任意一个数i,将该数列中是i的倍数的数取出;然后将这些数都除去 i,若得到的这些数的gcd为1,则i 一定在这个数列中。
    例如:i = 3, a中是i的倍数的数为:6, 15。除去3后得:2, 5。gcd(2, 5) = 1。所以3在之后得操作中一定会被加入在这个序列中。
  • 由于是从小到大枚举的,所以这些中途加入的数,一定不会是后来枚举到的数的倍数。

代码

const int N = 2e5 + 7, M = 1e6 + 10;
int a[M];
int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}
int main()
{
	IOS;
	int n; cin >> n;
	for (int x, i = 0; i < n; i++)
	{
		cin >> x; a[x] = 1;
	}

	int ans = 0;
	for (int i = 1; i < M - 7; i++)
	{
		int d = 0;
		for (int j = i; j < M - 7; j += i)
			if (a[j]) d = gcd(d, j / i);
		if (d == 1 && !a[i]) ans++;
		//注意:需要判断一下这个数是否以及在这个序列中
	}
	cout << ans << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WARNING: The script easy_install-3.9 is installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pip3 and pip3.9 are installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The script easy_install-3.9 is installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pip3 and pip3.9 are installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The script easy_install-3.9 is installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pip3 and pip3.9 are installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The script easy_install-3.9 is installed in '/home/gucci/Python-3.9.0/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值