結果

問題 No.2074 Product is Square ?
ユーザー 👑 p-adicp-adic
提出日時 2022-09-17 08:05:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 2,000 ms
コード長 2,278 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 200,156 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 17:28:20
合計ジャッジ時間 7,540 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 41 ms
4,376 KB
testcase_12 AC 136 ms
4,380 KB
testcase_13 AC 484 ms
4,380 KB
testcase_14 AC 139 ms
4,384 KB
testcase_15 AC 43 ms
4,376 KB
testcase_16 AC 141 ms
4,376 KB
testcase_17 AC 479 ms
4,376 KB
testcase_18 AC 127 ms
4,376 KB
testcase_19 AC 40 ms
4,376 KB
testcase_20 AC 138 ms
4,376 KB
testcase_21 AC 487 ms
4,380 KB
testcase_22 AC 131 ms
4,376 KB
testcase_23 AC 41 ms
4,380 KB
testcase_24 AC 138 ms
4,380 KB
testcase_25 AC 473 ms
4,380 KB
testcase_26 AC 128 ms
4,380 KB
testcase_27 AC 40 ms
4,380 KB
testcase_28 AC 141 ms
4,384 KB
testcase_29 AC 478 ms
4,376 KB
testcase_30 AC 130 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

#define CIN( LL , A ) LL A; cin >> A 
#define GETLINE( A ) string A; getline( cin , A ) 
#define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR ) 
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( ll VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ ) 
#define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) 
#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) 
#define RETURN( ANSWER ) cout << ( ANSWER ) << endl; return 0 
#define DOUBLE( PRECISION , ANSWER ) cout << fixed << setprecision( PRECISION ) << ( ANSWER ) << endl; return 0 
#define MIN( A , B ) A < B ? A : B;
#define MAX( A , B ) A < B ? B : A;

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

int main()
{
  CIN( ll , T );
  REPEAT( T ){
    CIN( ll , N );
    list<ll> coprime{};
    FOR( i , 0 , N ){
      CIN( ll , Ai );
      auto itr = coprime.begin() , end = coprime.end();
      while( itr != end ){
	ll& coprime_j = *itr;
	ll coprime_j_c = coprime_j;
	ll Ai_c = Ai;
	while( coprime_j_c > 0 && Ai_c > 0 ){
	  if( coprime_j_c < Ai_c ){
	    Ai_c -= coprime_j_c * ( Ai_c / coprime_j_c );
	  } else {
	    coprime_j_c -= Ai_c * ( coprime_j_c / Ai_c );
	  }
	}
	ll gcd = coprime_j_c + Ai_c;
	coprime_j /= gcd;
	Ai /= gcd; 
	if( coprime_j == 1 ){
	  itr = coprime.erase( itr );
	} else {
	  itr++;
	}
      }
      if( Ai != 1 ){
	coprime.push_back( Ai );
      }
    }
    bool square = true;
    FOR_ITR( coprime , itr , end ){
      ll& Ai = *itr;
      ll sqrt = 0;
      ll power = 1;
      while( power * power < Ai ){
	power *= 2;
      }
      while( power != 0 ){
	ll sqrt_new = sqrt + power;
	ll sqrt_new_sq = sqrt_new * sqrt_new;
	while( sqrt_new_sq > Ai ){
	  power /= 2;
	  sqrt_new = sqrt + power;
	  sqrt_new_sq = sqrt_new * sqrt_new;
	}
	if( sqrt_new_sq == Ai ){
	  break;
	} else {
	  sqrt = sqrt_new;
	  power /= 2;
	}
      }
      if( power == 0 ){
	square = false;
	break;
      }
    }
    cout << ( square ? "Yes" : "No" ) << endl;
  }
  return 0;
}
0