かくかくしかじか・・・

ゆるく、てきとうに

AtCoderで気をつけること①

データの範囲

  • 例えば、この問題 -> ABC158 B - Count Balls
  • 問題制約のデータNの範囲が1<N<1018となっている.
  • int型配列でこのデータを扱うことはできない
    • データ範囲: 約-21億から約21億であるため
    • 1kilo(103), 1mega(106), 1giga(109) < int < 1tera(1012) < 1peta(1015) < 1exa(1018) < long long int < 1zetta(1021) < 1yotta(1024)

各データ型の範囲

型名 サイズ ビット幅 最小値 最大値
int 4 32 -2147483648 2147483647
unsigned_int 4 32 0 4294967295
long long int 8 64 -9223372036854775808 9223372036854775807
int64_t 8 64 -9223372036854775808 9223372036854775807

long long int とint_64t

  • long longは64ビット以上の幅であることが保証されるが、64ビット固定の整数型が必要な場合には、int64_t型を使用することを推奨される
  • long longのように、longを2つ連続させることで「longより大きな型」であることを表現するのは非常に醜いものではあるが、C99およびコンパイラの実装による事実上の標準があったために、C++もそれにならった。
  • C言語およびC++言語ではint、long、long longのサイズは指定されておらず環境依存です。

検証プログラム

  cout << "int: " << sizeof(int) << "bytes" << endl; 
  cout << "___max: " << numeric_limits<int>::max() << endl;
  cout << "___min: " << numeric_limits<int>::min() << endl;
  
  cout << "unsigned int: " << sizeof(unsigned int) << "bytes" << endl; 
  cout << "___max: " << numeric_limits<unsigned int>::max() << endl;
  cout << "___min: " << numeric_limits<unsigned int>::min() << endl;  
  
  cout << "long : " << sizeof(long) << "bytes" << endl;
  cout << "___max: " << numeric_limits<long>::max() << endl;
  cout << "___min: " << numeric_limits<long>::min() << endl;


  cout << "long long: " << sizeof(long long int) << "bytes" << endl;
  cout << "___max: " << numeric_limits<long long int>::max() << endl;
  cout << "___min: " << numeric_limits<long long int>::min() << endl;

  cout << "int64_t: " << sizeof(int64_t) << "bytes" << endl; 
  cout << "___max: " << numeric_limits<int64_t>::max() << endl;
  cout << "___min: " << numeric_limits<int64_t>::min() << endl;
int: 4bytes
___max: 2147483647
___min: -2147483648
unsigned int: 4bytes
___max: 4294967295
___min: 0
long : 8bytes
___max: 9223372036854775807
___min: -9223372036854775808
long long: 8bytes
___max: 9223372036854775807
___min: -9223372036854775808
int64_t: 8bytes
___max: 9223372036854775807
___min: -9223372036854775808

参考サイト