結果

問題 No.474 色塗り2
ユーザー koba-e964koba-e964
提出日時 2016-12-24 13:42:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 52,508 KB
実行使用メモリ 20,072 KB
最終ジャッジ日時 2023-08-21 07:52:51
合計ジャッジ時間 1,016 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
19,760 KB
testcase_01 AC 10 ms
20,072 KB
testcase_02 AC 10 ms
19,776 KB
testcase_03 AC 62 ms
19,828 KB
testcase_04 AC 11 ms
19,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <iostream>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
const int mod = 1 << 21;
int residual[mod];
int pow2[mod];

int invmod(int x) {
  int e = mod / 2 - 1;
  int sum = 1;
  int cur = x;
  while (e > 0) {
    if (e % 2 == 1) {
      sum = sum * cur;
    }
    cur = cur * cur;
    e /= 2;
  }
  return sum & (mod - 1);
}


void ll_precompute(void) {
  residual[0] = 1;
  pow2[0] = 0;
  REP(i, 1, mod) {
    int r = i;
    int p = 0;
    while (r % 2 == 0) {
      r /= 2;
      p++;
    }
    residual[i] = residual[i - 1] * r;
    pow2[i] = pow2[i - 1] + p;
  }
}

ll h_partial(int x, int y) {
  if (x + y - 1 >= mod) {
    return 0;
  }
  if (y == 0) {
    return 0;
  }
  int rem = 1;
  int p2 = pow2[x + y - 1] - pow2[x] - pow2[y - 1];
  assert (p2 >= 0);
  rem = (rem * residual[x + y - 1]) & (mod - 1);
  rem = rem * invmod((residual[x] * residual[y - 1]) & (mod - 1)) & (mod - 1);
  while (p2 > 0) {
    p2--;
    rem = rem * 2;
  }
  return rem & (mod - 1);
}

int solve(int a, int b, int c) {
  if (c % 2 == 0) {
    return 0;
  }
  // H(a, c * H(b, c)) mod 2
  int rem = 1;
  // Computes H(b, c)
  rem = h_partial(b, c);
  rem = (rem * c) & (mod - 1);
  rem = h_partial(a, rem);
  return rem % 2;
}

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  ll_precompute();
  while (t--) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << solve(a, b, c) << "\n";
  }
}
0