ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1024. Permutations

TLE at test#11
Posted by Madhav 17 Jun 2008 17:22
I am getting time limit exceeded using a naive method.How can i write better program.pls some one help me.
this is my code

#include<iostream>
using namespace std;
int n;
int main(){
        cin>>n;int k=1;int i;
        int *a=new int[n+1];
        int *b=new int[n+1];
        int *c=new int[n+1];
        for(i=1;i<=n;i++){
                cin>>a[i];
                b[i]=a[i];
        }
        while(1){
                for(i=1;i<=n;i++)
                        if(b[i]!=i)
                                break;
                if(i==n+1){
                        cout<<k<<endl;
                        break;
                }
                for(i=1;i<=n;i++)
                        c[i]=b[i];
                for(i=1;i<=n;i++){
                        b[i]=c[a[i]];
                }
                k++;
        }
        return 0;
}
Re: TLE at test#11
Posted by Vedernikoff Sergey 18 Jun 2008 02:39
Answer can be up to 10^9, and it is fairly easy to construct such a test - so, try to develop a math solution in O(N)
Re: TLE at test#11
Posted by Madhav 19 Jun 2008 23:50
All of them using something like gcd and lcm.Can u tell me the idea.