ACM学习历程—广东工业大学2016校赛决赛-网络赛F 我是好人4(数论)
2024-09-26 00:25:14
题目链接: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 ;
}
最新文章
- 前端学PHP之mysql扩展函数
- 注解:【无连接表的】Hibernate双向1->;N关联
- svn没有对号等符号的问题
- java servlet+mysql全过程(原创)
- 根据OSG中的ref_ptr和Reference简化的智能指针
- Web Storage的方法
- MySQL安装过程net start mysql 启动失败 报“错误2,系统找不到文件”的解决办法
- CentOS yum kvm
- Maven手动创建多模块项目
- Linux 多学习过程
- 原生JS元素怎么取消事件
- 基于winpcap的以太网流量分析器(java)
- NYoj289苹果(0-1背包)
- 读Ghost博客源码与自定义Ghost博客主题
- 初始hibernate 和环境搭建
- [方案]基于Zynq WiFi方案构建
- JSON数据、PHP数组 转换 Excel表格
- 【织梦dedecms安全设置】dedecms如何防止被黑?dedecms被黑了怎么办?
- Python正则替换字符串函数re.sub用法示例(1)
- 一: vue的基本使用