結果

問題 No.2447 行列累乗根
ユーザー 👑 p-adicp-adic
提出日時 2023-04-07 14:44:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 5,341 bytes
コンパイル時間 3,337 ms
コンパイル使用メモリ 221,060 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-15 03:41:20
合計ジャッジ時間 11,324 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 2 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 2 ms
5,248 KB
testcase_17 AC 2 ms
5,248 KB
testcase_18 AC 5 ms
5,248 KB
testcase_19 AC 29 ms
5,248 KB
testcase_20 AC 266 ms
5,248 KB
testcase_21 AC 266 ms
5,248 KB
testcase_22 AC 272 ms
5,248 KB
testcase_23 AC 264 ms
5,248 KB
testcase_24 AC 260 ms
5,248 KB
testcase_25 AC 252 ms
6,816 KB
testcase_26 AC 257 ms
6,816 KB
testcase_27 AC 268 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ( "O3" )
#pragma GCC optimize( "unroll-loops" )
#pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define MAIN main
#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr )
#define CEXPR( LL , BOUND , VALUE ) constexpr const LL BOUND = VALUE
#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 REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES )
#define QUIT return 0
#define COUT( ANSWER ) cout << ( ANSWER ) << "\n"
#define SET_PRECISION( PRECISION ) cout << fixed << setprecision( PRECISION )

template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; }

template <typename T>
class TwoByTwoMatrix
{

public:
  T m_M00;
  T m_M01;
  T m_M10;
  T m_M11;

public:
  inline TwoByTwoMatrix( const T& M00 , const T& M01 , const T& M10 , const T& M11 ) noexcept;
  TwoByTwoMatrix<T>& operator=( const TwoByTwoMatrix<T>& mat ) noexcept;

  inline TwoByTwoMatrix<T> operator*( const TwoByTwoMatrix<T>& mat );
  
};

template <typename T> inline TwoByTwoMatrix<T>::TwoByTwoMatrix( const T& M00 , const T& M01 , const T& M10 , const T& M11 ) noexcept : m_M00( M00 ) , m_M01( M01 ) , m_M10( M10 ) , m_M11( M11 ) {}

template <typename T>
TwoByTwoMatrix<T>& TwoByTwoMatrix<T>::operator=( const TwoByTwoMatrix<T>& mat ) noexcept
{

  if( &mat != this ){

    m_M00 = mat.m_M00;
    m_M01 = mat.m_M01;
    m_M10 = mat.m_M10;
    m_M11 = mat.m_M11;

  }

  return *this;

}

template <typename T> inline TwoByTwoMatrix<T> TwoByTwoMatrix<T>::operator*( const TwoByTwoMatrix<T>& mat ) { return TwoByTwoMatrix<T>( m_M00 * mat.m_M00 + m_M01 * mat.m_M10 , m_M00 * mat.m_M01 + m_M01 * mat.m_M11 , m_M10 * mat.m_M00 + m_M11 * mat.m_M10 , m_M10 * mat.m_M01 + m_M11 * mat.m_M11 ); }

void stod( const string& s , ll& num , double& answer )
{
  bool negative;
  uint i_start;
  if( s.substr( 0 , 1 ) == "-" ){
    negative = true;
    i_start = 1;
  } else {
    negative = false;
    i_start = 0;
  }
  uint size = s.size();
  uint exponent = 0;
  num = 0;
  FOR( i , i_start , size ){
    string c = s.substr( i , 1 );
    if( c == "." ){
      assert( exponent == 0 );
      assert( i - i_start == 1 );
      exponent = size - 1 - i;
      assert( exponent == 4 );
    } else {
      num = num * 10 + stoi( c );
    }
  }
  answer = ( negative ? - num : num );
  while( exponent != 0 ){
    answer *= 0.1;
    exponent--;
  }
  return;
}

int MAIN()
{
  UNTIE;
  SET_PRECISION( 5 );
  double M_entry[2][2];
  double ( &M0 )[2] = M_entry[0];
  double ( &M1 )[2] = M_entry[1];
  double& M00 = M0[0];
  double& M01 = M0[1];
  double& M10 = M1[0];
  double& M11 = M1[1];
  ll num[2][2];
  double e[2];
  double& e0 = e[0];
  double& e1 = e[1];
  double cbrt_e[2];
  double& cbrt_e0 = cbrt_e[0];
  double& cbrt_e1 = cbrt_e[1];
  double U_entry[2][2];
  double ( &U0 )[2] = U_entry[0];
  double ( &U1 )[2] = U_entry[1];
  double& U00 = U0[0];
  double& U01 = U0[1];
  double& U10 = U1[0];
  double& U11 = U1[1];
  CEXPR( int , bound_T , 100000 );
  CIN_ASSERT( T , 1 , bound_T );
  CEXPR( double , llim_M , 0.00005 );
  CEXPR( double , llim_det , 0.000000005 );
  CEXPR( double , border_e0 , 0.00000001 );
  CEXPR( double , llim_disc , 0.000000005 );
  CEXPR( double , llim_normsq , 0.000000005 );
  REPEAT( T ){
    FOR( i , 0 , 2 ){
      double ( &Mi )[2] = M_entry[i];
      ll ( &numi )[2] = num[i];
      FOR( j , 0 , 2 ){
	CIN( string , S );
	stod( S.substr( 0 , S.size() - 1 ) , numi[j] , Mi[j] );
      }
    }
    if( Absolute( M01 ) < llim_M && Absolute( M10 ) < llim_M ){
      cout << cbrt( M00 ) << " " << 0.0 << "\n";
      cout << 0.0 << " " << cbrt( M11 ) << "\n";
    } else {
      double tr = M00 + M11;
      double det = M00 * M11 - M01 * M10;
      if( Absolute( det ) < llim_det ){
	e0 = cbrt_e[0] = 0.0;
	e1 = tr;
	cbrt_e[1] = cbrt( tr );
      } else {
	double disc = tr * tr - 4.0 * det;
	double discrt = ( disc < llim_disc ? 0.0 : sqrt( disc ) );
	e0 = ( tr + discrt ) / 2.0;
	e1 = ( tr - discrt ) / 2.0;
	if( Absolute( e0 ) > Absolute( e1 ) ){
	  swap( e0 , e1 );
	}
	cbrt_e1 = cbrt( e1 );
	cbrt_e0 = Absolute( e0 ) < border_e0 ? cbrt( det ) / cbrt_e1 : cbrt( e0 );
      }
      FOR( j , 0 , 2 ){
	double& ej = e[j];
	double& U0j = U0[j];
	double& U1j = U1[j];
	U0j = M10;
	U1j = ej - M00;
	double normsq = U0j * U0j + U1j * U1j;
	if( normsq < llim_normsq ){
	  U0j = ej - M11;
	  U1j = M01;
	  normsq = U0j * U0j + U1j * U1j;
	}
	assert( normsq >= llim_normsq );
	double norm = sqrt( normsq );
	U0j /= norm;
	U1j /= norm;
      }
      TwoByTwoMatrix<double> U{ U00 , U01 , U10 , U11 };
      TwoByTwoMatrix<double> Uinv{ U00 , U10 , U01 , U11 };
      TwoByTwoMatrix<double> Dcbrt{ cbrt_e0 , 0.0 , 0.0 , cbrt_e1 };
      TwoByTwoMatrix<double> Mcbrt = U * Dcbrt * Uinv;
      cout << Mcbrt.m_M00 << " " << Mcbrt.m_M01 << "\n";
      cout << Mcbrt.m_M10 << " " << Mcbrt.m_M11 << "\n";
    }
  }
  QUIT;
}
0