2026-01-04
CF
Problem - 71C - Codeforces
找可能形成的多边形边数,暴力枚举即可
时间复杂度:\(O(n\sqrt n)\)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=1e5+10;
int a[N];
int n;
bool ok(int x,int y){//判断是不是都是good
for (int i = x; i <= n;i+=y){
if(a[i]==0)
return 0;
}
return 1;
}
bool check(int x){
if(x==1||x==2)
return 0;
for (int i = 1; i <= n / x;i++){//找起始点
if(a[i]){
if(ok(i,n/x))
return 1;
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
for (int i = 1; i * i <= n;i++){
if(n%i==0){
if(check(i)){
cout << "YES\n";
return 0;
}
if(check(n/i)){
cout << "YES\n";
return 0;
}
}
}
cout << "NO\n";
}
Problem - 1031B - Codeforces(dp好题)
构造题,dp
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=1e5+10;
int dp[N][4];
int a[N], b[N];
int pre[N][4],ans[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n;i++){
cin >> a[i];
}
for (int i = 1; i < n;i++){
cin >> b[i];
}
for (int i = 0; i < 4;i++){
dp[0][i] = 1;
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 4; k++)
{
if ((j|k)==a[i]&&(j&k)==b[i]&&dp[i-1][j]){
dp[i][k] = 1;
pre[i][k]=j;//记录前驱
}
}
}
}
int en = -1;
for (int i = 0; i < 4;i++){
if(dp[n-1][i]){
en = i;
}
}
if(en==-1){
cout << "NO\n";
return 0;
}
for (int i = n-1; i >= 0;i--){
ans[i] = en;
en = pre[i][en];
}
cout << "YES\n";
for (int i = 0; i < n;i++){
cout << ans[i] << " ";
}
cout << endl;
}
Problem - 76D - Codeforces
一道很有意思的思维题
题意:
- \(A = X + Y\)
- \(B = X \oplus Y\)
- \(X\) is the smallest number among all numbers for which the first two conditions are true.
给定\(A,B\),找到满足以上三个条件的\(X,Y\)
思路:
- 首先,\(A\) 一定大于 \(B\),(异或是不进位加法)
- 并且\(A,B\) 奇偶性一定相同
所以根据这两个点判断有没有可能找到\(X,Y\)
然后,如果 \(X\) 和 \(Y\) 有一位二进制位同为 1,则加法后为 10,异或后为 0,将两者的差右移 1,得到 01。我们可以通过这种办法得到 \(X,Y\) 中均为 1 的位。因此 \((A-B)/2\) 的结果就是两数中均为 1 的位。所以,\(X\)(最小值)就找到了。
注意:\(0 \leq A, B \leq 2^{64} - 1\),所以要用unsigned long long (无符号整型)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
unsigned LL a, b,x,y;
cin >> a >> b;
if(a<b||(a-b)&1){
cout << -1 << endl;
return 0;
}
x = (a - b) / 2;
y = a - x;
cout << x << ' ' << y << endl;
}

浙公网安备 33010602011771号