当前位置:网站首页>牛客月赛50 D 生日(计算贡献)
牛客月赛50 D 生日(计算贡献)
2022-06-10 09:28:00 【eva_can(not)survive】
登录—专业IT笔试面试备考平台_牛客网牛客网是互联网求职神器,C++、Java、前端、产品、运营技能学习/备考/求职题库,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力
https://ac.nowcoder.com/acm/contest/11227/D这是我第一次接触计算贡献答案的题目,教育意义很好。
首先我们来思考一下该题目有哪些人能够相互造成影响从而贡献出当天生日的属性异或值,很明显就只有第i天,第2*i天和第2*i+1天,最多3天能相互影响。
那么在第i天就有以下几种组合能够做出贡献
所以会产生 (a[i] ^ a[2 * i]) + (a[i] ^ a[2 * i + 1]) + (a[2 * i] ^ a[2 * i + 1]) + (a[i] ^ a[2 * i] ^ a[2 * i + 1]) 的贡献值,但显然这只是一个方案中其中一天的贡献值,还有其他的天数,所以我们考虑在这种情况下其他天数选取的可能,有2的n-3次方那么多,乘上后即为该天对答案的贡献
当然我们还需要考虑一些特况,比如说上面的就是2i+1≤n的时候,当2i+1>n但2i<=n时,我们在第i天的组合明显就只有a[i]^a[2i+1]了,然后再乘上2的n-2次方即为最后对答案的贡献。
最后如果2*i也大于n了那么明显后面的天都不会对答案有贡献,所以直接break即可。
int n;
ll a[MAXN];
const int mod = 1e9 + 7;
ll q_pow(ll a, ll b) {
ll rec = 1;
while (b) {
if (b & 1)
rec = rec * a % mod;
a = a * a % mod;
b >>= 1;
}
return rec;
}
int main() {
// int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
ll base1 = q_pow(2, n - 3);
ll base2 = q_pow(2, n - 2);
// printf("%lld %lld\n", base1, base2);
ll ans = 0;
for (int i = 1; i <= n; i++) {
if (2 * i + 1 <= n) {
ans = (ans + base1 * ((a[i] ^ a[2 * i]) + (a[i] ^ a[2 * i + 1]) + (a[2 * i] ^ a[2 * i + 1]) +
(a[i] ^ a[2 * i] ^ a[2 * i + 1]))
% mod) % mod;
} else if (2 * i <= n) {
ans = (ans + base2 * (a[i] ^ a[2 * i]) % mod) % mod;
} else
break;
}
printf("%lld", ans);
return 0;
}
边栏推荐
- printk学习之(二):调试
- Local browser access remote server jupyter configuration
- How to Understand Your Data With Descriptive Statistics
- How to Spot-Check Classification Algorithms
- Leetcode level 19 - valid parentheses
- 谈谈数字化转型方法|正确认识数字化转型
- Latex basic grammar notes
- Linear Regression
- 工业数字化转型中的数据治理
- vscode-markdown all in one-keyboard shortcut
猜你喜欢

【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具

Alignment HR_ MySQL storage engine, so it is

C语言define变参__VA_ARGS__及##__VA_ARGS__的使用

Level 18 of leetcode Langya list - sum of two numbers (lookup table method)

Share the 2022 China supply chain digital upgrading industry research report (PDF attached)

从0开始搭建生物信息学R环境(踩坑记录)

阿裏巴巴數字化轉型的啟示

接触式IC卡 - STM32(Smart Card)

From zero to one, one-stop solution to MySQL under linux environment (download)

How to Understand Your Data With Visualization
随机推荐
【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具
技能树评测
Solr advanced query application - query by field group
C语言一维数组名究竟是什么
阿里巴巴数字化转型的启示
No module named ‘pyautogui‘
MainActivity
QQ微信实现连续发送消息【代码实现】
Redis的配置优化
Oracle dual 表生成多行伪记录
【黑马早报】蚂蚁集团暂无启动IPO计划;薇娅老公成立新直播公司;微博CEO质问顺丰;京东将试点餐饮外卖业务;腾讯人才管理体系大调整...
SOLR Advanced Query Application - - Grouper les requêtes par champ
本地浏览器访问远程服务器jupyter配置
pycocotools在线安装--【可用】
How to Understand Your Data With Visualization
悬赏任务源码开发设计构建时,要留意哪些事项
FormBuilder
分享|2022年中国供应链数字化升级行业研究报告(附PDF)
Task03: more complex query (2)
关于函数声明的思考