# 第十四届蓝桥杯 社区打卡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

``````#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

...全文
19 回复 打赏 收藏 举报

4.2w+

2023-03-18 12:04

刷题！