题目描述

You have an array $ a_0, a_1, \ldots, a_{n-1} $ of length $ n $ . Initially, $ a_i = 2^i $ for all $ 0 \le i \lt n $ . Note that array $ a $ is zero-indexed.

You want to reverse this array (that is, make $ a_i $ equal to $ 2^{n-1-i} $ for all $ 0 \le i \lt n $ ). To do this, you can perform the following operation no more than $ 250,000 $ times:

  • Select an integer $ i $ ( $ 0 \le i \lt n $ ) and replace $ a_i $ by $ a_i \oplus a_{(i+1)\bmod n} $ .

Here, $ \oplus $ denotes the bitwise XOR operation.

Your task is to find any sequence of operations that will result in the array $ a $ being reversed. It can be shown that under the given constraints, a solution always exists.

输入格式

The first line contains a single integer $ n $ ( $ 2 \le n \le 400 $ ) — the length of the array $ a $ .

输出格式

On the first line print one integer $ k $ ( $ 0 \le k \le 250,000 $ ) — the number of operations performed.

On the second line print $ k $ integers $ i_1,i_2,\ldots,i_k $ ( $ 0 \le i_j \lt n $ ). Here, $ i_j $ should be the integer selected on the $ j $ -th operation.

Note that you don't need to minimize the number of operations.

样例 #1

样例输入 #1

2

样例输出 #1

3
1 0 1

样例 #2

样例输入 #2

3

样例输出 #2

9
1 0 1 0 2 1 0 1 0

提示

In the notes, the elements on which the operations are performed are colored red.

In the first test case, array $ a $ will change in the following way:

$ [1,\color{red}{2}] \rightarrow [\color{red}{1},3] \rightarrow [2,\color{red}{3}] \rightarrow [2,1] $ .

In the second test case, array $ a $ will change in the following way:

$ [1,\color{red}{2},4] \rightarrow [\color{red}{1},6,4] \rightarrow [7,\color{red}{6},4] \rightarrow [\color{red}{7},2,4] \rightarrow [5,2,\color{red}{4}] \rightarrow [5,\color{red}{2},1] \rightarrow [\color{red}{5},3,1] \rightarrow [6,\color{red}{3},1] \rightarrow [\color{red}{6},2,1] \rightarrow [4,2,1] $

交换两个数,有一个经典的通过异或达到的方式。相信各位卡常高手应该都知道。

x^=y
y^=x
x^=y

简写完后就是 x^=y^=x^=y

拆成二进制后,发现每个二进制位都交换了,所以两个数可以成功交换。

所以,要实现交换,只要想办法使一个数 \(a_x^=a_y\) 就行了。

经过一波尝试,有这样一种方法:

10000
01000
00100
00010
00001

先异或一波

10000
11000
01100
00110
00011

然后把要异或上那个数移过来

10000
11000
10100
00110
00011
10000
11000
10100
10010
00011
10000
11000
10100
10010
10001

然后上面两种操作全部进行逆操作,将一切都变回去,就成功让一个数异或上另一个数,且其他数不变。

但是写完后跑了 400 时,出来的次数是 400000。大概多了一倍吧。

仔细观察上面的过程,会发现让一个数异或上另一个数用了 4 轮此操作,但是有两次操作是废的。那么如果我们把第一次操作完后反复使用。由于我们是要 \(a_x\oplus =a_y\),\(a_{x+1}\oplus=a_{y-1}\),所以可以比如说这样。

100000
110000
011000
001100
000110
000011
100000
110000
101000
100100
100010
100001
100000
010000
011000
001100
000110
100001
100000
010000
011000
010100
010010
100001
100000
010000
010000
010100
010010
100001

这样子就可以把常数除以2.

#include<cstdio>
const int N=405,M=3e5+5;
int n,m,st[M],k;
void run(int l,int r)
{
if(l>=r||l+1==r)
return;
for(int i=r-2;i>=l;i--)
st[++m]=i%n;
for(int i=l+1;i<r;i++)
st[++m]=i%n;
run(l+1,r-1);
}
void todo(int l,int r)
{
for(int i=l;i<r;i++)
st[++m]=i%n;
}
int main()
{
scanf("%d",&n);
k=n-2>>1;
todo(0,n-1);
run(0,n-1);
todo(n-1-k,n+k);
run(n-1-k,n+k);
todo(0,n-1);
run(0,n-1);
printf("%d\n",m);
for(int i=1;i<=m;i++)
printf("%d ",st[i]);
return 0;
}

