Skip to content

Commit 29ffe5c

Browse files
committed
sessions up to Mar 14; misc. practice
1 parent 0c75cf0 commit 29ffe5c

36 files changed

+1622
-5
lines changed
Binary file not shown.

2020/jan18/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
##
2+
3+
Contest:
4+
<https://codeforces.com/group/L0zlhp0AHw/contest/100610>
5+
6+
Problem C:
7+
We solve it using commuting functions.
8+
The cycle in the commuting functions must have lengths sharing factors.
9+

2020/jan18/a-alien.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long int lld;
5+
lld MAX = numeric_limits<lld>::max();
6+
7+
/*
8+
g++ -std=c++11 a-alien.cpp -o k
9+
*/
10+
int main() {
11+
freopen("acm.in", "r", stdin);
12+
freopen("acm.out", "w", stdout);
13+
14+
int n, m, i, j;
15+
cin >> n >> m;
16+
int a[n]; int b[m];
17+
for(i = 0; i < n; i++)
18+
cin >> a[i];
19+
for(i = 0; i < m; i++)
20+
cin >> b[i];
21+
22+
//cout << "DEBUG" << endl;
23+
string o = "";
24+
for(i = 0; i < n; i++) {
25+
o += "(10 ";
26+
for(j = 0; j < a[i]; j++) {
27+
o += " - 1";
28+
if(j == a[i] - 1) o += ")";
29+
else o += " ";
30+
}
31+
if(i == n - 1) o += " = 0";
32+
else o += " * ";
33+
}
34+
cout << o << endl;
35+
36+
return 0;
37+
}

2020/jan18/acm.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2 2
2+
2 4
3+
3 5

2020/jan18/acm.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

2020/jan18/c-commuting.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long int lld;
5+
lld MAX = numeric_limits<int>::max();
6+
7+
void debug() { cout << "DEBUG" << endl; }
8+
9+
template<class T>
10+
void print(T msg) { cout << msg << endl; }
11+
12+
typedef pair<int, int> P;
13+
14+
struct by_first {
15+
bool operator() (const vector<int> &a,
16+
const vector<int> &b) {
17+
return a[0] < b[0];
18+
}
19+
};
20+
21+
/*
22+
g++ -std=c++11 c-commuting.cpp -o k
23+
24+
1 -> 6
25+
2 -> 3
26+
3 -> 2
27+
4 -> 5
28+
5 -> 4
29+
6 -> 7
30+
7 -> 8
31+
8 -> 1
32+
33+
8
34+
6 3 2 5 4 7 8 1
35+
36+
1 -> 7
37+
2 -> 3
38+
3 -> 5
39+
4 -> 2
40+
5 -> 4
41+
6 -> 1
42+
7 -> 8
43+
8 -> 6
44+
45+
8
46+
7 3 5 2 4 1 8 6
47+
*/
48+
int main() {
49+
freopen("commuting.in", "r", stdin);
50+
freopen("commuting.out", "w", stdout);
51+
52+
int n, i, j, k;
53+
cin >> n;
54+
int f[n+1];
55+
int g[n+1];
56+
bool seen[n+1];
57+
vector<int> cycle;
58+
vector<int> *pcycle;
59+
vector<int>::iterator vit;
60+
vector<vector<vector<int>>> cycles(n+1);
61+
vector<vector<int>>::iterator sit;
62+
set<int> s;
63+
set<int> t;
64+
for(i = 1; i < n+1; i++) {
65+
cin >> f[i];
66+
g[i] = 0;
67+
seen[i] = false;
68+
}
69+
70+
// make cycles
71+
for(i = 1; i < n+1; i++) {
72+
if(seen[i] == false) {
73+
cycle = vector<int>();
74+
j = i;
75+
do {
76+
seen[j] = true;
77+
cycle.push_back(j);
78+
j = f[j];
79+
} while(i != j);
80+
k = cycle.size();
81+
cycles[k].push_back(cycle);
82+
s.insert(k);
83+
t.insert(k);
84+
}
85+
}
86+
87+
/*
88+
// list the cycles
89+
for(const vector<vector<int>> &cs : cycles) {
90+
for(const vector<int> &c : cs) {
91+
for(int p : c) {
92+
cout << p << " ";
93+
}
94+
cout << endl;
95+
}
96+
}
97+
*/
98+
99+
for(int i : s) {
100+
pcycle = NULL;
101+
for(int j : t) {
102+
if(i % j == 0) {
103+
if(j >= i) break;
104+
if(!pcycle || pcycle->at(0) > cycles[j][0][0]) {
105+
pcycle = &cycles[j][0];
106+
}
107+
}
108+
}
109+
110+
if(pcycle && pcycle->at(0) < cycles[i][0][0]) {
111+
// use pcycle representing cycle with order dividing i
112+
for(const vector<int> &c : cycles[i]) {
113+
vit = pcycle->begin();
114+
for(int p : c) {
115+
g[p] = *vit;
116+
vit++;
117+
if(vit == pcycle->end()) vit = pcycle->begin();
118+
}
119+
}
120+
t.erase(i);
121+
} else {
122+
// use first cycle of order i
123+
pcycle = &cycles[i][0];
124+
for(sit = cycles[i].begin(); sit != cycles[i].end(); sit++) {
125+
for(j = 0; j < pcycle->size(); j++)
126+
g[sit->at(j)] = pcycle->at(j);
127+
}
128+
}
129+
}
130+
131+
for(i = 1; i < n+1; i++) {
132+
cout << g[i];
133+
if(i == n) cout << endl;
134+
else cout << " ";
135+
}
136+
137+
/*
138+
// test f, g commute
139+
for(i = 1; i < n+1; i++) {
140+
cout << f[g[i]];
141+
if(i == n) cout << endl;
142+
else cout << " ";
143+
}
144+
for(i = 1; i < n+1; i++) {
145+
cout << g[f[i]];
146+
if(i == n) cout << endl;
147+
else cout << " ";
148+
}
149+
*/
150+
151+
return 0;
152+
}
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+

