結果

問題 No.685 Logical Operations
ユーザー 0w10w1
提出日時 2018-10-30 17:52:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,596 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 207,376 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-12 10:31:53
合計ジャッジ時間 3,279 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

template <typename T>
class Array : public vector<T> {
 public:
  using vector<T>::vector;
  using vector<T>::begin;
  using vector<T>::end;
  using vector<T>::push_back;
  template <typename R>
  Array<R> map(function<R(T)> f) {
    Array<R> res(end() - begin());
    for (auto it = begin(); it != end(); ++it) res[it - begin()] = f(*it);
    return res;
  }
  Array<T> select(function<bool(T)> f) {
    Array<T> res;
    for (auto it = begin(); it != end(); ++it)
      if (f(*it)) res.push_back(*it);
    return res;
  }
  template <typename R>
  R reduce(R u, function<R(R, T)> f) {
    for (auto it = begin(); it != end(); ++it) u = f(u, *it);
    return u;
  }
  template <typename U>
  Array<pair<T, U>> zip(const Array<U> &b) {
    assert(end() - begin() == b.end() - b.begin());
    Array<pair<T, U>> res;
    for (auto it = begin(); it != end(); ++it)
      res.push_back({*it, b[it - begin()]});
    return res;
  }
  Array<T> iota(T st) {
    Array<T> res(end() - begin());
    ::iota(res.begin(), res.end(), st);
    return res;
  }
  Array<T> slice(int st, size_t len) {
    auto it = (st < 0 ? end() : begin()) + st;
    return Array<T>(it, it + len);
  }
  void inspect() {
    copy(begin(), prev(end()), ostream_iterator<T>(cout, ", "));
    cout << *prev(end()) << endl;
  }
};
         
template <int mod>
struct ModInt {
  static int normal(int v) { return v < mod ? 0 <= v ? v : v + mod : v - mod; }
  int val;
  ModInt(int v = 0) : val(normal(v)) {}
  ModInt(int64_t v) : val(normal(v % mod)) {}
  ModInt operator+(const ModInt &b) const { return normal(val + b.val); }
  ModInt operator-(const ModInt &b) const { return normal(val - b.val); }
  ModInt operator*(const ModInt &b) const { return 1LL * val * b.val % mod; }
  ModInt operator/(const ModInt &oth) const {
    function<int(int, int, int, int)> modinv = [&](int a, int b, int x, int y) {
      if (b == 0) return x < 0 ? x + mod : x;
      return modinv(b, a - a / b * b, y, x - a / b * y);
    };
    return *this * modinv(oth.val, mod, 1, 0);
  }
  ModInt operator-() const { return mod - val; }
  template<int _mod>
  friend ostream& operator<<(ostream& os, ModInt<_mod> m) { return os << m.val, os; }
};

signed main() {
  ios::sync_with_stdio(false);

  int64_t N;
  cin >> N;

  string sn = bitset<64>(N).to_string();
  Array<int> asn = Array<char>(find(sn.begin(), sn.end(), '1'), sn.end())
                       .template map<int>([](char c) { return c - '0'; });

  Array<ModInt<int(1e9 + 7)>> dp((asn.size() + 1) * 2 * 2 * 2 * 2);
  dp[0] = 1;
  for (int i = 0; i < asn.size(); ++i)
    for (int lx = 0; lx < 2; ++lx)
      for (int ly = 0; ly < 2; ++ly)
        for (int l1 = 0; l1 < 2; ++l1)
          for (int l2 = 0; l2 < 2; ++l2) {
            int s = i << 4 | lx << 3 | ly << 2 | l1 << 1 | l2;
            for (int dx = 0; dx < (lx ? 2 : asn[i] + 1); ++dx)
              for (int dy = 0; dy < (ly ? 2 : dx + 1); ++dy) {
                if (!l1 & (dx & dy) > (dx ^ dy)) continue;
                if (!l2 & (dx ^ dy) > (dx | dy)) continue;
                int ns = i + 1 << 4
                       | (lx | dx < asn[i]) << 3
                       | (ly | dy < dx) << 2
                       | (l1 | (dx & dy) < (dx ^ dy)) << 1
                       | (l2 | (dx ^ dy) < (dx | dy));
                dp[ns] = dp[ns] + dp[s];
              }
          }

  ModInt<int(1e9 + 7)> ans;
  for (int lx = 0; lx < 2; ++lx)
    for (int ly = 0; ly < 2; ++ly)
      ans = ans + dp[asn.size() << 4 | lx << 3 | ly << 2 | 3];
  cout << ans << endl;

  return 0;
}
0