当前位置:网站首页>2020ccpc Weihai J - Steins; Game (SG function, linear basis)
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
2022-07-07 09:48:00 【moyangxian】
The question :n Rubble , Each pile of stones has two colors black and white , When taking black stones, you must take the least amount ( At least one ), There is no limit to white, you can take it at will ( At least one ). The backhand can dye all the stones before the game starts , Ask the number of coloring schemes that the backhand can win .
Answer key : First, you can divide black and white into two games , The XOR sum of two games is 0 That is, the backhand wins .
White game : amount to nim game
Black game : adopt sg You can find
SG value = Minimum number of stones − ( ( Minimum number of heaps + [ The number of all stones is the same ] ) % 2 )
Can find sg The value is only related to the smallest heap , Then we can enumerate the smallest heap . Use one pre Maintain XOR prefix and ,suf Maintain XOR suffixes and , Maintain a linear basis from back to front . Because the heap we enumerated is the smallest heap , Therefore, the pile with less stones than the current enumeration must be white , We want the final total XOR value to be 0, Then it depends on whether the linear basis can satisfy XOR and as 0 Number of numbers , Scheme i.e 2 Of (s - tot) Power . Finally, multiply by a combined number . The detailed code can be seen .
sg Function table code :
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int sg[N];
int SG(priority_queue<int, vector<int>, greater<int> > q) {
bool vis[N] = {
false };
if (q.empty()) return 0;
int t = q.top();
q.pop();
vis[SG(q)] = true;
for (int i = 1; i < t; i++) {
auto tmp = q;
tmp.push(t - i);
vis[SG(tmp)] = true;
}
for (int i = 0; ; i++)
if (!vis[i]) return i;
}
int main() {
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 0; i <= 5; i++) {
for (int j = i; j <= 5; j++) {
for (int k = j; k <= 5; k++) {
for (int l = k; l <= 5; l++) {
for (int r = l; r <= 5; r++) {
while (!q.empty()) q.pop();
if (i) q.push(i), cout << i << " ";
if (j) q.push(j), cout << j << " ";
if (k) q.push(k), cout << k << " ";
if (l) q.push(l), cout << l << " ";
if (r) q.push(r), cout << r << " ";
cout << " : " << SG(q) << endl;
}
}
}
}
}
return 0;
}
Title code :
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int f[N], inv[N];
int a[N], pre[N], suf[N];
int same[N], num[N];
map<int, int> t;
struct LB {
const static int N = 64;
ll a[N + 1], tot;
LB() {
memset(a, 0, sizeof(a)), tot = 0; }
bool insert(ll x) {
for (int i = N; i >= 0; i--) {
if (x & (1ll << i)) {
if (a[i]) x ^= a[i];
else {
a[i] = x;
tot++;
break;
}
}
}
return x > 0;
}
bool count(ll x) {
for (int i = N; i >= 0; i--)
if (x & (1ll << i)) x ^= a[i];
return x == 0;
}
}LB;
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void init(int n) {
f[0] = inv[0] = 1;
for (int i = 1; i <= n; i++)
f[i] = f[i - 1] * i % mod;
inv[n] = qpow(f[n], mod - 2);
for (int i = n - 1; i >= 1; i--)
inv[i] = inv[i + 1] * (i + 1) % mod;
}
int C(int a, int b) {
if (a < b) return 0;
return f[a] * inv[b] % mod * inv[a - b] % mod;
}
signed main() {
init(N - 1);
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
t[a[i]]++;
ans ^= a[i];
}
ans = (ans == 0 ? 1 : 0);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] ^ a[i];
for (int i = n; i >= 1; i--)
suf[i] = suf[i + 1] ^ a[i];
for (int i = n; i >= 1; i--) {
same[i] = (a[i] == a[i + 1] ? same[i + 1] + 1 : 1);
num[i] = t[a[i]];
}
int head = n + 1;
for (int i = n; i >= 1; i--) {
int sum = 0;// Record the plan
int x = pre[i - 1];// Prefix exclusive or and
int y = a[i] - ((same[i] + 1) & 1);
// Currently selected same[i] The number of pieces is a[i] The stone pile is dyed black ( All stone heaps have the same number of stones )
int z = suf[i + same[i]];// Suffix XOR and
if ((x ^ y ^ z) == 0) sum = (sum + 1) % mod;// If XOR and is 0, Method number plus one
y = a[i] - (same[i] & 1);// ditto y, But the number of stones in all stone piles is different
int need = x ^ y;// Satisfy XOR and do 0 Number of numbers
if (LB.count(need)) sum = (sum + qpow(2, n - head + 1 - LB.tot)) % mod;
// If need It can be expressed by linear basis , The scheme that the linear basis can express this number is 2^(s - tot)
//s Is the number of linear basis insertions ,tot Is the number in the linear base
if ((need ^ suf[i + same[i]]) == 0) sum = (sum - 1 + mod) % mod;
// If the suffix all numbers are XOR and equal need Then the number of schemes should be reduced by one
if (same[i - 1] == 1) {
// If it is currently a[i] The last stone pile of the quantity , Set all values to a[i] Insert the number of into the linear base
for (int j = 1; j <= same[i]; j++) LB.insert(a[i]);
head = i;
}
ans = (ans + sum * C(num[i], same[i]) % mod) % mod;
// Finally, take the bus at num[i] Middle selection same[i] Number of heap methods
}
cout << ans << endl;
return 0;
}
边栏推荐
- JS逆向教程第一发
- H5 web player easyplayer How does JS realize live video real-time recording?
- Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
- CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
- Scratch crawler mysql, Django, etc
- [4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
- esp8266使用TF卡并读写数据(基于arduino)
- Esp8266 uses TF card and reads and writes data (based on Arduino)
- 进程间的通信方式
- HCIP 第一天 笔记整理
猜你喜欢

NATAPP内网穿透

【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯

Esp8266 uses TF card and reads and writes data (based on Arduino)

【原创】程序员团队管理的核心是什么?

Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
![[Frida practice]](/img/20/fc68bcf2f55b140d6754af6364896b.png)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform

JMeter JDBC batch references data as input parameters (the simplest method for the whole website)

使用BigDecimal的坑

Unity shader (basic concept)

sqlplus乱码问题,求解答
随机推荐
Information Security Experiment 3: the use of PGP email encryption software
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
细说Mysql MVCC多版本控制
EXT2 file system
Information Security Experiment 1: implementation of DES encryption algorithm
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
Vs2013 generate solutions super slow solutions
面试被问到了解哪些开发模型?看这一篇就够了
Sqlplus garbled code problem, find the solution
2020CCPC威海 J - Steins;Game (sg函数、线性基)
How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
Strategic cooperation subquery becomes the secret weapon of Octopus web browser
如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
ComputeShader
Binary tree high frequency question type
章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
flinkcdc采集oracle在snapshot阶段一直失败,这个得怎么调整啊?
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消