Orac and LCM cf地址

For the multiset of positive integers s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:

gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.

lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12. Note that for any positive integer x, gcd({x})=lcm({x})=x.

Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

Input
The first line contains one integer n (2≤n≤100000).

The second line contains n integers, a1,a2,…,an (1≤ai≤200000).

Output
Print one integer: gcd({lcm({ai,aj}) | i<j}).

Examples
inputCopy
2
1 1
outputCopy
1
inputCopy
4
10 24 40 80
outputCopy
40
inputCopy
10
540 648 810 648 720 540 594 864 972 648
outputCopy
54
Note
For the first example, t={lcm({1,1})}={1}, so gcd(t)=1.

For the second example, t={120,40,80,120,240,80}, and it’s not hard to see that gcd(t)=40.

题目大意: 两两求最小公倍数,然后得到集合,求最大公因数。

思路: 求质因数次小的指数。
注意:当有某个质因数的个数为n的的时候,应该any*=次小指数
当为n-1的时候 应该为any*=最小指数。

     直接把0算进去应该不用考虑这两种情况了

我的代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define re register
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define rep(i,n) for(int i=0;(i)<(n);i++)
#define rep1(i,n) for(int i=1;(i)<=(n);i++)
#define se secondusing namespace std;
typedef long long  ll;
typedef unsigned long long  ull;
typedef pair<int,int > pii;
const ll mod=104857601;
const int N =2e5+10;
const double eps = 1e-6;
const double pi=acos(-1);
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int dx[4]={-1,0,1,0} , dy[4] = {0,1,0,-1};
int a[N];
int p[N], cnt;
bool st[N];void get_primes(int n)
{for (int i = 2; i <= n; i ++ ){if (!st[i]) p[cnt ++ ] = i;for (ll j = 0; p[j] <= n / i; j ++ ){st[p[j] * i] = true;if (i % p[j] == 0) break;}}
}int pow1(int a,int b)
{ll any=1;while(b){if(b&1) any*=a;a*=a;b>>=1;}return any;
}int d1[N];
int d2[N];
int d3[N];
int main()
{get_primes(N);int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}if(n==2){   printf("%lld",ll(a[1]/gcd(a[1],a[2]))*a[2]);return 0;}for(int i=1;i<=n;i++){for(int j=0;j<cnt;j++){if(a[i]<p[j]||a[i]<p[j]*p[j]) break;if(a[i]%p[j]==0){int x=0;d3[p[j]]++;while(a[i]%p[j]==0){a[i]/=p[j];x++;}if(x<=d1[p[j]]||d1[p[j]]==0){d2[p[j]]=d1[p[j]];d1[p[j]]=x;}else if(x<d2[p[j]]||d2[p[j]]==0){d2[p[j]]=x;}}}if(a[i]>1){d3[a[i]]++;int x=1;if(x<=d1[a[i]]||d1[a[i]]==0){d2[a[i]]=d1[a[i]];d1[a[i]]=x;}else if(x<d2[a[i]]||d2[a[i]]==0){d2[a[i]]=x;}}}ll any=1;for(int i=0;i<200000;i++){if(d3[i]==n) any*=pow1(i,d2[i]);else if(d3[i]==n-1) any*=pow1(i,d1[i]);}printf("%lld\n",any);return 0;
}

大神代码:优美的求次小指数,在最小指数的基础上求。

#include <bits/stdc++.h>using namespace std;typedef long long ll;ll n,d[100007],a,b;int main()
{cin>>n;cin>>d[1]>>d[2];a=__gcd(d[1],d[2]);b=d[1]*d[2];for(int i=3;i<=n;i++){cin>>d[i];b=__gcd(a*d[i],b);a=__gcd(a,d[i]);}cout<<b/a;return 0;
}

