GCD + 调和级数

Problem - 1627D - Codeforces

题意

有 \(n\;(1<=n<=10^6)\) 个互不相同的数 \(a[i]\;(1<=a[i]<=10^6)\), 每次可以在 a 数组中选择两个数 \(a[i],a[j]\), 将令 \(d=gcd(a[i],a[j])\), 如果 \(d\), 不在 a 数组中,就把 \(d\) 加进去

求最多能加进去多少个数

思路

  1. 看到值域只有 \(10^6\), 可以想到可能与调和级数有关
  2. 对于数论题,很多时候可以更换枚举的顺序,所以可以枚举 \(d\), 看 \(d\) 能不能被加进去
  3. 对于所有 \(d\) 的倍数,如果他们的 \(gcd\) 等于 \(d\), 说明 \(d\), 可以被加进去
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; const int N = 1e6 + 10;
int n;
int cnt[N]; int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
cnt[x]++;
}
for (int d = 1; d <= N - 10; d++)
{
int g = 0;
for (int i = d; i <= N - 10; i += d)
if (cnt[i]) g = __gcd(g, i);
if (g == d && !cnt[d]) ans++;
}
cout << ans << endl;
return 0;
}

Codeforces Round #766 (Div. 2) - D. Not Adding的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. Nacos安装与启动

    一.官网下载 1. 地址 https://github.com/alibaba/nacos/releases   二.安装 将下载的安装包解压至非中文目录即可,解压后目录: bin 目录下有启动脚本, ...

  2. Redis设置开机自启动

    0.前提条件 redis_version:7.0.5 Linux Alibaba Cloud Linux release 3 (soaring Falcon) 查询版本有如下两种方式: 1.通过red ...

  3. MNIST数据集output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

    transform = transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: x.repeat(3,1,1)) ...

  4. SAR成像(六):距离徙动矫正

    1.什么是距离徙动? 距离徙动产生原因是合成孔径雷达的运动平台与目标之间的相对运动.对于地面一静止的点目标,当它处于雷达波束照射范围内的时间中时,随着飞行载体的运动,它与雷达飞行载体之间的距离会不断的 ...

  5. tomcat不生成日志文件

    1.不生成 catalina.out 将 /bin/catalina.sh 文件中的: if [ -z "$CATALINA_OUT" ] ; then CATALINA_OUT= ...

  6. c# message (转)

    一.消息概述 Windows下应用程序的执行是通过消息驱动的.消息是整个应用程序的工作引擎,我们需要理解掌握我们使用的编程语言是如何封装消息的原理. 什么是消息(Message) 消息就是通知和命令. ...

  7. pyqt5离线安装教程

    目前总结的安装pyqt5,需要的离线安装包,除了每一个包要跟系统版本适配之外,还要考虑包跟包之间的适配.pyqt5跟它开头的一些包要保持是同一个版本,至少有2个小数点的位数是一样的才行,qt5跟它开头 ...

  8. 备份docker mysql数据库

    1.查看容器 docker ps 2.进入容器 docker exec -it 容器id /bin/bash 3.备份数据 mysqldump 数据库名称 -uroot -p >/root/** ...

  9. Python的入门学习Day 14~15——form”夜曲编程“

    Day 14 time: 2021.8.11. ​ 原文再续,输接上一回.在数据结构之上,我们又开启了新一章"循环"的学习.而今天,我们首先学习了"for"循环 ...

  10. 记录安装perl-Verilog过程

    开始,编译带Verilog::Netlist的脚本,报 YumRepo Error: All mirror URLs are not using ftp, http[s] or file.centos ...