跳到正文

个人记录

2026/3/20

liuyilun123约 5 分钟
原文
文章信息与版本

文章信息

收录
2026年3月22日
更新
2026年3月22日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

P7071 [CSP-J 2020] 优秀的拆分

错误原因:

算是偏分。

思路:

  1. 因为是二的次幂加起来所以不可能是奇数 ,所以如果 nn 是奇数就直接输出 −1-1 。

  2. 从 6060 开始循环 (其实只要 3030), 一直到一 。每次检查如果 nn 大于等于 2i2^i , 就减去并输出 。

代码:

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

int n;

signed main()
{
	cin >> n;

	if(n % 2 == 1)
	{
		cout << "-1";
		return 0;
	}

	for(int i = 60; i >= 1; i--)
	{
		if(n >= (1ll << i))
		{
			n -= (1ll << i);
			cout << (1ll << i) << " ";
		}
	}
	return 0;
}

P1461 [USACO2.1] 海明码 Hamming Codes

错误原因:

没有思路 。

思路:

  1. 我们可以循环从 11 ~ 2b2^b- 11 , 每次判断 ii 和前面编码中的 Hamming 距离 是否大于等于 dd 。

  2. 如果是就把 ii 也加入编码 ,当编码的数量等于 nn 时就可以跳出循环了。最后输出。(00 也要加入编码)。

代码:

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

int n, b, d, cnt, a[105];

bool hanshu(int a, int b)
{
	int cnt = 0;
	for(int i = 60; i >= 0; i--)
	{
		int t1 = (a >> i) & 1;
		int t2 = (b >> i) & 1;

		if(t1 != t2)
		{
			cnt++;
		}
	}

	return cnt >= d;
}

signed main()
{
	cin >> n >> b >> d;

	a[++cnt] = 0;

	int o = pow(2, b) - 1;
	for(int i = 1; i <= o; i++)
	{
		bool v = 0;
		for(int j = 1; j <= cnt; j++)
		{
			if(hanshu(i, a[j]) == 0)
			{
				v = 1;
				break;
			}
		}

		if(v == 0)
		{
			a[++cnt] = i;
		}

		if(cnt == n)
		{
			break;
		}
	}

	for(int i = 1; i <= n; i++)
	{
		cout << a[i] << " ";
		if(i % 10 == 0)
		{
			cout << "\n";
		}
	}
	return 0;
}

P11485 「Cfz Round 5」Non-breath Oblige

错误原因:

没思路 , 偏分。

思路:

  1. 首先先判断 ss 是不是等于 tt, 如果是就直接输出 00 。否则就从 nn - 11 循环到 00 , 如果 ss 右移 ii 位 与上 11 等于 00 , 并且 tt 右移 ii 位 与上 11 等于 11 , 代价就加上 11 右移 ii 位 。

  2. 否则如果 ss 右移 ii 位 与上 11 等于 11 , 并且 tt 右移 ii 位 与上 11 等于 00 , 代价也加上 11 右移 ii 位 。

  3. 否则如果 ss 右移 ii 位 与上 11 等于 00 , 并且 tt 右移 ii 位 与上 11 等于 00 , 代价也加上 11 右移 ii 位 乘以二 (因为要转两次) 。

代码:

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

int q, n, s, t;

void solve()
{
	cin >> n >> s >> t;

	if(s == t)
	{
		cout << 0 << "\n";
		return ;
	}

	int cnt = 0;
	for(int i = n - 1; i >= 0; i--)
	{
		if(((s >> i) & 1) == 0 && ((t >> i) & 1) == 1)
		{
			cnt += (1ll << i);
		}
		else if(((s >> i) & 1) == 1 && ((t >> i) & 1) == 0)
		{
			cnt += (1ll << i);
		}
		else if(((s >> i) & 1) == 0 && ((t >> i) & 1) == 0)
		{
			cnt += (1ll << i) * 2;
		}
	}

	cout << cnt << "\n";
}

signed main()
{
	cin >> q;
	while(q--)
	{
		solve();
	}
	return 0;
}
原文