UVA 10542 - Hyper-drive(容斥原理)
来源:互联网 发布:windows更新数据库 编辑:程序博客网 时间:2024/06/15 01:04
UVA 10542 - Hyper-drive
题目链接
题意:给定一些个d维的方块,给定两点,求穿过多少方块
思路:容斥原理,每次选出一些维度,如果gcd(a, b),就会穿过多少点,对应的就减少穿过多少方块,所以最后得到式子d1 + d2 + .. dn - gcd(d1, d2)..+gcd(d1, d2, d3)...
代码:
#include <cstdio>#include <cstring>typedef long long ll;int t, d, a[15];int bitcount(int x) {int ans = 0;while (x) {ans += (x&1);x >>= 1;}return ans;}int gcd(int a, int b) {while (b) {int tmp = a % b;a = b;b = tmp;}return a;}int main() {int cas = 0;scanf("%d", &t);while (t--) {scanf("%d", &d);for (int i = 0; i < d; i++)scanf("%d", &a[i]);int tmp;for (int i = 0; i < d; i++) {scanf("%d", &tmp);a[i] -= tmp;if (a[i] < 0) a[i] = -a[i];}ll ans = 0;for (int i = 0; i < (1<<d); i++) {int cnt = bitcount(i);int sum = 0;for (int j = 0; j < d; j++) {if (i&(1<<j))sum = gcd(sum, a[j]);}if (cnt&1) ans += sum;else ans -= sum;}printf("Case %d: %lld\n", ++cas, ans);}return 0;}
1 0
- UVA 10542 - Hyper-drive(容斥原理)
- uva 10542 - Hyper-drive(容斥)
- UVA 10325 (容斥原理)
- UVA 11806 Cheerleaders(容斥原理)
- UVA 11806 容斥原理
- uva 11806 容斥原理
- uva 10325 容斥原理
- uva 11806(容斥原理)
- UVA 11806 容斥原理
- UVA 11806 Cheerleaders(容斥原理)(组合数)
- UVa 11806 Cheerleaders 解题报告(容斥原理)
- UVA 11806 - Cheerleaders(数论+容斥原理)
- UVA 12075 - Counting Triangles(容斥原理计数)
- UVa 11806 - Cheerleaders (组合数学 容斥原理)
- uva 10325 The Lottery(组合数学-容斥原理)
- UVa 11481 Arrange the Number(容斥原理)
- UVA 10325 The Lottery(容斥原理)
- UVA 11806 Cheerleaders (容斥原理+二进制枚举)
- map使用简明教程
- WINCE6.0+SLC ECC产生和校验 .
- hadoop 笔记
- 探讨分布式系统与集群的区别
- 和的发货的发货的发货的发货的合适的话多少的实施的和
- UVA 10542 - Hyper-drive(容斥原理)
- 使用Fiddler对移动设备上的HTTP/HTTPS抓包
- mysql 存储过程错误调试记录
- 【重读设计模式】工厂方法模式
- Servlet/Jsp实现发送压缩Web页面 Gzip技术
- ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
- MAC配置SVN服务器
- B. Appleman and Card Game
- LRU算法,最近最少使用算法