public static long calc ( int P , int K ) {
  if ( K == 0 ) {
    return modPow ( P , P - 1 ) ;
  }
  if ( K == 1 ) {
    return modPow ( P , P ) ;
  }
  int count = 0 ;
  boolean [ ] visited = new boolean [ P ] ;
  for ( int i = 1 ;
  i < visited . length ;
  i ++ ) {
    if ( visited [ i ] ) {
      continue ;
    }
    long j = i ;
    count ++ ;
    while ( ! visited [ ( int ) j ] ) {
      visited [ ( int ) j ] = true ;
      j = ( j * K ) % P ;
    }
  }
  return modPow ( P , count ) ;
}

public static long calc ( int P , int K ) { if ( K == 0 ) { return modPow ( P , P - 1 ) ; } if ( K == 1 ) { return modPow ( P , P ) ; } int count = 0 ; boolean [ ] visited = new boolean [ P ] ; for ( int i = 1 ; i < visited . length ; i ++ ) { if ( visited [ i ] ) { continue ; } long j = i ; count ++ ; while ( ! visited [ ( int ) j ] ) { visited [ ( int ) j ] = true ; j = ( j * K ) % P ; } } return modPow ( P , count ) ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (return_statement (method_invocation name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (return_statement (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (array_type element: (boolean_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (boolean_type) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (array_access array: (identifier) index: (identifier))) consequence: (block (continue_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (expression_statement (update_expression (identifier))) (while_statement condition: (parenthesized_expression (unary_expression operand: (array_access array: (identifier) index: (cast_expression type: (integral_type) value: (identifier))))) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (cast_expression type: (integral_type) value: (identifier))) right: (true))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (identifier)))))))) (return_statement (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier))))))