CDOJ 1041 Hug the princess 【思维】
来源:互联网 发布:大数据云气象阅读答案 编辑:程序博客网 时间:2024/05/17 21:48
题目链接:CDOJ 1041 Hug the princess
题意:求解
思路:a ^ b + a | b + a & b = a + b + a | b - a & b。先统计a + b的贡献,而a | b + a & b的贡献则是每个二进制位上1的个数,最后累加一下就好了。
AC代码:
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <queue>#include <set>#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se secondusing namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e5 +10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void add(LL &x, LL y) { x += y; x %= MOD; }LL a[MAXN], sum[MAXN], bit[MAXN][41], sbit[41];int main(){ int n; while(scanf("%d", &n) != EOF) { sum[0] = 0; for(int j = 0; j <= 40; j++) { bit[0][j] = 0; sbit[j] = 0; } for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = sum[i-1] + a[i]; for(int j = 0; j <= 40; j++) { bit[i][j] = 0; if(a[i] & (1LL << j)) { bit[i][j] = 1; } bit[i][j] += bit[i-1][j]; } } LL ans = 0; for(int i = 1; i < n; i++) { ans += (n - i) * a[i] + sum[n] - sum[i]; for(int j = 0; j <= 40; j++) { int num = bit[n][j] - bit[i][j]; if(a[i] & (1LL << j)) { sbit[j] += n - num - i; } else { sbit[j] += num; } } } LL res = 0; for(int i = 0; i <= 40; i++) { res += sbit[i] * (1LL << i); } printf("%lld\n", ans + res); } return 0;}
0 0
- CDOJ 1041 Hug the princess 【思维】
- CDOJ 1041 Hug the princess
- UESTC--1041--Hug the princess(位运算)
- UESTC - 1041 Hug the princess(位运算&转换)好题
- The 13th UESTC Programming Contest Preliminary——Hug the princess
- The lonely hug shirt
- CDOJ 1045 Lovely princess
- cdoj-1423【思维&&排列组合】
- cdoj-1523【思维】
- 【HDU】1029 - Ignatius and the Princess IV(STL || 思维)
- [思维题] Ignatius and the Princess IV HDU
- HDU--1029 Ignatius and the Princess IV (map&&思维&&memset)
- 【HUD 1029】Ignatius and the Princess IV(思维-水题)
- HDU 1029Ignatius and the Princess IV(基础dp(思维))
- CDOJ 1195 Find the Treasure
- CDOJ 1195 Find the Treasure
- AGU13-Save The Princess
- Ignatius and the Princess
- Android 判断网络连接状态--实现微信在线/离线状态切换
- bzoj 1419: Red is good
- C# + WinForm + EmguCV 学习二:创建和操作图片
- c++作业5
- 1041
- CDOJ 1041 Hug the princess 【思维】
- <<程序员面试宝典>>读书笔记 5
- PHP的垃圾回收机制
- Android M版本和非M版本动态权限适配方案---续篇
- 自定义控件——可拖拽排序的ListView
- STL:序列式容器vector总结
- Intellj IDEA显示行号
- C#目录操作 Path类与Directory类
- qt5.0串口写数据返回为-1的问题