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 ...
随机推荐
- etcd入门
简介 etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库. etcd内部采用raft协议作为一致性算法,基于Go语言实现. et ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-search
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- win下的常用8个命令
windows下常用的几个指令 一,ping 它是用来检查网络是否通畅或者网络连接速度的命令.作为一个生活在网络上的管理员或者黑客来说,ping命令是第一个必须掌握的DOS命令,它所利用的原理是这样的 ...
- C++ 非白名单程序间接启动
主要的思路是不能用不受信任的程序直接参与创建进程,而是间接启动目标进程.比如你可以把目标程序创建快捷方式,然后设置快捷键.然后向桌面发快捷键的按键消息,目标程序就会被桌面程序启动.
- c#查看本机网络端口和对应的程序名
360安全卫士里面有个组件叫流量防火墙,感觉挺好用,但是不想安装360全家桶,于是自己捣鼓着用C#写一个比较简化的版本. 查看电脑上开启的TCP或UDP端口,可以用netstat命令,netstat用 ...
- 六十七、SAP中内表插入的三种方法之一,APPEND的使用
一.如果内表是一个普通的内表,只用于存储数据不用来排序,那么优先选择APPEND插入 二.我们运行程序,并把工作区和内表加入到断点变量,如图所示,1X22的意思如图 三.我们点击ITAB1,来看内表数 ...
- Windows系统安装免费的开源虚拟机软件VirtualBox
https://www.qikegu.com/uncategorized/1179 VirtualBox是什么 VirtualBox是一个虚拟机平台软件,在VirtualBox平台上可以安装各种操作系 ...
- junit基础学习之-简介(1)
JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1 JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...
- 线段树&树状数组与离散化的妙用
牛客2019多校联盟Day7 Fine the median 题意: 每次给数组插入区间[Li,Ri] 内的所有数,每操作一次查询中位数. 遇到这题真的算是巧合,然而就是这种冥冥之中的缘分,给了我线 ...
- 使用kali中的Metasploit通过windows7的永恒之蓝漏洞攻击并控制win7系统(9.27 第十三天)
1.开启postgresql数据库 2.msfconsole 进入MSF中 3.search 17-010 搜索cve17-010相关的exp auxiliary/scanner/smb/smb_ms ...