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 ...
随机推荐
- Centos 8下普通用户增加root权限
问题: 解决: 重启Centos,使用root登陆:
- 【剑指Offer】面试题32 - III. 从上到下打印二叉树 III
题目 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推. 例如: 给定二叉树: [3,9,20,nu ...
- 使用Ubuntu系统管理包工具(apt)部署Zabbix企业级监控系统
使用Ubuntu系统管理包工具(apt)部署Zabbix企业级监控系统 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Ubuntu系统部署笔记:https://www.cnblo ...
- JDK源码阅读-------自学笔记(一)(java.lang.Object重写toString源码)
一.前景提要 Object类中定义有public String toString()方法,其返回值是 String 类型. 二.默认返回组成 类名+@+16进制的hashcode,当使用打印方法打印的 ...
- C#不显示在任务栏
在我用c#写一些小程序是总是希望,程序窗体不在任务栏上显示程序的窗体,c# Form提供了一个 属性值可以很好的解决这个问题 这个属性就是 ShowInTaskbar 在微软的官方声明格式为: pub ...
- Hadoop组件详解(随缘摸虾)
1.1. Hadoop组成: Hadoop = hdfs(存储) + mapreduce(计算) + yarn(资源协调) + common(工具包) + ozone(对象存储) + submarin ...
- vue学习(十)mixin 偷懒
一 mixin混入偷懒技术 架子 <div id="app"> {{msg}} </div> <script> let app = new Vu ...
- 快速搭建单机版Spring Cloud EurekaServer
原文链接:http://www.yiidian.com/springcloud/build-eureka-single.html 本文介绍搭建单机版的Eureka Server服务 1 创建项目,导入 ...
- POJ 3295:Tautology
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10482 Accepted: 3982 Descri ...
- tensorflow-cnnn-mnist
#coding=utf-8import tensorflow as tfimport numpy as npimport matplotlib .pyplot as pltfrom tensorflo ...