Orac and LCM #641(div2) c题--求质因数次小指数相关推荐

  1. C. Orac and LCM(数论lcm, gcd)

    C. Orac and LCM 思路 题目非常简单,就是求gcd(lcm(i,j))foriinrange(n),forjinrange(n),i<jgcd(lcm_(i,\ j))\ for\ ...

  2. Orac and LCM(数论)

    题目链接: Orac and LCM 大致题意: 给你一个长度为 n 的数组,求 gcd {lcm({ai , aj}) | i < j} 解题思路: gcd1=gcd[lcm(a1,a2),l ...

  3. C. Orac and LCM(数论)

    C. Orac and LCM(数论) 题目传送门 g 1 = g c d [ l c m ( a 1 , a 2 ) , l c m ( a 1 , a 3 ) - l c m ( a 1 , a ...

  4. 做算法题时的一些小技巧

    做算法题时的一些小技巧 技巧一: 在用C++做算法题时,我们会觉的cin,cout比scanf,printf使用起来更加的方便,不用指定输入输出格式. 但是cin,cout的运行时间比scanf,pr ...

  5. 电大考的是职称英语同计算机,最新电大统考计算机应用基础真题选择题详细分析小抄.doc...

    您所在位置:网站首页 > 海量文档 &nbsp>&nbsp计算机&nbsp>&nbsp计算机原理 最新电大统考计算机应用基础真题选择题详细分析小抄.d ...

  6. 5y计算机应用选择题答案,2016年电大网考计算机应用基础统考试题模拟真题及答案 含小抄复习资料推荐.docx...

    2016年电大网考计算机应用基础统考试题模拟真题及答案 含小抄复习资料推荐 电大计算机应用基础1一.单选题1.第一台电子计算机是1946年在美国研制成功的,该机的英文缩写名是______.答案: A ...

  7. 5y平台计算机应用测试题,2016电大网考计算机应用基础统考试题模拟真题及答案 含小抄复习资料.docx...

    2016电大网考计算机应用基础统考试题模拟真题及答案 含小抄复习资料 电大计算机应用基础1一.单选题1.第一台电子计算机是1946年在美国研制成功的,该机的英文缩写名是______.答案: A A:E ...

  8. Bugku crypto 入门题之聪明的小羊

    Bugku crypto 入门题之聪明的小羊 继续开启全栈梦想之逆向之旅~ 这题是Bugku crypto 入门题之聪明的小羊 积累逆向一个linux工具命令积累累了,gbd调试.于是来做些简单的题散 ...

  9. 求质因数只能是2,3,5,7的第n大个数(丑数求解)

    题目:求质因数只能是2,3,5,7的第n大个数.例如:1,2,3,4,5,6,7,8,910,12,14,15,16,18 方法一:循环判断每一个数(自然数序列)是否符合丑数的定义,直至第n个数.还有 ...

最新文章

  1. python深浅拷贝 面试_python基础-深浅拷贝
  2. SSH服务理论+实践
  3. python代码块使用缩进表示-Python 为什么使用缩进来划分代码块?
  4. ubuntu 下安装 cudnn
  5. C++模板的那丢丢事儿
  6. appium和selenium不同与相同之处
  7. 豆瓣评分 9.4 的编程巨著!《算法》
  8. 去oracle过程,记一次Oracle数据恢复过程
  9. 华为v3鸿蒙系统_重磅!华为鸿蒙系统问世!
  10. go - struct
  11. HMM、MEMM、CRF模型的比较
  12. 山东大学软件学院计算机组成原理课程设计整机实验(1)
  13. java基于uni-app框架的民宿客房预订系统 小程序
  14. java练习题,个人所得税计算
  15. MM-Wiki部署方案
  16. c51单片机万年历模拟,12864LCD屏显示实时温度与时间
  17. QGIS编译---QGIS3.22.4 + Qt5.15.3 + VS2019 ---64位版本
  18. anaconda3 复制or克隆环境
  19. 南卡蓝牙耳机和JBL蓝牙耳机哪个更值得买?音质最好的蓝牙耳机测评
  20. windows phone水平滑动翻页动画效果

热门文章

  1. 简述python解释器的作用_什么是python解释器?
  2. 原来医生的处方不是随便乱写的...
  3. 10个遥远但近在人间的天堂...
  4. 他让全世界凶手睡不着觉,现实版福尔摩斯,退休了4次又被拽回来工作,无敌实在是太寂寞了~...
  5. 暴击!被初中生碾压智商!这份被国家数学集训队采用的初中奥数资料究竟有多厉害?...
  6. 10张劲爆眼球的科学动图
  7. 雨中的蚊子为啥不会被雨滴砸死?
  8. 微信又添新功能!这个微信群可以学英语,而且全程免费
  9. 从小害怕数学的他,却成为了科普数学教育的数学家
  10. 100个微信小程序的源码公开分享