結果
| 問題 |
No.685 Logical Operations
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-12-17 19:54:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,213 bytes |
| コンパイル時間 | 1,229 ms |
| コンパイル使用メモリ | 97,424 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-25 07:37:12 |
| 合計ジャッジ時間 | 2,166 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
#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 calc(int x, int y, int state) {
int nstate = state;
if(nstate == 1) { nstate = cmp(x, y); }
return nstate;
}
i64 f(i64 x) {
string s = to_bin(x);
int n = static_cast<int>(s.size());
// dp[xがyより未満/丁度/超過][yがNより未満/丁度/超過][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=0; i<n; ++i) { // 上から
vector<vector<vector<vector<i64>>>> ndp(3, vector<vector<vector<i64>>>(3, vector<vector<i64>>(3, vector<i64>(3, 0))));
for(int state0=0; state0<3; ++state0) {
for(int state1=0; state1<3; ++state1) {
for(int state2=0; state2<3; ++state2) {
for(int state3=0; state3<3; ++state3) {
if(!dp[state0][state1][state2][state3]) { continue; }
for(int xi=0; xi<2; ++xi) {
for(int yi=0; yi<2; ++yi) {
int nstate0 = calc(xi, yi, state0),
nstate1 = calc(yi, s[i]-'0', state1),
nstate2 = calc(xi & yi, xi ^ yi, state2),
nstate3 = calc(xi ^ yi, xi | yi, state3);
ndp[nstate0][nstate1][nstate2][nstate3] += dp[state0][state1][state2][state3];
ndp[nstate0][nstate1][nstate2][nstate3] %= mod;
}
}
}
}
}
}
dp = ndp;
}
i64 res = 0;
for(int state0=0; state0<2; ++state0) {
for(int state1=0; state1<2; ++state1) {
res += dp[state0][state1][0][0];
res %= mod;
}
}
return res;
}
int main(void) {
i64 N; scanf("%ld", &N);
i64 res = f(N);
printf("%ld\n", res);
return 0;
}