【CodeChef】Strange Transform

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

【题目链接】

【思路要点】

  • 若我们将每一位分开考虑,异或可以看做模 22 意义下的加法。
  • 因此,一个位置 fk,xf_{k,x} 的值可以看做从 (k,x)(k,x) 出发,每次可以选择从 (x,y)(x,y) 走到 (x1,y),(x,y+1)(x-1,y),(x,y+1) ,最终停在 (0,x) (f0,x=1)(0,x)\ (f_{0,x}=1) 处的方案数对 22 取模。
  • 因此, fk1,xi=0k1k2fk2,x+i(k1k2i) (Mod 2)f_{k_1,x}\equiv\sum_{i=0}^{k_1-k_2}f_{k_2,x+i}*\binom{k_1-k_2}{i}\ (Mod \ 2)
  • 注意到由 LucasLucas 定理,当且仅当 aa 在二进制下数位包含 bb(ab)%2=1\binom{a}{b}\%2=1 ,上式有特殊情况: fk,xfk2i,x+fk2i,x+2i (Mod 2)f_{k,x}\equiv f_{k-2^i,x}+f_{k-2^i,x+2^i}\ (Mod \ 2)
  • 现在我们来考虑一个询问 (k,x)(k,x) ,由上面的推断,我们令 k=k&(2181)k=k\&(2^{18}-1) 不会使答案发生变化,下令 k=O(N)k=O(N)
  • kO(N)k≤O(\sqrt{N}) ,我们可以预处理出 fk,f_{k,*} 的结果,直接查询。
  • k>O(N)k>O(\sqrt{N}) ,令 bitbit 表示 kk 最高的为 11 的位置,则有 fk,x=fk2bit,xfk2bit,x+2bitf_{k,x}=f_{k-2^{bit},x}\oplus f_{k-2^{bit},x+2^{bit}} ,递归求解即可。
  • 时间复杂度 O(NN+QN)O(N\sqrt{N}+Q\sqrt{N})

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MAXM = 512;
const int goal = 511;
const int Min = 262143;
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("");
}
int n, m, type;
int a[MAXM][MAXN];
int work(int pos, int k) {
	if (k == 0) return a[type][pos];
	int tmp = k & -k, ans = 0;
	ans ^= work(pos, k ^ tmp);
	if (pos + tmp <= n) ans ^= work(pos + tmp, k ^ tmp);
	return ans;
}
int main() {
	read(n), read(m);
	for (int i = 1; i <= n; i++)
		read(a[0][i]);
	for (int i = 1; i <= goal; i++)
	for (int j = 1; j <= n; j++)
		a[i][j] = a[i - 1][j] ^ a[i - 1][j + 1];
	for (int i = 1; i <= m; i++) {
		int k, x;
		read(k), read(x);
		k &= Min, type = k & goal;
		writeln(work(x, k ^ type));
	}
	return 0;
}
阅读更多
相关热词
换一批

Strange Billboard

12-02

Problem DescriptionnThe marketing and public-relations department of the Czech Technical University has designed a new reconfigurable mechanical Flip-Flop Bill-Board (FFBB). The billboard is a regular two-dimensional grid of R ×C square tiles made of plastic. Each plastic tile is white on one side and black on the other. The idea of the billboard is that you can create various pictures by flipping individual tiles over. Such billboards will hang above all entrances to the university and will be used to display simple pictures and advertise upcoming academic events. nnTo change pictures, each billboard is equipped with a ”reconfiguration device”. The device is just an ordinary long wooden stick that is used to tap the tiles. if you tap a tile, it flips over to the other side, i.e., it changes from white to black or vice versa. Do you agree this idea is very clever? nnUnfortunately, the billboard makers did not realize one thing. The tiles are very close to each other and their sides touch. Whenever a tile is tapped, it takes all neighboring tiles with it and all of them flip over together. Therefore, if you want to change the color of a tile, all neighboring tiles change their color too. Neighboring tiles are those that touch each other with the whole side. All inner tiles have 4 neighbors, which means 5 tiles are flipped over when tapped. Border tiles have less neighbors, of course. nn![](http://acm.hdu.edu.cn/data/images/C106-1001-1.JPG)nnnFor example, if you have the billboard configuration shown in the left picture above and tap the tile marked with the cross, you will get the picture on the right. As you can see, the billboard reconfiguration is not so easy under these conditions. Your task is to find the fastest way to ”clear” the billboard, i.e., to flip all tiles to their white side. n nnInputnThe input consists of several billboard descriptions. Each description begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 16) specifying the billboard size. Then there are R lines, each containing C characters. The characters can be either an uppercase letter “X” (black) or a dot “.” (white). There is one empty line after each map. nThe input is terminated by two zeros in place of the board size.n nnOutputnFor each billboard, print one line containing the sentence “You have to tap T tiles.”, where T is the minimal possible number of taps needed to make all squares white. if the situation cannot be solved, output the string “Damaged billboard.” instead. n nnSample Inputn5 5nXX.XXnX.X.Xn.XXX.nX.X.XnXX.XXn5 5n.XX.Xn.....n..XXXn..X.Xn..X..nn1 5n...XXnn5 5n...X.n...XXn.XX..n..X..n.....nn8 9n..XXXXX..n.X.....X.nX..X.X..XnX.......XnX.X...X.XnX..XXX..Xn.X.....X.n..XXXXX..nn0 0n nnSample OutputnYou have to tap 5 tiles.nDamaged billboard.nYou have to tap 1 tiles.nYou have to tap 2 tiles.nYou have to tap 25 tiles.

Strange Graph

07-18

DescriptionnnLet us consider an undirected graph G = < V,E >. Let us denote by N(v) the set of vertices connected to vertex v (i.e. the set of neighbours of v). Recall that the number of vertices connected to v is called the degree of this vertex and is denoted by deg v. nWe will call graph G strange if it is connected and for its every vertex v the following conditions are satisfied: n1. deg v >= 2 (i.e. there are at least two vertices connected to v) n2. If deg v = 2 then the two neighbours of v are not connected by an edge n3. If degv > 2 then there is u ∈ N(v), such that the following is true: n(a) deg u = 2 n(b) Any two different vertices w1,w2 ∈ N(v) \ u are connected, i.e. (w1,w2) ∈ E.nnYou are given some strange graph G. Find hamiltonian cycle in it, i.e. find such cycle that it goes through every vertex of G exactly once.nInputnnThe first line of the input file contains two integer numbers N and M -- the number of vertices and edges in G respectively (3 <= N <= 10 000, M <= 100 000). 2M integer numbers follow -- each pair represents vertices connected by the corresponding edge (vertices are numbered from 1 to N). It is guaranteed that each edge occurs exactly once in the input file and that there are no loops (i.e. ends of each edge are distinct).nOutputnnIf there is no hamiltonian cycle in G, print -1 on the first line of the output file. In the other case output N numbers -- the sequence of vertices of G as they appear in the hamiltonian cycle found (note that the last vertex must be connected to the first one). If there are several solutions, output any one.nSample Inputnn4 4n1 2 2 3 3 4 4 1nSample Outputnn1 2 3 4

没有更多推荐了,返回首页