ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)
2016-04-10 20:17
423 查看
题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5
这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥。
不过直接上去是T,考虑到序列中同时存在i和ki的话,其实只需要考虑i,所以先对序列中为倍数的对进行处理。
这里的容斥用了hqw的写法。
代码:
View Code
这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对lcm进行容斥。
不过直接上去是T,考虑到序列中同时存在i和ki的话,其实只需要考虑i,所以先对序列中为倍数的对进行处理。
这里的容斥用了hqw的写法。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include <set> #include <map> #include <queue> #include <vector> #include <string> #define LL long long using namespace std; const LL maxN = 1e9; int n; int t[60], a[60], len; LL ans; void input() { scanf("%d", &n); len = n; for (int i = 0; i < len; ++i) scanf("%d", &t[i]); for (int i = 0; i < len; ++i) { if (t[i] == -1) continue; for (int j = 0; j < len; ++j) { if (i == j) continue; if (t[j] == -1) continue; if (t[j]%t[i] == 0) t[j] = -1; } } int top = 0; for (int i = 0; i < len; ++i) if (t[i] != -1) a[top++] = t[i]; len = top; sort(a, a+len); } LL gcd(LL x, LL y) { LL r; while (y != 0) { r = y; y = x%y; x = r; } return x; } LL lcm(LL x, LL y) { return x/gcd(x, y)*y; } void dfs(int now, LL num, int sz) { if (now == len) { if (sz) { LL last = maxN/num; if (sz&1) ans -= last; else ans += last; } return; } if (num%a[now] == 0) return; dfs(now+1, num, sz); LL t = lcm(num, a[now]); if (t <= maxN) dfs(now+1, t, sz+1); } void work() { if (len == 1 && a[0] == 1) printf("0\n"); else { ans = maxN; dfs(0, 1, 0); cout << ans << endl; } } int main() { //freopen("test.in", "r", stdin); int T; scanf("%d", &T); for (int times = 1; times <= T; ++times) { input(); work(); } return 0; }
View Code
相关文章推荐
- 2.TCP/IP协议族
- ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)
- 【Android基础】网络图片查看器
- HDU 4975 A simple Gaussian elimination problem网络流
- 关于编写网络技术文档的基本步骤
- ACM学习历程—广东工业大学2016校赛决赛-网络赛C wintermelon的魔界寻路之旅(最短路 && 递推)
- 采用TCP协议的PIC32MZ ethernet bootloader
- HTTP协议格式
- HTTP报文结构
- HTTP 411 Content-Length required
- 编写新闻客户端&网络编程day1(66期第四天)
- ios解决http上传乱码问题
- Http请求头探讨
- 神经网络指南Hacker's guide to Neural Networks
- BP神经网络原理及编程实现
- 卷积神经网络Convolutional Neural Networks
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
- TCP漏洞:半连接
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
- TCP协议三次握手连接四次握手断开和DOS攻击