[CF1748F] Circular Xor Reversal的更多相关文章

  1. 如何在Spring MVC Test中避免”Circular view path” 异常

    1. 问题的现象 比如在webConfig中定义了一个viewResolver public class WebConfig extends WebMvcConfigurerAdapter { //配 ...

  2. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  3. Circular Buffer

    From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...

  4. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. 在.NET Core中遭遇循环依赖问题"A circular dependency was detected"

    今天在将一个项目迁移至ASP.NET Core的过程中遭遇一个循环依赖问题,错误信息如下: A circular dependency was detected for the service of ...

  6. BZOJ 2115 【Wc2011】 Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  7. xor和gates的专杀脚本

    前段时间的一次样本,需要给出专杀,应急中遇到的是linux中比较常见的两个家族gates和xor. 首先是xor的专杀脚本,xor样本查杀的时候需要注意的是样本的主进程和子进程相互保护(详见之前的xo ...

  8. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  9. Xor && 线性基练习

    #include <cstdio> #include <cstring> ; ; int cnt,Ans,b,x,n; inline int Max(int x,int y) ...

  10. BC之Claris and XOR

    http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. 记一次weak_up函数绕过

    2023 蓝帽杯CTF LovePHP 因为比赛已经结束,所以复现环境是从本地进行复现,这次比赛本来排名挺靠前的,原本总排名是60多名,赛区排名30多名,本来是以为有希望进入半决赛的,但是没想到比赛结 ...

  2. CodeForces 1343E Weights Distributing

    题意 多组样例 给定\(n,m,a,b,c\),给定一个长度为\(m\)的数组\(p[]\),给定\(m\)条边,构成一个\(n\)个点\(m\)条边的无向图,\(Mike\)想要从\(a\)走到\( ...

  3. 如何在Vue3中配置国际化语言i18n

    1. 安装 vue-i18n npm i vue-i18n -S 2. 创建一个i8n的配置文件 如:i18nConfig.js // 配置 vue-i18n 实现国际化语言设置 import { c ...

  4. 月工资不到10元的内容审核专员? - ChatGPT 在内容自动审查中的应用

    内容过滤筛查是指对网络上发布或传播的文本.图片.视频等内容进行审核和监管,以防止出现违法违规.暴力色情.虚假广告.电信诈骗等现象,维护网络安全和社会秩序. 内容过滤筛查是一个亟待解决的问题,因为网络内 ...

  5. ContentPresenter使用DataTemplate

    在使用自定义样式内容时,有时也需要在自定义样式中绑定一下数据模板 可以使用ContentPresenter的ContentTemplate绑定定义好的资源 DateTemplate 用法代码如下 &l ...

  6. 「sdoi2019 - D2T2」移动金币

    对 @command_block 没有 implementation 做法的细化.理论来说可以通过,但因为我实现得较劣无法通过.:( 把金币中的空隙看作石子,就是一个阶梯 Nim 的模型(有总共 \( ...

  7. Tarjan强连通分量详解

    1.简介: 在阅读下列内容之前,请务必了解 图论相关概念 中的基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Compo ...

  8. 23集训 Day4 数论

    快速幂 定义 快速幂,是一个在 \(\Theta(\log n)\) 的时间内计算 \(a^n\) 的小技巧,而暴力的计算需要 \(\Theta(n)\) 的时间. 解释 \[\because a^{ ...

  9. Go反射终极指南:从基础到高级全方位解析

    在本文中,我们将全面深入地探讨Go语言的反射机制.从反射的基础概念.为什么需要反射,到如何在Go中实现反射,以及在高级编程场景如泛型编程和插件架构中的应用,本文为您提供一站式的学习指南. 关注[Tec ...

  10. 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(12) -- 使用代码生成工具Database2Sharp生成WPF界面代码

    1.代码生成工具Database2Sharp生成WPF界面代码 WPF应用端的基础接口,和Winform端.Vue3+ElementPlus前端一样,都是调用SqlSugar开发框架中的相关业务接口, ...