結果

問題 No.685 Logical Operations
ユーザー yuppe19 😺yuppe19 😺
提出日時 2018-11-15 14:57:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,162 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 95,000 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 01:16:33
合計ジャッジ時間 2,675 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bitset>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
using i64 = int64_t;

constexpr int mod = static_cast<int>(powl(10, 9)) + 7;

string to_bin(const i64 a) {
  if(a == 0) { return "0"; }
  string s = bitset<64>(a).to_string();
  int p = static_cast<int>(s.find('1'));
  return s.substr(p);
}

int cmp(int x, int y) {
  if(x <  y) { return 0; }
  if(x == y) { return 1; }
  return 2;
}

int mycmp(int x, int y, int val) {
  int res = cmp(x, y);
  if(res == 1) { res = val; }
  return res;
}

i64 f(i64 x) {
  string s = to_bin(x);
  int n = static_cast<int>(s.size());
  // dp[yがNより未満/丁度/超過][xがyより未満/丁度/超過][ANDがXORより未満/丁度/超過][XORがORより未満/丁度/超過] := パターン数
  vector<vector<vector<vector<i64>>>> dp(3, vector<vector<vector<i64>>>(3, vector<vector<i64>>(3, vector<i64>(3, 0))));
  dp[1][1][1][1] = 1;
  for(int i=n-1; i>=0; --i) {
    vector<vector<vector<vector<i64>>>> ndp(3, vector<vector<vector<i64>>>(3, vector<vector<i64>>(3, vector<i64>(3, 0))));
    for(int flag0=0; flag0<3; ++flag0) {
      for(int flag1=0; flag1<3; ++flag1) {
        for(int and_xor=0; and_xor<3; ++and_xor) {
          for(int xor_or=0; xor_or<3; ++xor_or) {
            if(!dp[flag0][flag1][and_xor][xor_or]) { continue; }
            for(int yi=0; yi<2; ++yi) {
              int nflag0 = mycmp(yi, s[i]-'0', flag0);
              for(int xi=0; xi<2; ++xi) {
                int nflag1 = mycmp(xi, yi, flag1),
                    n_and_xor = mycmp(xi & yi, xi ^ yi, and_xor),
                    n_xor_or  = mycmp(xi ^ yi, xi | yi, xor_or);
                ndp[nflag0][nflag1][n_and_xor][n_xor_or] += dp[flag0][flag1][and_xor][xor_or];
                ndp[nflag0][nflag1][n_and_xor][n_xor_or] %= mod;
              }
            }
          }
        }
      }
    }
    dp = ndp;
  }
  i64 res = 0;
  for(int flag0=0; flag0<2; ++flag0) {
    for(int flag1=0; flag1<2; ++flag1) {
      res += dp[flag0][flag1][0][0];
      res %= mod;
    }
  }
  return res;
}

int main(void) {
  i64 x; scanf("%ld", &x);
  i64 res = f(x);
  printf("%ld\n", res);
  return 0;
}
0