Skip to content

Commit b8d6522

Browse files
committed
Some CSES
1 parent 7c52a35 commit b8d6522

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2109
-59
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(int i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
ll n;
11+
cin >> n;
12+
while (n != 1) {
13+
cout << n << ' ';
14+
if (n & 1) {
15+
n *= 3;
16+
n++;
17+
} else {
18+
n /= 2;
19+
}
20+
}
21+
cout << 1 << '\n';
22+
return 0;
23+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(int i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
char curr = '_', x;
11+
int mx = 0, cnt = 0;
12+
while (cin >> x) {
13+
if (x == curr) cnt++;
14+
else {
15+
mx = max(mx, cnt);
16+
cnt = 1;
17+
curr = x;
18+
}
19+
}
20+
mx = max(mx, cnt);
21+
cout << mx << '\n';
22+
return 0;
23+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(int i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
int n;
11+
cin >> n;
12+
if (n == 2 || n == 3) {
13+
cout << "NO SOLUTION\n";
14+
} else if (n == 1) {
15+
cout << "1\n";
16+
} else if (n == 4) {
17+
cout << "2 4 1 3\n";
18+
} else {
19+
FOR(i, 0, n) {
20+
if (i & 1) {
21+
cout << n - i / 2 - ceil(n / 2.0) << ' ';
22+
} else {
23+
cout << n - i / 2 << ' ';
24+
}
25+
}
26+
cout << '\n';
27+
}
28+
return 0;
29+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC Optimize("O3")
3+
#define FOR(i, x, y) for (int i = x; i < y; i++)
4+
#define MOD 1000000007
5+
typedef long long ll;
6+
using namespace std;
7+
8+
int main() {
9+
iostream::sync_with_stdio(false);
10+
cin.tie(0);
11+
int t;
12+
cin >> t;
13+
FOR(i, 0, t) {
14+
ll x, y;
15+
cin >> x >> y;
16+
ll ans = max(x, y) * max(x, y);
17+
if (max(x, y) % 2 == 0) {
18+
if (max(x, y) == x) ans -= min(x, y) - 1;
19+
else ans -= 2 * y - x - 1;
20+
}
21+
else {
22+
if (max(x, y) == y) ans -= min(x, y) - 1;
23+
else ans -= 2 * x - y - 1;
24+
}
25+
cout << ans << '\n';
26+
}
27+
return 0;
28+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(ll i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
ll n;
11+
cin >> n;
12+
FOR(i, 1, n + 1) {
13+
cout << ((i - 1) * (i + 4) * (i * i - 3 * i + 4)) / 2 << '\n';
14+
}
15+
return 0;
16+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(int i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
int n, a[500000];
11+
cin >> n;
12+
FOR(i, 0, n - 1) {
13+
cin >> a[i];
14+
}
15+
sort(a, a + n - 1);
16+
FOR(i, 0, n - 1) {
17+
if (a[i] != i + 1) {
18+
return cout << i + 1 << '\n', 0;
19+
}
20+
}
21+
cout << n << '\n';
22+
return 0;
23+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
typedef long long ll;
7+
8+
int main() {
9+
ll n;
10+
cin >> n;
11+
ll t = n * (n + 1) / 2;
12+
if (t % 2)
13+
cout << "NO\n";
14+
else {
15+
t /= 2;
16+
vector<int> a;
17+
ll s = 0;
18+
ll e = 0;
19+
ll i = n;
20+
while (true) {
21+
a.push_back(i);
22+
s += i--;
23+
e = t - s;
24+
if (!e) break;
25+
if (e <= i) {
26+
a.push_back(e);
27+
break;
28+
}
29+
}
30+
cout << "YES\n" << a.size() << endl;
31+
for (int j = 0; j < a.size(); j++) {
32+
cout << a[j];
33+
if (j < a.size() - 1) cout << ' ';
34+
}
35+
cout << '\n';
36+
cout << n - a.size() << '\n';
37+
for (ll j = i; j > 0; j--) {
38+
if (j == e) continue;
39+
cout << j;
40+
if ((e != 1 && j > 1) || (e == 1 && j > 2)) cout << ' ';
41+
}
42+
cout << '\n';
43+
}
44+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC optimize("O3")
3+
#define FOR(i, x, y) for(ll i = x; i < y; i++)
4+
typedef long long ll;
5+
using namespace std;
6+
7+
int main() {
8+
ios_base::sync_with_stdio(false);
9+
cin.tie(NULL);
10+
ll n;
11+
cin >> n;
12+
ll curr = 0, tot = 0;
13+
FOR(i, 0, n) {
14+
ll x;
15+
cin >> x;
16+
if (x < curr) tot += (curr - x);
17+
curr = max(curr, x);
18+
}
19+
cout << tot << '\n';
20+
return 0;
21+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC Optimize("O3")
3+
#define FOR(i, x, y) for (int i = x; i < y; i++)
4+
#define MOD 1000000007
5+
typedef long long ll;
6+
using namespace std;
7+
8+
ll expo(ll base, int n) {
9+
ll ans = 1;
10+
while (n) {
11+
if (n & 1) ans = (ans * base) % MOD;
12+
base = (base * base) % MOD;
13+
n >>= 1;
14+
}
15+
return ans;
16+
}
17+
18+
int main() {
19+
iostream::sync_with_stdio(false);
20+
cin.tie(0);
21+
int n;
22+
cin >> n;
23+
cout << expo(2, n);
24+
return 0;
25+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC Optimize("O3")
3+
#define FOR(i, x, y) for (int i = x; i < y; i++)
4+
#define MOD 1000000007
5+
typedef long long ll;
6+
using namespace std;
7+
8+
int main() {
9+
iostream::sync_with_stdio(false);
10+
cin.tie(0);
11+
int n;
12+
cin >> n;
13+
int ans = 0;
14+
int pow = 5;
15+
while (n / pow) {
16+
ans += n / pow;
17+
pow *= 5;
18+
}
19+
cout << ans << '\n';
20+
return 0;
21+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC Optimize("O3")
3+
#define FOR(i, x, y) for (int i = x; i < y; i++)
4+
#define MOD 1000000007
5+
typedef long long ll;
6+
using namespace std;
7+
8+
set<string> strings;
9+
string s;
10+
bool visited[8];
11+
int n;
12+
13+
void dfs(int level, int indx, string curr) {
14+
visited[indx] = true;
15+
if (level == n) {
16+
strings.insert(curr + s[indx]);
17+
} else {
18+
FOR(i, 0, n) {
19+
if (!visited[i]) dfs(level + 1, i, curr + s[indx]);
20+
}
21+
}
22+
visited[indx] = false;
23+
}
24+
25+
int main() {
26+
iostream::sync_with_stdio(false);
27+
cin.tie(0);
28+
cin >> s;
29+
n = s.size();
30+
31+
FOR(i, 0, n) {
32+
dfs(1, i, "");
33+
}
34+
35+
cout << strings.size() << '\n';
36+
for (string i : strings) cout << i << '\n';
37+
return 0;
38+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
#pragma GCC Optimize("O3")
3+
#define FOR(i, x, y) for (int i = x; i < y; i++)
4+
#define MOD 1000000007
5+
typedef long long ll;
6+
using namespace std;
7+
8+
int a[20];
9+
ll tot = 0;
10+
11+
int main() {
12+
iostream::sync_with_stdio(false);
13+
cin.tie(0);
14+
int n;
15+
cin >> n;
16+
FOR(i, 0, n) {
17+
cin >> a[i];
18+
tot += a[i];
19+
}
20+
21+
ll ans = INT_MAX;
22+
FOR(i, 0, 1<<n) {
23+
ll sm = 0;
24+
int temp = i;
25+
FOR(j, 0, n) {
26+
if (temp & 1) sm += a[j];
27+
temp >>= 1;
28+
}
29+
ans = min(ans, abs(tot - 2 * sm));
30+
}
31+
32+
cout << ans << '\n';
33+
return 0;
34+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <bits/stdc++.h>
2+
#define FOR(i, x, y) for (int i = x; i < y; i++)
3+
typedef long long ll;
4+
using namespace std;
5+
6+
int ans = 0;
7+
char board[8][8];
8+
bool visited[8], diag_l[16], diag_r[16];
9+
10+
void dfs(int curr, int depth) {
11+
visited[curr] = true;
12+
diag_l[curr - depth + 7] = true;
13+
diag_r[curr + depth] = true;
14+
15+
if (depth == 7) ans++;
16+
else {
17+
FOR(i, 0, 8) {
18+
if (board[depth + 1][i] == '.' && !visited[i] && !diag_l[i - depth + 6] && !diag_r[i + depth + 1]) {
19+
dfs(i, depth + 1);
20+
}
21+
}
22+
}
23+
24+
visited[curr] = false;
25+
diag_l[curr - depth + 7] = false;
26+
diag_r[curr + depth] = false;
27+
}
28+
29+
int main() {
30+
ios_base::sync_with_stdio(0);
31+
cin.tie(0);
32+
FOR(i, 0, 8) FOR(j, 0, 8) cin >> board[i][j];
33+
FOR(i, 0, 8) if (board[0][i] == '.') dfs(i, 0);
34+
cout << ans;
35+
return 0;
36+
}

0 commit comments

Comments
 (0)