题意:
给你pairOr,pairSum两个含有n个非负整数数组,问你是否能构造出一个数组满足
推论:
证明:

这样就能得到pairOr,pairAnd。
咱单独处理二进制的每一位,因为知道了,所以只要知道,就能知道。每次都带入0或1看看行不行即可。
代码:
#include<cstdio>#include<vector>#include<string>#include<iostream>using namespace std;class OrAndSum {public:string tak = "Possible", nie = "Impossible";bool check(int k, int x, vector < long long > a, vector < long long > b) {for (int i = 0; i < a.size(); ++i) {int _or = (a[i] >> k) & 1; int _and = (b[i] >> k) & 1;if (_or && !_and)x ^= 1;if (_or && _and && !x)return false;if (!_or && _and)return false;if (!_or && !_and && x)return false;} return true;}string isPossible(vector < long long > pairOr, vector < long long > pairSum) {vector < long long > &a = pairOr, &b = pairSum;for (int i = 0; i < b.size(); ++i) b[i] -= a[i];for (int i = 0; i < b.size(); ++i) if (b[i] < 0) return nie;for (int j = 0; j < 64; ++j)if ((!check(j, 0, a, b)) && (!check(j, 1, a, b)))return nie;return tak;}};
博客链接:
https://www.cnblogs.com/p0ny/p/8316226.html
例题:2021年牛客多校第8场D题
链接:https://ac.nowcoder.com/acm/contest/11259/D
AC代码:
/*
D题
标签:规律
*/
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
int b[N],c[N];
int n;
int check(int k,int x)
{
for (int i=2;i<=n;i++)
{
int _or = (b[i]>>k)&1, _and = (c[i]>>k)&1,f = 0;//第k位上,or和and的两个结果
for (int j=0;j<2;j++)//循环遍历,看能否找到x[i+1]的第k位满足等式的值,只有0和1.
if((x|j)==_or&&(x&j)==_and)//找到了,更新x[i+1]的第k位的值。
{
f=1;
x=j;
break;
}
if(!f)//没有找到,表示不存在。
return 0;
}
return 1;
}
int main ()
{
cin>>n;
for (int i=2;i<=n;i++)
cin>>b[i];
for (int i=2;i<=n;i++)
cin>>c[i],c[i]-=b[i];
int ans = 1;
for (int i=0;i<31;i++)
ans*=check(i,0)+check(i,1);
cout<<ans;
return 0;
}
