题目链接: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 ;
}

ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)的更多相关文章

  1. ACM学习历程—广东工业大学2016校赛决赛-网络赛E 积木积水(最值问题 || 动态规划)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=4 这个题目自然会考虑到去讨论最长或者最短的板子. 笔上大概模拟一下的话,就 ...

  2. ACM学习历程—广东工业大学2016校赛决赛-网络赛D 二叉树的中序遍历(数据结构)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=3 这算是一个胡搞类型的题目.当然肯定是有其数据结构支撑的. 唯一的限制就是 ...

  3. ACM学习历程—广东工业大学2016校赛决赛-网络赛C wintermelon的魔界寻路之旅(最短路 && 递推)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=2 题目由于要找对称的路径,那么狠明显可以把右下角的每一块加到左上角对应的每 ...

  4. 广东工业大学2016校赛决赛重现——E积木积水(方法据说很多)

    Problem E: 积木积水 Description 现有一堆边长为1的已经放置好的积木,小明(对的,你没看错,的确是陪伴我们成长的那个小明)想知道当下雨天来时会有多少积水.小明又是如此地喜欢二次元 ...

  5. 广东工业大学2016校赛决赛-网络赛 1169 Problem A: Krito的讨伐 优先队列

    Problem A: Krito的讨伐 Description Krito终于干掉了99层的boss,来到了第100层.第100层可以表示成一颗树,这棵树有n个节点(编号从0到n-1),树上每一个节点 ...

  6. 广东工业大学2016校赛决赛-网络赛 1174 Problem F 我是好人4 容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  7. 广州工业大学2016校赛 F 我是好人4 dfs+容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  8. ACM学习历程—NPU1086 随机数 2015年陕西省程序设计竞赛网络预赛(正式赛)C题 (计数排序 || set容器)

    Description 开学了,ACM队的边老板想在学校中请一些妹子一起做一项问卷调查,调查妹子们对ACM的了解情况,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100), ...

  9. ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)

    hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB   描述 There is a strange storehouse in PKU. In this ...

随机推荐

  1. 【BZOJ】2186 沙拉公主的困惑

    一道很有价值的题. [解析1]欧几里德算法求乘法逆元,前缀和 [Analysis]O(T n log n). [Sum] ①int运算.假设会超出界,第一个数前要加上(LL)即类型转换. ②gcd不变 ...

  2. TextView实现打印机效果 ,字符串逐字显示

    https://github.com/lygttpod/AndroidCustomView/blob/master/app/src/main/java/com/allen/androidcustomv ...

  3. C语言中的编译时分配内存

    1.栈区(stack) --编译器自动分配释放,主要存放函数的参数值,局部变量值等: 2.堆区(heap) --由程序员分配释放: 3.全局区或静态区 --存放全局变量和静态变量:程序结束时由系统释放 ...

  4. WCF基础之序列化

    wcf是基于消息进行通信的,这篇就是简单说下序列化引擎是如何将.net object转化为xml消息.一般情况下很少用到这些,你只需定义数据协定之类的或者指定相应的序列化引擎,然后设置相应的特性就好. ...

  5. Redis通过PUBLISH / SUBSCRIBE 等命令实现了订阅与发布模式

    # 切换目录 [root@localhost /]# cd /opt/redis-4.0.10 # 启动客户端 -p 指定端口 [root@localhost ~]# redis-cli -p 638 ...

  6. Linux c编程:线程属性

    前面介绍了pthread_create函数,并且当时的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结构的指针.可以使用pthread_attr_t结构修改线程默认属性,并把这些 ...

  7. webdriver与JS操作浏览器元素

    1.JQuery的选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 c ...

  8. 【leetcode刷题笔记】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...

  9. php生成各种验证码

    片段 1 片段 2 片段 3 index.html ```<script type="text/javascript" src="jquery.min.js&quo ...

  10. how to close the old Session - if the same username starts a new Session?

    Question: want to close the old Session - if the same username starts a new Session Any ideas how i ...