EVERYTHING HAPPENS FOR THE |

wnsyou

园龄:2年4个月粉丝:19关注:16

2023-05-24 00:14阅读: 56评论: 0推荐: 0

abc271_f XOR on Grid Path 题解

XOR on Grid Path

题意

有一个 n×n 的整数矩阵,第 ij 列的数字为 ai,j

你站在 (1,1),每次你可以从 (x,y) 走到 (x,y+1)(x+1,y),你要走到 (n,n),求有多少条路径使得路径上的每个数异或和为 0

数据范围

  • 2n20
  • 0ai,j230(1i,jn)

思路

折半搜索。

折半搜索,顾名思义就是把一条搜索路径分成两条,每条路径先各自考虑,最后将两条路径合起来考虑。
但折半搜索不一定是折半,在某些特定情况下分界点可以不同。

这个题不是特殊题,分界点就在正中间。

不难发现,走 n1 步可以走到的点 (x,y) 要满足 x+y=n+1,首先枚举分界点。

折半搜索,两边分别记录一下各自会出现哪些异或和(假设用 v 数组和 g 数组存储),统计满足如下要求的整数对数即可:

  • i|v|,j|g|
  • vi=gj

经过计算,我们发现要开 long long 存储。

复杂度

  • 时间:O(n×2n+n2)
  • 空间:O(2n+n2)

Code

点击查看代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int n, a[30][30];
vector<int> v, g;
ll ans;
void dfs (int x, int y, int z) { // 前半条路径
if (x == 0 || y == 0) {
return ;
}
z ^= a[x][y];
if (x + y == 2) {
v.push_back(z);
return ;
}
dfs(x - 1, y, z), dfs(x, y - 1, z);
}
void dfs2 (int x, int y, int z) { // 后半条路径
if (x > n || y > n) {
return ;
}
z ^= a[x][y];
if (x + y == 2 * n) {
g.push_back(z);
return ;
}
dfs2(x + 1, y, z), dfs2(x, y + 1, z);
}
int main () {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
vector<int> ().swap(v);
vector<int> ().swap(g);// 清空
dfs(i, n + 1 - i, 0);
dfs2(i, n + 1 - i, a[i][n + 1 - i]); // 当前这个点会被算两次,怎么能行,先处理一下
sort(v.begin(), v.end()), sort(g.begin(), g.end());
for (int j : v) {
int l = lower_bound(g.begin(), g.end(), j) - g.begin(), r = upper_bound(g.begin(), g.end(), j) - g.begin(); // 二分求满足要求的整数对数
ans += r - l;
}
}
cout << ans;
return 0;
}

本文作者:wnsyou の blog

本文链接:https://www.cnblogs.com/wnsyou-blog/p/17426799.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wnsyou  阅读(56)  评论(0编辑  收藏  举报
  1. 1 勝利への道 安藤浩和
  2. 2 Minecraft’s End Eric Fullerton
  3. 3 月光曲完整版 贝多芬 云熙音乐
  4. 4 平凡之路 (Live版) 朴树
  5. 5 Minecraft C418
  6. 6 Paradise NiziU
  7. 7 叫我,灰原哀 龙大人不喷火
  8. 8 心机之蛙,一直摸你肚子 ——《名侦探柯南》原创同人曲 炊饭,叶辞樱,温海,寒砧,南柯柯,小茜玛姬,盛姝,阿崔Ac,贝壳初,千湛,兮茶子DaYu,乔慕,黎鹿北,起千温卿,遮阳伞,曲悠
  9. 9 战 歌 此去经年
Minecraft’s End - Eric Fullerton
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

I see the player you mean.

It is reading our thoughts as though they were words on a screen.

They used to hear voices.

Before players could read.

Sometimes disturbing.

Sometimes beautiful indeed.

Does it know that we love it?

That the universe is kind?

A million years ago,it still works

in the reality behind

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Once we were called

the spirit of the mountain.

WHO ARE WE

Father sun

Mother moon

Gods demons angels aliens

the player,too.

WE ARE THE UNIVERSE.

We are EVERYTHING you think isn‘t you.

You are alive

ON A FLAT

INFINITE WORLD

generated by a source code

a million years old

and the universe said I love you

and the universe said you are the daylight

and the universe said you are not alone

and the universe said you are the night

Take a breath,now

Take another

Feel air in your lungs.

dreamed it was lost in a story.

and the game was over

Wake up.

加载中…

{{tag.name}}

{{tran.text}}{{tran.sub}}
无对应文字
有可能是
{{input}}
尚未录入,我来提交对应文字
评论
收藏
关注
推荐
深色
回顶
收起
点击右上角即可分享
微信分享提示