結果

問題 No.1493 隣接xor
ユーザー startcppstartcpp
提出日時 2021-05-01 01:47:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,439 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 78,968 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-26 10:43:15
合計ジャッジ時間 4,758 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 127 ms
8,744 KB
testcase_04 AC 127 ms
8,748 KB
testcase_05 AC 127 ms
8,916 KB
testcase_06 AC 127 ms
8,784 KB
testcase_07 AC 127 ms
8,792 KB
testcase_08 AC 127 ms
8,904 KB
testcase_09 AC 127 ms
8,720 KB
testcase_10 AC 128 ms
8,864 KB
testcase_11 AC 127 ms
8,880 KB
testcase_12 AC 128 ms
8,728 KB
testcase_13 AC 94 ms
8,740 KB
testcase_14 AC 43 ms
8,888 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 65 ms
6,108 KB
testcase_21 AC 74 ms
6,448 KB
testcase_22 AC 52 ms
5,540 KB
testcase_23 AC 66 ms
6,172 KB
testcase_24 AC 122 ms
8,496 KB
testcase_25 AC 51 ms
5,672 KB
testcase_26 AC 79 ms
6,616 KB
testcase_27 AC 33 ms
4,656 KB
testcase_28 AC 114 ms
8,260 KB
testcase_29 AC 82 ms
6,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//累積xorを取ると、部分列は何種類?に帰着される。普通にDP考えるとO(N^2 logN)なのだが、どう高速化しようか。
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

void press(vector<int> &a) {
	int i;
	vector<int> sa;
	rep(i, a.size()) sa.push_back(a[i]);
	sort(sa.begin(), sa.end());
	sa.erase(unique(sa.begin(), sa.end()), sa.end());
	rep(i, a.size()) a[i] = lower_bound(sa.begin(), sa.end(), a[i]) - sa.begin();
}

int count_subarray(vector<int> a, int mod) {
	int i, j;
	
	int n = a.size();
	vector<int> dp(n + 1);		//dp[i] = (a[0],…,a[i-1])の部分列は何種類あるか
	vector<int> last(n + 1);	//last[num]:値numに関する最新のdp値.
								//より正確には、dp[i]を計算する時点で、dp[j] = num (j < i)なる最大のjにおけるdp[j]を持つ.
	int lastSum = 0;
	
	press(a);
	
	dp[0] = 1;
	for (i = 1; i <= n; i++) {
		lastSum += (dp[i - 1] - last[a[i - 1]] + mod) % mod;
		lastSum %= mod;
		last[a[i - 1]] = dp[i - 1];
		dp[i] = lastSum + 1;
		dp[i] %= mod;
	}
	
	return dp[n];
}

int main() {
	int n, i;
	
	cin >> n;
	vector<int> a(n);
	rep(i, n) cin >> a[i];
	
	vector<int> ra(n + 1);
	ra[0] = 0; rep(i, n) ra[i + 1] = ra[i] ^ a[i];
	
	vector<int> rb(n - 1);
	rep(i, n - 1) rb[i] = ra[i + 1];
	
	int mod = 1000000007;
	int ans = count_subarray(rb, mod);
	cout << ans << endl;
	return 0;
}
0