【最小公倍数】A. Di-visible Confusion

题目来源

Problem - 1603A - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/problemset/problem/1603/A

题干

官方思路 

 解释

对于任意一个ai,要想它被消除,就必须在【2,i+1】之间找到一个不可被整除的数。

然而

由欧几里得最小公倍数原理

当i>=22时,lcm(2,3,……,23)一定会大于10^{^{9}},这意味着ai一定能找到一个不可整除的数,所以一定可以被清除。 

这样我们只需要对i\epsilon [1,22]进行暴力遍历,检查是不是满足全部整除条件,如果有一个ai满足全部条件,则可以输出“NO”了

ac代码

void solve()
{
	int n;
	cin >> n;
	vector<int>a(n);
	for (auto& x : a)cin >> x;
	 bool ok = 1;
	for (int i = 0; i < n; i++)
	{
		bool found = 0;
		for (int j = i + 2; j >= 2; j--)
			if (a[i] % j)
			{
				found = 1;
				break;
			}
		ok &= found;
	}
	if (ok)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}

おすすめ

転載: blog.csdn.net/nathanqian123/article/details/121246686