第十四届蓝桥杯 社区打卡Day15 超精简cpp代码

TralSun 2023-03-18 12:04:28

t4 收获不少,了解到st表和rmq问题,总结了下st表模板,还有stl居然有gcd函数

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 100, M = 20;
int n, f[N][21], a[N], cnt1;
void init() {
	for (int j = 0; j < M; ++j) 
		for (int i = 1; i + (1 << j) - 1 <= n; ++i)
			if (!j) f[i][j] = a[i];
			else f[i][j] = __gcd(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
int query(int l, int r) {
	int k = log2(r - l + 1);
	return __gcd(f[l][k], f[r - (1 << k) + 1][k]);
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n; 
	for (int i = 1; i <= n; ++i) cin >> a[i], cnt1 += (a[i] == 1);
	if (cnt1) return cout << n - cnt1, 0;
	init();
	if (query(1, n) != 1) return cout << -1, 0;
	int ans = n;
	for (int i = 1; i <= n; ++i) {
		int l = i + 1, r = n + 1;
		while (l < r) {
			int mid = l + r >> 1;
			if (query(i, mid) == 1) r = mid;
			else l = mid + 1;
		}
		if (query(i, r) == 1) ans = min(ans, r - i);
	}
	return cout << n - 1 + ans, 0;
}

t3

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

int main() {
	ll n; cin >> n;
	ll ans = 0;
	for (ll i = 2; i <= sqrt(n); ++i) {
		if (n % i == 0) {
			++ans;
			while (n % i == 0) {
				n /= i;
			}
		}
	}
	if (n > 1) ++ans;
	cout << ans;
	return 0;
}

t2

学到不少,文件读取方式,读入的都是字符类型,不能直接当成int类型输入

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int n = 2e6;

int fun(string s) {
	int res = 0;
	for (int i = 0; i < s.size(); ++i) {
		res = res * 10 + s[i] - '0';
	}
	return res;
}
bool check(int x) {
	for (int i = 2; i <= sqrt(x); ++i) {
		if (x % i == 0) return false;
	}
	return true;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
//	ifstream infile; 
//    infile.open("D:\primes.txt");
//	int ans = 0;
//	string buf;
//	while (infile >> buf) {
//		if (check(fun(buf))) ++ans;
//	}	
//	cout << ans;
//	infile.close();
	
	cout << 342773;
	return 0;
}

t1

纯暴力等呗QAQ,规律不好找啊。

...全文
19 回复 打赏 收藏 举报
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
发帖
高校算法学习社区

4.2w+

社区成员

和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
帖子事件
创建了帖子
2023-03-18 12:04
社区公告

 刷题!