题解:P17079 Lab
题目解析
定义第
贪心,拆位去做,从大到小枚举二进制位
然后发现我们还有一个目标,让前面的数字都清零。这会对
为什么要异或值尽量小呢?因为根据上面拆位让每一个数字最大的策略,
复杂度做到了
正解代码
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int A[1000005];
int s=0;
struct RandomNumberGenerator {
private:
unsigned long long state;
static constexpr unsigned long long a = 6364136223846793005ULL;
static constexpr unsigned long long c = 1442695040888963407ULL;
int k;
vector<unsigned long long> p;
public:
explicit RandomNumberGenerator(unsigned long long seed, int k_, const vector<unsigned long long>& p_)
: state(seed), k(k_), p(p_) {
if (k > 32) k = 32;
if (p.size() < static_cast<size_t>(k)) {
p.resize(k, 0);
}
}
unsigned long long next() {
state = state * a + c;
return state;
}
unsigned long long operator()(){
unsigned long long result = 0;
for (int i = 0; i < k; i++) {
unsigned long long rand_val = next();
if (rand_val < p[i]) {
result |= (1ULL << i);
}
}
return result;
}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n,k,seed;
cin>>n>>k>>seed;
vector<int>p(k);
for(int i=0;i<k;i++)cin>>p[i];
RandomNumberGenerator rnd(seed,k,p);
for(int i=1;i<=n;i++)A[i]=rnd();
//for(int i=1;i<=n;i++) cin>>A[i];
for(int i=2;i<=n;i++)
{
for(int j=40;j>=0;j--)
{
if(((A[i]&(1ll<<j))==0) && (A[i-1]>=(1ll<<j)))
{
A[i]|=(1ll<<j);//开 1ll 而不是 1!区分数字 1 和字母 l。
A[i-1]-=(1ll<<j);
}
if(j==0) break;
}
s^=A[i-1]; //拆位后,答案是0/1只和b[1]到b[n-1]的奇偶有关。异或不会破坏奇偶性,当然求和也可以。
}
cout<<(A[n]^(s&1));
}