Follows 374.c (Total 23 lines):

/* @JUDGE_ID:4461XX 374 C */
/* A */
#include <stdio.h>

void main( void )
{
	unsigned long b , m , i , p , n , top ;
	while( scanf( "%lu %lu %lu" , &b , &m , &p )==3 )
	{
		b=b%p ; n=b ;
		for ( i=2 ; ; i++ )
		{
			n=(b*n)%p ;
			if ( n==b ) { top=i-1 ; break ; }
		}
		n=b ;
		m=m%top ; if ( m==0 ) m=top ;
		for ( i=2 ; i<=m ; i++ ) n=(b*n)%p ;
		if ( b==0 ) n=0 ;
		if ( m==0 ) n=1 ;
		printf( "%lu\n" , n ) ;
	}
}

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com