B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板
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 eachnumber 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).
Input
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.
Output
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).
Examples
5
1 2 3 4 5
1
6
6 90 12 18 30 18
4 求一组数的共同因数的数目。
看数据,暴力一个一个看肯定超时。看完这个题,我的第一反应,竟然只知道这些因数一定小于最小的数(废话)。结果思维一直限制,绕在里面出不来。
打了一年了,自己还是....唉,努力训练吧,上题解。
以下两种解法都需要求出这组数的最大公因数。为什么呢,找到总的最大公因数N,那么剩下的公因数一定小于它。竟然这组数可以整除N,那么也一定可以整除N的因数。
接下来的工作,就是找N的因数个数。
解法一:直接找,但是不能暴力一个一个for,还是会超时。要知道,一个数N的因数,以sqrt(N)为分界线,一半在左,一半在右。所以我们只需求出<sqrt(n)的部分ans,
ans*2,然后对sqrt(N)特判一下,看看,是否(int)sqrt(N)*(int)sqrt(N)==N.
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=4e5+;
ll tot=;
ll a[maxn];
ll cha[maxn];
ll b[maxn];
int main()
{
ll n;
while(cin>>n)
{
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
ll k=a[];
for(int i=;i<n;i++)
k=__gcd(k,a[i]);
if(k==)
{
printf("1\n");
continue;
}
double mid=sqrt(k);
ll ans=;
for(int i=;i<mid;i++)
{
if(k%i==)
ans++;
}
ans*=;
if((int)mid*mid==k)
ans++;
cout<<ans<<endl;
}
}
解法二:数论。算术基本定理(摘自csdn):

所以对N进行分解:
ll ans=;
for(ll i=;i*i<=t;i++)
{
if(t%i==)
{
ll cnt=;
while(t%i==)
{
t=t/i;
cnt++;
}
ans=ans*(cnt+);
}
}
if(t>) //别忘了加
ans*=;
//算是唯一分解定理的一个模板
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=4e5+;
ll a[maxn];
int main()
{
ll n;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
ll t=a[];
for(int i=;i<n;i++)
t=__gcd(t,a[i]);
if(t==)
cout<<""<<endl;
else
{
ll ans=;
for(ll i=;i*i<=t;i++)
{
if(t%i==)
{
ll cnt=;
while(t%i==)
{
t=t/i;
cnt++;
}
ans=ans*(cnt+);
}
}
if(t>)
ans*=;
cout<<ans<<endl;
}
}
不多说,接着训练!不给自己的acm生涯留遗憾!
B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板的更多相关文章
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- Uva10791 唯一分解定理模板
唯一分解定理: Uva10791 题意: 输入整数n,要求至少两个正整数,使得他们的最小公倍数为n,且这些整数的和最小 解法: 首先假设我们知道了一系列数字a1,a2,a3……an,他们的LCM是n, ...
- Common Divisors CodeForces - 1203C
题意: 给你n个数,让你找出来公因子有多少个.公因子:对于这n个数,都能被这个公因子整除 题解: 只需要找出来这n个数的最大公因子x,然后找出来有多少不同数能把x给整.(因为我们可以保证x可以把这n个 ...
- acm数论之旅--唯一分解定理
题目: 给出n,问n = b^p中p符合该等式的最大值 分析: 先求出所有n的质因子,然后对这m个质因子分类统计,比如 n = 36时,可以分成 2个2,2个3,然后求出所有这些基数的 最大公因数gc ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- UVA10791-Minimum Sum LCM(唯一分解定理基本应用)
原题:https://vjudge.net/problem/UVA-10791 基本思路:1.借助唯一分解定理分解数据.2.求和输出 知识点:1.筛法得素数 2.唯一分解定理模板代码 3.数论分析-唯 ...
- [gcd]Codeforces Common Divisors
Common Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #579 (Div. 3) B Equal Rectangles、C. Common Divisors
B Equal Rectangles 题意: 给你4*n个数,让你判断能不能用这个4*n个数为边凑成n个矩形,使的每个矩形面积相等 题解: 原本是想着用二分来找出来那个最终的面积,但是仔细想一想,那个 ...
- 简单数论之整除&质因数分解&唯一分解定理
[整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...
随机推荐
- 解决d7在更高版本上运行乱码问题,或者是调用更高版本的dll
将String类型改成WideString类型即可
- delphi base64编码
需要uses IdCoderMIME: function TForm1.Base64E(Path: string): string;var filepath: string; filestream: ...
- 吴裕雄--天生自然java开发常用类库学习笔记:一对多关系范例
import java.util.List ; import java.util.ArrayList ; public class School{ private String name ; priv ...
- JuJu团队12月1号工作汇报
JuJu团队12月1号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 修改generator函数 优化代码 不熟悉julia 婷婷 和金华一起调试main.jl 继 ...
- HihoCoder第十四周:无间道之并查集
#1066 : 无间道之并查集 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 这天天气晴朗.阳光明媚.鸟语花香,空气中弥漫着春天的气息--额,说远了,总之,小Hi和小H ...
- 【CS224n-2019学习笔记】Lecture 1: Introduction and Word Vectors
附上斯坦福cs224n-2019链接:https://web.stanford.edu/class/archive/cs/cs224n/cs224n.1194/ 文章目录 1.课程简单介绍 1.1 本 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 数据抽象
数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节. 数据抽象是一种依赖于接口和实现分离的编程(设计)技术. 它们向外界提供了大量用于操作对象数据的公共方法,也 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-camera
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- jupiter的@TempDir 等不生效
jupiter与junit是 完全独立的测试组件,要严防在测试中将二者混用.最好在依赖引入jupiter 时 就将junit的依赖干掉,以防在写测试用例时将二者混用.不会报错,但是会导致 jupite ...
- 一、VIP课程:互联网工程专题 04-Maven私服使用与插件开发
第四课:Maven私服构建与插件开发.docx 一.maven 生命周期 知识点概要: 生命周期的概念与意义 maven 三大生命周期与其对应的phase(阶段) 生命周期与插件的关系 生命周期与默认 ...