[gcd]Codeforces Common Divisors
2 seconds
256 megabytes
standard input
standard output
You are given an array aa consisting of nn integers.
Your task is to say the number of such positive integers xx such that xx divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.
For example, if the array aa will be [2,4,6,2,10][2,4,6,2,10], then 11 and 22 divide each number from the array (so the answer for this test is 22).
The first line of the input contains one integer nn (1≤n≤4⋅1051≤n≤4⋅105) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤10121≤ai≤1012), where aiai is the ii-th element of aa.
Print one integer — the number of such positive integers xx such that xx divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).
5
1 2 3 4 5
1
6
6 90 12 18 30 18
4
题意:
给你n个数,找出这n个公因数的个数
思路:
这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数
看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个
注意:
最好用scanf,如果用cin要加上流加速,否则会TLE
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=4e5+;
ll a[amn];
ll gcd(ll a,ll b){
return !b?a:gcd(b,a%b);
}
int main(){
ios::sync_with_stdio(); ///以后要习惯性地加上流加速
int n;
cin>>n; ///以后大数据时用cin还是要加上流加速,或直接用scanf,没加前在test5总TLE
for(int i=;i<=n;i++){
cin>>a[i];
}
ll minn=a[];
for(int i=;i<=n;i++){
minn=min(gcd(minn,a[i]),minn); ///找出他们中的一个最小的最大公因数,记为minn
if(minn==){printf("1\n");return ;} ///如果是1就直接输出后退出
}
// cout<<minn<<endl;
ll ans=;
for(ll i=;i*i<=minn;i++){ ///看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
if(i*i==minn)ans++; ///如果这个因子的平方等于minn那就只算一个
else if(minn%i==)ans+=; ///否则找到了i,就可知道n/i,所以要加上2个
}
printf("%lld\n",ans);
}
/***
给你n个数,找出这n个公因数的个数
这些公因数的值不会超过他们的最大公因数,因此我们要找到这n个数的最大公因数
看这个最小的最大公因数minn有多少个因子,现在从1找到sqrt(minn),或写成i*i<=minn
如果这个因子的平方等于minn那就只算一个,否则找到了i是minn的因子,就可知道n/i也是minn的因子,所以要加上2个
注意最好用scanf,如果用cin要加上流加速,否则会TLE
***/
[gcd]Codeforces Common Divisors的更多相关文章
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板
You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...
- CF #579 (Div. 3) C.Common Divisors
C.Common Divisors time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...
- Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)
http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...
- Common Divisors CodeForces - 1203C
题意: 给你n个数,让你找出来公因子有多少个.公因子:对于这n个数,都能被这个公因子整除 题解: 只需要找出来这n个数的最大公因子x,然后找出来有多少不同数能把x给整.(因为我们可以保证x可以把这n个 ...
- [Lyft Level 5 Challenge 2018 - Elimination Round][Codeforces 1033D. Divisors]
题目链接:1033D - Divisors 题目大意:给定\(n\)个数\(a_i\),每个数的约数个数为3到5个,求\(\prod_{i=1}^{n}a_i\)的约数个数.其中\(1 \leq n ...
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】
题目: Mr. F has nn positive integers, a1,a2,…,an. He thinks the greatest common divisor of these integ ...
随机推荐
- OSX编译安装Python3及虚拟开发环境Virtualenv
0X00.前言 因为工作原因,最近主要做Python开发,刚好电脑系统重装之后所有的东西都需要重新配置.此文主要记录OSX下通过源码编译安装Python3以及安装虚拟开发环境Virtualenv. 0 ...
- 获取网站title的脚本
脚本在此 公司的商城需要添加一个脚本,这个脚本就是观察首页页面是否正常,虽然已经配置了zabbix监控网站是否200,但是有一些特殊的情况,比如网页可以打开但是页面是"file not fo ...
- git push的完整形式
现在的情况是,本地有两个分支:master.div, 远程仓库有一个分支:master,本地master分支和远程master分支建立有跟踪联系,这样本地master分支提交时直接git push(只 ...
- 【快速上手】Git的使用教程
创建Git仓库 git init 查看当前仓库情况 git status 添加修改 git add (file) or git add . 查看未提交的修改 git diff 撤销提交操作 git r ...
- 如何成为一名高级C++程序员
C++这门语言从诞生到今天已经经历了将近30个年头.不可否认,它的学习难度都比其它语言较高.而它的学习难度,主要来自于它的复杂性.现在C++的使用范围比以前已经少了很多,java.C#.python ...
- Pom.xml的依赖自动生成
1.第一种用引入jar包的方法 网盘链接:https://pan.baidu.com/s/10HNjNeZc1d5QrFNtvLPWBA 提取码:oako 以上是整个文件直接用idea打开即可 imp ...
- 完整版EXCEL导出 (大框架SpringCloud 业务还是Springboot一套)
这里用的是easypoi 首先引入jar包 <!-- excel --><dependency> <groupId>cn.afterturn</groupId ...
- idea通过maven打jar包不成功或无法加载主类或打成功也找不到jar包
这个问题纠结了我一天,在网上也搜了一些方法,现在把我自己的处理过程记录一下,以方便遇到此类问题的小伙伴参考: 第一步:查看idea的jdk版本与Windows安装的jdk是不是同一个版本,如下图 第二 ...
- UVA - 10462 Is There A Second Way Left?
题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...
- preload & prefetch
原文地址在 我的笔记里,觉得还行就给个 star 吧:) 关于 preload 和 prefetch 早有耳闻,知道它们可以优化页面加载速度,然具体情况却了解不多.搜索了相关的资料后对其有了些认识,在 ...