WA+TLE求助
查看原帖
WA+TLE求助
364848
Bodhi楼主2023/10/16 20:32

RT,不管是复杂度正确的还是暴力做法都是WA,各位能帮忙看一下吗QwQ

复杂度正确记录:

#include <bits/stdc++.h>
using namespace std;

const int R = 1e6 + 10;
int a[R], cnt[15];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	vector<int> pr;
	pr.push_back(2), pr.push_back(3), pr.push_back(7), pr.push_back(11);
	int T, n, i;
	long long k, sum;
	string ks;
	bool flag;
	cin >> T;
	while (T--)
	{
		cin >> n >> ks;
		sum = 0;
		flag = false;
		memset(cnt, 0, sizeof(cnt));
		for (i = 1; i <= n; ++i)
		{
			cin >> a[i];
			sum += (long long)a[i];
			for (int x : pr)
			{
				if (a[i] % x == 0)
				{
					++cnt[x];
				}
			}
		}
		k = (ks.size() <= 17 ? stoll(ks) : 1e17);
		if (sum >= k)
		{
			flag = true;
		}
		else
		{
			for (i = 1; i <= n; ++i)
			{
				if ((a[i] % 154 == 0) ||
					(a[i] % 147 == 0) ||
					(a[i] % 77 == 0 && cnt[2]) ||
					(a[i] % 22 == 0 && cnt[7]) ||
					(a[i] % 14 == 0 && cnt[11]) ||
					(a[i] % 49 == 0 && cnt[3]) ||
					(a[i] % 21 == 0 && cnt[7] >= 2))
				{
					flag = true;
					break;
				}
			}
		}
		cout << (flag ? "Yes\n" : "No\n");
	}
	return 0;
}

暴力记录:

#include <bits/stdc++.h>
using namespace std;

const int R = 1e6 + 10;
long long a[R];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T, n, i, j, tot;
	long long k, sum, x;
	string ks;
	bool flag;
	cin >> T;
	while (T--)
	{
		cin >> n >> ks;
		tot = sum = 0;
		flag = false;
		for (i = 1; i <= n; ++i)
		{
			cin >> x;
			sum += x;
			if (x % 2 == 0 || x % 3 == 0 || x % 7 == 0 || x % 11 == 0)
			{
				a[++tot] = x;
			}
		}
		k = (ks.size() <= 17 ? stoll(ks) : 1e17);
		if (sum >= k)
		{
			flag = true;
		}
		for (i = 1; i <= tot; ++i)
		{
			for (j = i + 1; j <= tot; ++j)
			{
				if (a[i] * a[j] % 147 == 0 || a[i] * a[j] % 154 == 0)
				{
					flag = true;
				}
			}
		}
		cout << (flag ? "Yes\n" : "No\n");
	}
	return 0;
}
2023/10/16 20:32
加载中...