ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1024. Permutations

Posted by Ghiorghiu Ioan-Viorel 16 Apr 2017 00:12
If you don't feel like reviewing my code at least give me some tests please.
So... um... I think my code should work... I tested it along with an accepted program... and it worked fine... Here's the code:

#define DM 1001
#include <bitset>
#include <iostream>
#include <set>
using namespace std;

bitset <DM> bs;
bool verif;
int n, c, lg[DM], mp[DM];
long long v;
set <int> s;

void recursiv (int a)
{
bs[a] = 1;
++v;
if (s.find(mp[a]) != s.end())
return;
s.insert(a);
recursiv(mp[a]);
}

int main ()
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> mp[i];
if (mp[i] == i)
bs[i] = 1;
}
for (int i = 1; i <= n; ++i)
if (!bs[i])
{
recursiv(i);
s.clear();
lg[++c] = v;
v = 0;
}
for (v = lg[1]; !verif; ++v)
{
verif = 1;
for (int i = 1; i <= c; ++i)
if (v%lg[i] != 0)
verif = 0, i = c + 1;
}
cout << v - 1;
return 0;
}