2026/3/20
liuyilun123 · · 个人记录
P7071 [CSP-J 2020] 优秀的拆分
错误原因:
算是偏分。
思路:
-
因为是二的次幂加起来所以不可能是奇数 ,所以如果
n 是奇数就直接输出-1 。 -
从
60 开始循环 (其实只要30 ), 一直到一 。每次检查如果n 大于等于2^i , 就减去并输出 。代码:
#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. ### 我们可以循环从 $1$ ~ $2^b$- $1$ , 每次判断 $i$ 和前面编码中的 **Hamming 距离** 是否大于等于 $d$ 。
2. ### 如果是就把 $i$ 也加入编码 ,当编码的数量等于 $n$ 时就可以跳出循环了。最后输出。($0$ 也要加入编码)。
## 代码:
```cpp
#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
错误原因:
没思路 , 偏分。
思路:
-
首先先判断
s 是不是等于t , 如果是就直接输出0 。否则就从n -1 循环到0 , 如果s 右移i 位 与上1 等于0 , 并且t 右移i 位 与上1 等于1 , 代价就加上1 右移i 位 。 -
否则如果
s 右移i 位 与上1 等于1 , 并且t 右移i 位 与上1 等于0 , 代价也加上1 右移i 位 。 -
否则如果
s 右移i 位 与上1 等于0 , 并且t 右移i 位 与上1 等于0 , 代价也加上1 右移i 位 乘以二 (因为要转两次) 。代码:
#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; }