結果

問題 No.181 A↑↑N mod M
ユーザー 👑 p-adicp-adic
提出日時 2022-10-31 16:36:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,887 bytes
コンパイル時間 1,933 ms
コンパイル使用メモリ 200,720 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 13:29:59
合計ジャッジ時間 3,698 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;

#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define CIN( LL , A ) LL A; cin >> A 
#define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) 
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX ) 
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) 
#define QUIT return 0 
#define RETURN( ANSWER ) cout << ( ANSWER ) << "\n"; QUIT 
#define RESIDUE( A , P ) ( A >= 0 ? A % P : P - ( - A - 1 ) % P - 1 ) 

#define POWER( ANSWER , ARGUMENT , EXPONENT , MODULO )			\
  TYPE_OF( ARGUMENT ) ANSWER = 1;					\
  {									\
    TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT );	\
    TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT );	\
    while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){			\
      if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){			\
	ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO;	\
      }									\
      ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO; \
      EXPONENT_FOR_SQUARE_FOR_POWER /= 2;				\
    }									\
  }									\



ll CheckBounded( const ll& A , const ll& N , const ll& bound )
{
  if( bound <= 1 ){
    return -1;
  }
  if( N == 0 ){
    return 1;
  }
  ll A_power = 1;
  ll exponent = 0;
  while( A_power < bound ){
    A_power *= A;
    exponent++;
  }
  ll answer = CheckBounded( A , N - 1 , exponent );
  if( answer != -1 ){
    POWER( A_power_new , A , answer , bound );
    answer = A_power_new;
  }
  return answer;
}

inline constexpr const ll bound_M = 2000;

ll Solve( const ll& A , const ll& N , const ll& M )
{
  if( M == 1 ){
    return 0;
  } else if( A == 1 || N == 0 ){
    return 1;
  }
  ll A_power = 1;
  vector<ll> memory{};
  memory.push_back( A_power );
  uint memory_size = 1;
  uint period;
  bool searching = true;
  while( searching ){
    A_power = ( A_power * A ) % M;
    FOR( i , 0 , memory_size ){
      if( A_power == memory[i] ){
	period = memory_size - i;
	searching = false;
	break;
      }
    }
    memory.push_back( A_power );
    memory_size++;
  }
  ll answer = CheckBounded( A , N - 1 , bound_M );
  if( answer == -1 ){
    ll exponent = Solve( A , N - 1 , period ) - bound_M;
    exponent = RESIDUE( exponent , period );
    POWER( tetration , A , bound_M + exponent , M );
    answer = tetration;
  } else {
    POWER( tetration , A , answer , M );
    answer = tetration;
  }
  return answer % M;
}

int main()
{
  UNTIE;
  constexpr const ll bound = 1000000000;
  CIN_ASSERT( A , 1 , bound );
  CIN_ASSERT( N , 0 , bound );
  CIN_ASSERT( M , 1 , bound_M );
  RETURN( Solve( A , N , M ) );
}
0