CodeForces 778B - Bitwise Formula

题意:

  选择一个 m 位的二进制数字,总分为 n 个算式的答案之和。问得到最低分和最高分分别应该取哪个二进制数字

分析:

  因为所有数字都是m位的,因为高位的权重大于地位 ,我们就从高到低考虑 ans 的每一位是取 0 还是取 1,统计该位的权重(即n个式子该位结果之和)即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n, m;
 4 map<string, int> mp;
 5 struct query{
 6     string f;
 7     int num;
 8     int a, op, b;
 9 }q[5005];
10 void init()
11 {
12     cin >> n >> m;
13     string s;
14     mp["?"] = 0;
15     for (int i = 1;  i <= n; i++)
16     {
17         cin >> s;
18         mp[s] = i;
19         cin >> s;
20         cin >> s;
21         if (s[0] >= ‘0‘ && s[0] <= ‘9‘)
22         {
23              q[i].op = 0;
24              q[i].f = s;
25         }
26         else
27         {
28             q[i].a = mp[s];
29             cin >> s;
30             if (s[0] == ‘A‘) q[i].op = 1;
31             if (s[0] == ‘O‘) q[i].op = 2;
32             if (s[0] == ‘X‘) q[i].op = 3;
33             cin >> s;
34             q[i].b = mp[s];
35         }
36     }
37 }
38 int find(int x, int p)
39 {
40     int ret = 0;
41     q[0].num = p;
42     for (int i = 1; i <= n; i++)
43     {
44         if (q[i].op == 0) q[i].num = q[i].f[x]-‘0‘;
45         else
46         {
47             int a = q[q[i].a].num;
48             int b = q[q[i].b].num;
49             if (q[i].op == 1) q[i].num = a&b;
50             if (q[i].op == 2) q[i].num = a|b;
51             if (q[i].op == 3) q[i].num = a^b;
52         }
53         ret += q[i].num;
54     }
55     return ret;
56 }
57 void solve()
58 {
59     string ans1, ans2;
60     for (int i = 0; i < m; i++)
61     {
62         int x0 = find(i, 0);
63         int x1 = find(i, 1);
64         x0 <= x1 ? ans1 += ‘0‘ : ans1 += ‘1‘;
65         x0 >= x1 ? ans2 += ‘0‘ : ans2 += ‘1‘;
66     }
67     cout << ans1 << endl << ans2 << endl;
68 }
69 int main()
70 {
71     init();
72     solve();
73 }
时间: 2024-10-28 14:21:49

CodeForces 778B - Bitwise Formula的相关文章

CF778B(round 402 div.2 E) Bitwise Formula

题意: Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consis

Codeforces Gym 100610 Problem E. Explicit Formula 水题

Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Consider 10 Boolean variables x1, x2, x3, x4, x5, x6, x7, x8, x9, and x10. Consider all pairs and triplets of distinct variables amon

Codeforces 1004F Sonya and Bitwise OR (线段树)

题目链接 https://codeforces.com/contest/1004/problem/F 题解 这种水题都不会做了怎么.. 考虑一个序列的前缀 \(\text{or}\) 值只会变化 \(O(\log W)\) 次,于是线段树维护每个区间的前缀和后缀 \(\text{or}\) 值即可. 时间复杂度 \(O(n\log n\log W)\). 代码 #include<bits/stdc++.h> #define llong long long #define mkpr make_p

[Codeforces 100633J]Ceizenpok’s formula

Description 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \(Lucas\) 是在模数 \(p\) 是质数的条件下适用的.我们来考虑 \(p\) 不是质数的条件. 我们对 \(p\) 进行唯一分解,记 \(p=p_1^{k_1}p_2^{k_2}\cdots p_q^{k_q}\) ,由于形同 \(p_i^{k_i}\) 的部分是互质的,显然我们可以用 \(CRT\)

Sonya and Bitwise OR CodeForces - 1004F (线段树)

大意: 给定序列$a$, 给定整数$x$. 两种操作(1)单点修改 (2)给定区间$[l,r]$,求有多少子区间满足位或和不少于$x$. 假设不带修改. 固定右端点, 合法区间关于左端点单调的. 可以预处理出最近的合法的左端点位置.那么每次询问答案就为$\sum\limits_{\substack{pre[i]\ge l\\ l \le i\le r}}(pre[i]-l+1)$. 用二维数点的方法处理即可. 带修改的话, 关键是要注意到或和最多改变$20$次, 线段树记录下来改变的位置, 暴力

Codeforces 788A Functions again - 贪心

Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about

Codeforces 424 C Magic Formulas

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 424C Description People in the Tomskaya region like magic formulas very much. You can see some of them below. Imagine you are given a sequ

Codeforces Round #275 (Div. 2)

链接:http://codeforces.com/contest/483 A. Counterexample time limit per test 1 second memory limit per test 256 megabytes Your friend has recently learned about coprime numbers. A pair of numbers {a,?b} is called coprime if the maximum number that divi

Codeforces 424C(异或)

Magic Formulas Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description People in the Tomskaya region like magic formulas very much. You can see some of them below. Imagine you are given a sequence of pos