6/7~6/9两日总结

第二周任务 [Cloned] - Virtual Judge (vjudge.net)

A - AABCC

思路:欧拉筛+剪枝

通过欧拉筛将2~n的所有素数找出来,再暴力求解

#include
#include
#include
using namespace std;
int vis[1000005];
int p[1000005];
int cnt = 0;
typedef long long ll;
void init() {
	for (int i = 2; i < sqrt(1e12); i++)
	{
		if (!vis[i])
			p[cnt++] = i;
		for (int j = 0; p[j] * i < sqrt(1e12); j++)
		{
			vis[p[j] * i] = true;
			if (i % p[j] == 0)
				break;
		}
	}
}
int main()
{
	ll n;
	cin >>  n;
	init();
	ll ans = 0;
	for(int i =0;i n)
			break;

		for (int j = i + 1; j < cnt; j++)
		{
			ll b = p[j];
			if (a * a * b * b * b > n)
				break;

			for (int m = j + 1; m < cnt; m++)
			{
				ll c = p[m];
				if (a * a * b * c * c > n)
					break;
				else ans++;
			}

		}

	}

	cout << ans << endl;
	return 0;




	
}

第二周任务 [Cloned] - Virtual Judge (vjudge.net)

K - Gap Existence

排序后,用滑动窗口

如果sum值大于x,L++

如果sum值小于x,R++


#include
#include
#include
typedef long long ll;
ll a[200005];

using namespace std;
int main()
{
	ll n, x;
	cin >> n >> x;
	x = abs(x);
	for (ll i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	ll L, R,sum=0;
	for (L = 1, R = 1; R <= n && L <= R;)
	{
		sum = a[R] - a[L];
		if (sum == x)
		{
			cout << "Yes" << '\n';
			return 0;
		}
		if (sum < x)
		{
			R++;
		}
		else if (sum > x)
		{
			L++;
		}
	}
	cout << "No" << '\n';
	return 0;
	

}

你可能感兴趣的:(算法,c++,开发语言)