【Codeforces】Codeforces Round #836 (Div. 2) B. XOR = Average | 构造

203 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第5天,点击查看活动详情

【Codeforces】Codeforces Round #836 (Div. 2) B. XOR = Average | 构造

题目链接

Problem - B - Codeforces

题目

image.png

题目大意

对于给定的正整数 nn,构造一个长度为 nn 的数组,满足

a1a2a3...an=a1+a2+a3+...+a+nna_1\oplus a_2\oplus a_3\oplus...\oplus a_n=\frac{a_1+a_2+a_3+...+a+n}{n}

注意上式中除法并不是整除,不进行舍入。

思路

显然 nn 为奇数时可以直接输出 111111...1 1 1 1 1 1 ...。因为两两异或没了最后剩下一个 11,且平均数显然为 11

试图构造当 nn 为偶数时的答案。读完题之后发现样例里有一个 n=4n=4 的情况,于是开始想怎么构造 n=2n=2 时的答案。手玩无果后飞快打了个表如下:

#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
using LL=long long;
const int N=500001;
const LL mod=1000000007;
int main()
{
	for (int i=1;i<=100;++i)
		for (int j=1;j<=100;++j) 
			if ((i^j)*2==i+j) printf("%d %d\n",i,j);
	return 0;
}

打表的结果为

image.png

然后发现当我们两个数 aabb 的异或结果 ab=a+b2a\oplus b=\frac{a+b}{2} 时,我们只需要将剩下的所有位置都填充上 a+ba+b 即可。偶数个相同的数异或结果为 00

所以最终的答案为:

  • 如果 nn 是奇数,输出 nn11。(代码里输出了 nn77,其实都一样)。
  • 如果 nn 是偶数,先输出 1133,再输出 n2n-222

代码

#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <string.h>
#include <queue>
#define nnn printf("No\n")
#define yyy printf("Yes\n")
using namespace std;
using LL=long long;
const int N=500001;
const LL mod=1000000007;
//const LL mod=998244353;
struct asdf{
};
vector<int> e[N];
int n,m,k,x,y,z,tot,a[N],b[N];
char ch[N];
LL gcd(LL x,LL y) {return y==0?x:gcd(y,x%y);}

LL solve()
{
	
	scanf("%d",&n);
	if (n&1) for (int i=1;i<=n;++i) printf("7 ");
	else
	{
		printf("3 1 ");
		for (int i=3;i<=n;++i) printf("2 "); 
	}
	printf("\n");
	return 0;
}
int main()
{
	int T=1;
	scanf("%d",&T);
	while (T--) solve();

	return 0;
}