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. UC_Center整合单点登录后远程注册不激活问题的解决办法

    修改:bbs目录\uc_server\model\user.php 下方法add_user 如下: function add_user($username, $password, $email, $u ...

  2. DECL: 针对噪声时间序列的去噪感知对比学习《Denoising-Aware Contrastive Learning for Noisy Time Series》(时间序列、对比学习、去噪)

    今天是2024年9月12日,组会摸鱼,很久没看论文了,在摸鱼看代码,最近IJCAI 2024出来了,找了几篇论文看,首先这是第一篇. 论文:Denoising-Aware Contrastive Le ...

  3. CTC联结时间分类算法: 连接主义时间分类: 用递归神经网络标记未分割序列数据《Connectionist Temporal Classification: Labelling Unsegmented Sequence Data with Recurrent Neural Networks》(语音、文本识别、RNN端到端训练序列数据的方法)

    11月6号例会汇报. 糊弄的看了一个算法-CTC算法,没有汇报论文,因为没看论文(我导知道后,应该会锤死我...) 当然,汇报完之后,被我导腾讯会议通过网线批了我一顿,PPT做的太烂了!字太多,听不到 ...

  4. Angular 18+ 高级教程 – Reactive Forms

    前言 上一篇的 Ajax 和这一篇的表单 (Form) 都是前端最最最常见的需求. 为此,Angular 分别提供了两个小型库来帮助开发者实现这些需求: Ajax – HttpClient Form ...

  5. Vs Code, Visual Studio 2022, Angular and Live Server Running Through Https and IP Address

    前言 之前就写过 angular cli, vs code liveserver, vs 2019 iis express 10, vs code kestrel 使用 https + ip. 但写的 ...

  6. CSS – BEM (Block__Element--Modifier)

    前言 BEM 是一种 CSS class 命名规范. 目的是防止大家名字取太短, 不小撞名字后果很麻烦. 参考: Youtube – You Probably Need BEM CSS in Your ...

  7. 解密Prompt系列38.多Agent路由策略

    常见的多智能体框架有几类,有智能体相互沟通配合一起完成任务的例如ChatDev,CAMEL等协作模式, 还有就是一个智能体负责一类任务,通过选择最合适的智能体来完成任务的路由模式,当然还有一些多智能体 ...

  8. C++ char*类型与vector类型的相互转换

    char*类型与vector<char> 类型的相互转换 很多时候需要使用动态的字符串,但是char*难以完成相应的扩容操作,而动态数组vector则可以简单地完成,结合二者特性就可以完成 ...

  9. Java中使用BigDecimal进行double类型的计算(高精度,可保留几位小数)

    Java中 小数直接进行乘除运算,会出现精度问题导致计算结果有误需要使用 BigDecimal 类型辅助运算,保证精度无误源码: import java.math.BigDecimal;import ...

  10. 2024年7月中国数据库排行榜:PolarDB独领云风骚,达梦跨越新巅峰

    在7月发布的中国数据库流行度排行榜中,各大国产数据库厂商在不同领域表现势如破竹,PolarDB以800分刷新记录,并在SIGMOD 2024上获得"最佳论文奖":OceanBase ...