开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第5天,点击查看活动详情
【Codeforces】Codeforces Round #836 (Div. 2) B. XOR = Average | 构造
题目链接
题目
题目大意
对于给定的正整数 ,构造一个长度为 的数组,满足
注意上式中除法并不是整除,不进行舍入。
思路
显然 为奇数时可以直接输出 。因为两两异或没了最后剩下一个 ,且平均数显然为 。
试图构造当 为偶数时的答案。读完题之后发现样例里有一个 的情况,于是开始想怎么构造 时的答案。手玩无果后飞快打了个表如下:
#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;
}
打表的结果为
然后发现当我们两个数 和 的异或结果 时,我们只需要将剩下的所有位置都填充上 即可。偶数个相同的数异或结果为 。
所以最终的答案为:
- 如果 是奇数,输出 个 。(代码里输出了 个,其实都一样)。
- 如果 是偶数,先输出 和 ,再输出 个 。
代码
#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;
}