Skip to content

Commit

Permalink
Add problem Fixed-point theorem from UVA #math
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonber committed Nov 18, 2018
1 parent 50b1882 commit b2e81b0
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions uva/13074.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;

int mcm (int a, int b) {
return (a * b) / __gcd(a, b);
}

int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
while (cin >> n) {
if (!n) break;

vector<int> v(n);
for (auto &i: v) cin >> i;

vector<set<int>> count(n);
vector<int> cur(n), next(n);

for (int i = 1; i <= n; ++i) cur[i - 1] = i;

vector<int> cycle(n, 0);
bool found = true;
while (found) {
found = false;
for (int i = 0; i < n; ++i) {
int t = cur[v[i] - 1];
next[i] = t;
if (count[i].count(t) == 0) {
count[i].insert(t);
cycle[i] ++;
found = true;
}
}
cur = next;
}

set<int> st(cycle.begin(), cycle.end());
int ans = 1;
for (auto i: st) {
ans = mcm(ans, i);
}

cout << ans << endl;
}

return 0;
}

0 comments on commit b2e81b0

Please sign in to comment.