B. Chips on the Board

题解:贪心

  • 显然我们可以把题意转化为:对于任意一个\((i,j)\),我们可以花费\(a_{i,j}\)的代价占据第\(i\)行和第\(j\)列,求占据所有格子的最小代价

  • 考虑两种情况:

  1. 在每一行选一个格子
  2. 在每一列选一个格子

贪心选即可

int n, a[N], b[N];

void solve()
{
cin >> n;
int posa = -1, posb = -1;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
if (posa == -1 || a[i] < a[posa])
posa = i;
}
for (int i = 1; i <= n; ++i)
{
cin >> b[i];
if (posb == -1 || b[i] < b[posb])
posb = i;
}
int ans = INF, sum = 0;
for (int i = 1; i <= n; ++i)
sum += a[i];
sum += n * b[posb];
ans = min(ans, sum);
sum = 0;
for (int i = 1; i <= n; ++i)
sum += b[i];
sum += n * a[posa];
ans = min(ans, sum);
cout << ans << endl;
}

C. Make it Alternating

题解:组合数学

  • 显然可以将字符串\(s\)分成由连续\(1\)或\(0\)组成的几段区间,那么最小操作次数显然是区间的数量

  • 我们考虑方案数:

对于每段区间,我们必须留下一个数,假设某段区间的长度为\(len\),那么对答案的贡献为:\(ans:=ans\times len\)

那么其他没有被留下的数就要全部被删除,那么删除的顺序任意,假设被删除的数的数量为\(cnt\),那么对答案的贡献为\(ans:=ans \times cnt!\)

int n, fac[N];

void init()
{
fac[0] = 1ll;
for (int i = 1; i < N; ++i)
fac[i] = (fac[i - 1] * i) % mod;
} void solve()
{
string s;
cin >> s;
n = s.length();
s = " " + s;
int ans1 = 0, ans2 = 1;
for (int i = 1; i < n; ++i)
ans1 += (s[i] == s[i + 1]);
int l, r, f = 0;
for (int i = 1; i < n; ++i)
{
if (!f && s[i] == s[i + 1])
{
f = 1;
l = i;
}
else if (f && s[i] != s[i + 1])
{
f = 0;
r = i;
ans2 *= (r - l + 1);
ans2 %= mod;
}
else if (f && i + 1 == n)
{
f = 0;
r = n;
ans2 *= (r - l + 1);
ans2 %= mod;
}
}
if (f)
ans2 = ans2 * (n - l + 1) % mod;
ans2 = ans2 * fac[ans1] % mod;
cout << ans1 << " " << ans2 << endl;
}

D. Sum of XOR Functions

题解:按位计算贡献

  • 我们发现,对于二进制中某一位\(i\)来说,我们考虑其对答案的贡献 :
\[2^i \sum_{r = 1}^n \sum_{l=1}^r g(l,r)(r - l + 1)
\]

\(g(l,r)\)为第\(i\)位二进制在\([l,r]\)中\(1\)的个数,如果\(1\)为奇数,则\(g(l,r)=1\),否则\(g(l,r) = 0\)

  • 那么题目就转化为:对于一个\(01\)序列,固定右端点\(r\),求有多少个左端点\(l\)使得\(g(l,r)=1\)

  • 我们定义\(pre[i]\)为\([1,i]\)的前缀异或和,那么\(pre[r] \oplus pre[l-1] = 1\)就代表\(g(l,r)=1\)

  • 那么我们完全可以对前缀维护一个\(cnt\)和\(sum\)

  • 所以复杂度为 \(O(30n)\)

int n, a[N];

ll add(ll a, ll b) { return (a + b) % mod; }
ll sub(ll a, ll b) { return ((a - b) % mod + mod) % mod; } void solve()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
int ans = 0ll;
// 考虑每一位二进制对答案产生的贡献
for (int i = 0; i <= 30; ++i)
{
vector<int> vec(n + 10), pre(n + 10), cnt(2), sum(2);
int res = 0ll;
cnt[0]++; // pre[0] = 0
for (int j = 1; j <= n; ++j)
{
vec[j] = (a[j] >> i & 1);
pre[j] = (pre[j - 1] ^ vec[j]);
cnt[pre[j]]++;
sum[pre[j]] += j;
res = add(res, sub((cnt[pre[j] ^ 1] * (j) % mod), sum[pre[j] ^ 1]));
}
ans = add(ans, (1ll << i) * res % mod);
}
cout << ans << endl;
}

Educational Codeforces Round 155 (Rated for Div的更多相关文章

  1. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  2. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  3. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  4. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  5. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  6. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  9. Educational Codeforces Round 48 (Rated for Div. 2) CD题解

    Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) 题解

    Educational Codeforces Round 60 (Rated for Div. 2) 题目链接:https://codeforces.com/contest/1117 A. Best ...

随机推荐

  1. 强!70.3K star ! 推荐一款功能强大、开源、可视化的性能实时监控系统:Netdata

    在当今复杂多变的IT环境中,系统性能的实时监控与分析对于确保业务连续性.系统稳定运行以及快速故障排查至关重要.随着云计算.大数据和微服务架构的普及,对监控系统的要求也日益增高. 今天给大家推荐一款性能 ...

  2. 携手华为云WeLink,合合信息旗下名片全能王推动人脉管理数智化升级

    名片是商务场景中信息传递的重要载体.在无纸化办公日益兴盛的当下,数字名片逐渐被广大职场人士接受,成为商务交流的新方式.近期,合合信息旗下名片全能王与华为云WeLink联合研发,升级数字名片" ...

  3. HTTP三次握手

    转载:http://blog.163.com/wangzhenbo85@126/blog/static/1013632822013423502833/?suggestedreading&wum ...

  4. USB协议详解第2讲(协议核心学习要点)

    USB协议详解第2讲(协议核心学习要点) 看了这么多概念,想必大家会问"我要学会USB协议,并且会编程,我具体要学习那些有关的内容?",这一篇我们将会讲解在学习USB协议中务必要掌 ...

  5. 封装JWT - 生成 jwt 和解析 jwt

    1. ASP.NET Core 身份验证和授权验证的功能由Authentication,Authorization中间件提供 :app.UseAuthentication(),app.UseAutho ...

  6. docker安装过程 - 下载mysql

    1. 下载必要的包 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 2. 指定虚拟机去哪里安装 docker sudo ...

  7. v-model 语法糖-在父子组件传值 的简写形式

    props的变量名字 必须是  value ,this.$emit('input',数据值) 的自定义事件必须是 input : v-model 是 vue 中进行数据双向绑定的指令,在内部实际上是通 ...

  8. kotlin类和对象—>属性与字段

    1.声明属性,Kotlin 类中的属性既可以用关键字 var 声明为可变的,也可以用关键字 val 声明为只读的 class Address { var name: String = "Ho ...

  9. 四、Spring Boot集成Spring Security之认证流程

    二.概要说明 本文主要介绍登录登出业务流程,所以使用基于内存的用户名密码,暂不介绍授权相关内容,后续会详细介绍基于数据库的认证及授权 如何查看基于内存的默认用户名密码 如何配置基于内存的自定义用户名密 ...

  10. Machine Learning Week_1 Linear Algebra Review 7-12

    目录 4.7 Video: Matrix Matrix Multiplication unfamiliar words unfamiliar words in transcript 4.8 Readi ...