题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=5

这个题目一看就是一道数论题,应该考虑使用容斥原理,这里对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[], a[], len;
LL ans; void input()
{
scanf("%d", &n);
len = n;
for (int i = ; i < len; ++i)
scanf("%d", &t[i]);
for (int i = ; i < len; ++i)
{
if (t[i] == -) continue;
for (int j = ; j < len; ++j)
{
if (i == j) continue;
if (t[j] == -) continue;
if (t[j]%t[i] == ) t[j] = -;
}
}
int top = ;
for (int i = ; i < len; ++i)
if (t[i] != -)
a[top++] = t[i];
len = top;
sort(a, a+len);
} LL gcd(LL x, LL y)
{
LL r;
while (y != )
{
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&) ans -= last;
else ans += last;
}
return;
}
if (num%a[now] == ) return;
dfs(now+, num, sz);
LL t = lcm(num, a[now]);
if (t <= maxN) dfs(now+, t, sz+);
} void work()
{
if (len == && a[] == ) printf("0\n");
else
{
ans = maxN;
dfs(, , );
cout << ans << endl;
}
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
input();
work();
}
return ;
}

最新文章

  1. 前端学PHP之mysql扩展函数
  2. 注解:【无连接表的】Hibernate双向1-&gt;N关联
  3. svn没有对号等符号的问题
  4. java servlet+mysql全过程(原创)
  5. 根据OSG中的ref_ptr和Reference简化的智能指针
  6. Web Storage的方法
  7. MySQL安装过程net start mysql 启动失败 报“错误2,系统找不到文件”的解决办法
  8. CentOS yum kvm
  9. Maven手动创建多模块项目
  10. Linux 多学习过程
  11. 原生JS元素怎么取消事件
  12. 基于winpcap的以太网流量分析器(java)
  13. NYoj289苹果(0-1背包)
  14. 读Ghost博客源码与自定义Ghost博客主题
  15. 初始hibernate 和环境搭建
  16. [方案]基于Zynq WiFi方案构建
  17. JSON数据、PHP数组 转换 Excel表格
  18. 【织梦dedecms安全设置】dedecms如何防止被黑?dedecms被黑了怎么办?
  19. Python正则替换字符串函数re.sub用法示例(1)
  20. 一: vue的基本使用

热门文章

  1. jsp安全性问题
  2. python 快速排序详述
  3. Hadoop生态优秀文章集锦
  4. java拾遗3----XML解析(三) StAX PULL解析
  5. Full卷积 Same卷积 Vaild卷积
  6. 最简单的 GitExtensions 教程(持续更新中)
  7. 洛谷 2216 [HAOI2007]理想的正方形
  8. Django 之 admin组件使用&amp;源码解析
  9. centos下写Symfony
  10. 从硬盘设计思想到RAID改良之道