【LOJ3073】「2019 集训队互测 Day 2」序列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/89762720

【题目链接】

【思路要点】

  • 不妨令 M M 2 2 的次幂。
  • 可以发现题目中的初始序列满足:序列的前一半或后一半完全相同,序列的另一半递归地满足该性质。
  • 并且,两个满足该性质的序列卷积后同样满足该性质。
  • 因此,可以用 O ( L o g M ) O(LogM) 个数描述序列,并在 O ( L o g M ) O(LogM) 的时间复杂度内完成卷积。
  • 时间复杂度 O ( N L o g M + Q L o g M ) O(NLogM+QLogM)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 7e5 + 5;
const int MAXLOG = 21;
const int P = 998244353;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct Info {
	bool mask[MAXLOG];
	int value[MAXLOG];
} a[MAXN];
void update(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}
Info operator * (const Info &a, const Info &b) {
	Info ans;
	int suma = a.value[0], sumb = b.value[0];
	for (int i = 1; i < MAXLOG; i++) {
		update(suma, 1ll * (1 << (i - 1)) * a.value[i] % P);
		update(sumb, 1ll * (1 << (i - 1)) * b.value[i] % P);
	}
	int sum = 0;
	for (int i = MAXLOG - 1; i >= 1; i--) {
		ans.mask[i] = a.mask[i] ^ b.mask[i];
		update(suma, P - 1ll * (1 << (i - 1)) * a.value[i] % P);
		update(sumb, P - 1ll * (1 << (i - 1)) * b.value[i] % P);
		ans.value[i] = (sum + 1ll * a.value[i] * sumb + 1ll * b.value[i] * suma) % P;
		update(sum, 1ll * a.value[i] * b.value[i] % P * (1 << (i - 1)) % P);
	}
	ans.value[0] = (1ll * a.value[0] * b.value[0] + sum) % P;
	return ans;
}
int query(const Info &a, int pos) {
	for (int i = MAXLOG - 1; i >= 1; i--)
		if (((pos & (1 << (i - 1))) != 0) != a.mask[i]) return a.value[i];
	return a.value[0];
}
int n, m, q;
int main() {
	read(n), read(m), read(q);
	for (int i = 1; i <= n; i++) {
		int x, y; read(x), read(y);
		for (int j = MAXLOG - 1; j >= 1; j--)
			if (x & (1 << (j - 1))) {
				a[i].mask[j] = true;
				int tx = x & ~((1 << (j - 1)) - 1);
				a[i].value[j] = 1ll * tx * (tx ^ y) % P;
			} else a[i].mask[j] = false;
	}
	for (int i = 2; i <= n; i++)
		a[i] = a[i] * a[i - 1];
	for (int i = 1; i <= q; i++) {
		int x, y; read(x), read(y);
		writeln(query(a[x], y));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/89762720