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

test #3 is quite werid
Posted by mathfrog 25 Aug 2007 19:12
// why wa, i was really confused
#include <stdio.h>
#define MAXN 1002
int perm[ MAXN ] ;
void swap(long long* x,long long* y )
{
    long t = *x;
        *x = *y;
        *y = t;
    }

long long gcd(long long m,long long n)
{
    long r;
    if( m < n )
        swap( &m,&n );
    do{
        r = m % n ;
        m = n;
        n = r;
        }while( r );
    return m;
    }

int main()
{
    int size , i, mark, q[ MAXN ] ;
    long long t[ MAXN ]  ;
    while( scanf("%d",&size ) != EOF ){
        for( i = 1 ; i <= size; i++ ){
                scanf("%d",&perm[ i ]);
                q[ i ] = perm[ i ] ;
                t[ i ] = 1;
                }
        for( i = 1; i <= size; i++ )
            for( ; i != perm[ i ] ; t[ i ]++ )
                perm[ i ] = q[ perm[ i ] ] ;
        for( i = 1; i <= size - 1; i++ )
              t[ i ] = ( t[ i ] / gcd( t[ i ],t[ i + 1 ] ) ) * t[ i + 1 ]  ;
        printf("%I64d\n",t[ i - 1 ] );
        for( i = 1 ; i <= size; i++ )
                t[ i ] = 1;
        }
    return 0;
    }
Re: test #3 is quite werid
Posted by Razdolbay from SIS 26 Aug 2007 14:49
Tests for your program:
1  1
5  2 1 4 5 3

Use

for( i = 1; i <= size - 1; i++ )
t[ i + 1 ] = ( t[ i ] / gcd( t[ i ],t[ i + 1 ] ) ) * t[ i + 1 ] ;
printf("%I64d\n",t[ i ] );

instead of

for( i = 1; i <= size - 1; i++ )
t[ i ] = ( t[ i ] / gcd( t[ i ],t[ i + 1 ] ) ) * t[ i + 1 ] ;
printf("%I64d\n",t[ i - 1 ] );

Edited by author 26.08.2007 14:50