2020/jan18/c-commuting1.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef long long int lld;
5+
lld MAX = numeric_limits<int>::max();
6+
7+
/*
8+
g++ -std=c++11 c-commuting.cpp -o k
9+
did not work...
10+
11+
10
12+
2 3 4 5 6 7 8 1 9 10
13+
14+
10
15+
1 2 3 4 5 6 7 8 9 10
16+
17+
5
18+
5 2 4 3 1
19+
out: 1 2 2 2 5
20+
21+
8
22+
8 3 2 4 6 5 7 1
23+
out: 1 2 3 4 4 4 4 8
24+
25+
11
26+
6 8 3 5 4 7 1 9 2 10 11
27+
out: 1 2 3 3 3 6 7 8 9 3 3
28+
29+
9
30+
2 1 4 3 5 7 8 9 6
31+
*/
32+
int main() {
33+
freopen("commuting.in", "r", stdin);
34+
freopen("commuting.out", "w", stdout);
35+
36+
int n, i, j, k;
37+
cin >> n;
38+
int f[n+1];
39+
int g[n+1];
40+
for(i = 1; i < n+1; i++) {
41+
cin >> f[i];
42+
g[i] = 0;
43+
}
44+
45+
// k is the first fixed point in f
46+
k = 0;
47+
for(i = 1; i < n+1; i++) {
48+
if((bool) g[i]) {
49+
// identified i as part of a cycle that has elements j < k
50+
// do nothing
51+
} else if(i == f[i]) {
52+
// i is a fixed point
53+
if(! (bool) k) {
54+
// landed on first fixed point
55+
k = i;
56+
}
57+
g[i] = k;
58+
} else {
59+
// i is in a cycle
60+
if(!k) {
61+
// has not reached first fixed point
62+
j = f[i];
63+
while(j != i) {
64+
g[j] = j;
65+
j = f[j];
66+
}
67+
g[i] = i;
68+
} else {
69+
// is past first fixed point
70+
g[i] = k;
71+
}
72+
}
73+
}
74+
75+
//cout << "f(g) ";
76+
//for(i = 1; i < n+1; i++) {
77+
// cout << f[g[i]];
78+
// if(i == n) cout << endl;
79+
// else cout << " ";
80+
//}
81+
//cout << "g(f) ";
82+
//for(i = 1; i < n+1; i++) {
83+
// cout << g[f[i]];
84+
// if(i == n) cout << endl;
85+
// else cout << " ";
86+
//}
87+
88+
for(i = 1; i < n+1; i++) {
89+
cout << g[i];
90+
if(i == n) cout << endl;
91+
else cout << " ";
92+
}
93+
94+
return 0;
95+
}

0 commit comments

Comments
 (0)