Skip to content

Commit 005d3ff

Browse files
Add files via upload
1 parent 8195fc0 commit 005d3ff

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
for _ in range(int(input())):
2+
print("YES" if (int(input()) - 1) % 3 == 0 else "NO")

Other_Tasks/CF2071_B_Perfecto.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
from functools import lru_cache
3+
4+
@lru_cache(maxsize=None)
5+
def isSquare(n):
6+
if n == 0 or n == 1:
7+
return True
8+
9+
start = 1
10+
end = n
11+
while start <= end:
12+
mid = (start + end) // 2
13+
if mid * mid == n:
14+
return True
15+
elif mid * mid < n:
16+
start = mid + 1
17+
else:
18+
end = mid - 1
19+
return False
20+
21+
22+
for _ in range(int(input())):
23+
n = int(input())
24+
25+
s = 0
26+
i = 1
27+
ans = []
28+
while i < n+1:
29+
s += i
30+
if isSquare(s):
31+
if i == n:
32+
ans = [-1]
33+
break
34+
else:
35+
ans.append(i+1)
36+
ans.append(i)
37+
s += i+1
38+
i += 2
39+
else:
40+
ans.append(i)
41+
i += 1
42+
43+
print(" ".join(map(str, ans)))
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
11+
int main(){
12+
int tc;
13+
cin >> tc;
14+
while (tc--){
15+
int n, st, en;
16+
cin >> n >> st >> en;
17+
st--, en--;
18+
vector<vector<int>> adjlist(n, vector<int>());
19+
for (int i=0; i<n-1; i++){
20+
int u, v;
21+
cin >> u >> v;
22+
u--, v--;
23+
adjlist[u].push_back(v);
24+
adjlist[v].push_back(u);
25+
}
26+
27+
// we first find the shortest path from S to E
28+
vector<int> dist(n, -1);
29+
queue<int> q;
30+
q.push(st);
31+
dist[st] = 0;
32+
while (!q.empty()){
33+
int u = q.front();
34+
q.pop();
35+
for (int v : adjlist[u]){
36+
if (dist[v] == -1){
37+
dist[v] = dist[u] + 1;
38+
q.push(v);
39+
}
40+
}
41+
}
42+
vector<int> path;
43+
path.push_back(en);
44+
int cur = en;
45+
while (cur != st){
46+
for (int v : adjlist[cur]){
47+
if (dist[v] == dist[cur] - 1){
48+
path.push_back(v);
49+
cur = v;
50+
break;
51+
}
52+
}
53+
}
54+
reverse(path.begin(), path.end());
55+
unordered_set<int> path_set(path.begin(), path.end());
56+
57+
58+
// now we traverse along the path, exploring the surrounding sub trees first before going down the path
59+
vector<int> ans;
60+
dist.assign(n, -1);
61+
vector<pair<int, int>> subtree; // dist, node
62+
int pathLen = path.size();
63+
for (int i=0; i<pathLen; i++){
64+
int root = path[i];
65+
dist[root] = 0;
66+
for (int v : adjlist[root]){
67+
if (path_set.find(v) == path_set.end()){
68+
q.push(v);
69+
dist[v] = 1;
70+
}
71+
}
72+
73+
while (!q.empty()){
74+
int u = q.front();
75+
subtree.push_back({dist[u], u});
76+
q.pop();
77+
for (int v : adjlist[u]){
78+
if (dist[v] == -1){
79+
dist[v] = dist[u] + 1;
80+
q.push(v);
81+
82+
}
83+
}
84+
}
85+
86+
for (auto it = subtree.rbegin(); it != subtree.rend(); it++){
87+
ans.push_back(it->second + 1);
88+
}
89+
90+
subtree.clear();
91+
ans.push_back(root + 1);
92+
}
93+
94+
for (auto x: ans){
95+
cout << x << " ";
96+
}
97+
cout << endl;
98+
99+
}
100+
101+
102+
103+
return 0;
104+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
template <typename T=int>
11+
struct StaticSum {
12+
const int n;
13+
vector<T> pfs; // arr[i] contains the range sum from 0 to i inclusively
14+
15+
T rsq(int l, int r) const { // range sum from l to r inclusively
16+
if (l < 0 || r >= n) assert(false); // boundary condition check
17+
if (l == 0) return pfs[r];
18+
return (pfs[r] ^ pfs[l - 1]);
19+
}
20+
21+
StaticSum(const vector<T>& arr) : n(arr.size()), pfs(arr) {
22+
for (int i = 1; i < n; i++) {
23+
pfs[i] ^= pfs[i - 1];
24+
}
25+
}
26+
};
27+
28+
29+
int main(){
30+
int tc;
31+
cin >> tc;
32+
while (tc--){
33+
int n;
34+
ll q;
35+
cin >> n >> q >> q;
36+
vector<int> v(n+1);
37+
for (int i=0; i<n; i++){
38+
cin >> v[i+1];
39+
}
40+
41+
42+
if (n%2 == 0){ // make n odd
43+
int acc = 0;
44+
for (int i=1;i<=n/2; i++){
45+
acc ^= v[i];
46+
}
47+
v.emplace_back(acc);
48+
n++;
49+
}
50+
51+
StaticSum<int> ss(v);
52+
53+
int ans = 0;
54+
while (q > 0){
55+
if (q <= n){
56+
cout << (v[q] ^ ans) << endl;
57+
break;
58+
}
59+
else if (q / 2LL <= n){
60+
cout << (ss.rsq(1, q/2LL) ^ ans) << endl;
61+
break;
62+
}
63+
else if ((q / 2LL) %2 == 1){
64+
cout << (ss.rsq(1, n) ^ ans) << endl;
65+
break;
66+
}
67+
else{
68+
q = (q/2LL);
69+
ans ^= ss.rsq(1, n);
70+
}
71+
}
72+
73+
74+
}
75+
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)