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

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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/315446.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) dfs + 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给一张图&#xff0c;求必须经过aaa点和bbb点的路径条数。 思路&#xff1a; 通过观察我们发现&#xff0c;这个路径无非就是x−>a−>b−>yx->a->b->yx−>a−>b−>y或者x−>…

一张图带你了解 Insider Dev Tour 2019中国技术大会

点击阅读原文&#xff0c;前往微软Insider Dev Tour 全球官网

Codeforces Round #632 (Div. 2)巧用小技巧

Codeforces Round #632 (Div. 2)点这 Eugene likes working with arrays. And today he needs your help in solving one challenging task. An array c is a subarray of an array b if c can be obtained from b by deletion of several (possibly, zero or all) elements f…

Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) 构造

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给nnn个数&#xff0c;让你构造一个尽可能大的矩阵&#xff0c;其中每个点所在的行和列都不含相等元素。 思路&#xff1a; 假设构造的答案矩阵大小为ababab且a<ba<ba<b&#xff0c;那么我们可以…

.NET项目迁移到.NET Core操作指南

这篇文章&#xff0c;汇集了大量优秀作者写的关于".NET迁移到.NET Core"资料文章以及微软官方教程文档。是我在迁移公司框架项目到.NET Core和.NET Standard时遇到的问题&#xff0c;并将相关资料整理成这篇文章。记录如何一步一步把项目迁移到.NET Core。在此感谢这…

Educational Codeforces Round 88 D. Yet Another Yet Another Task(巧枚举)

cf地址 **题目大意&#xff1a;**一个序列&#xff0c;你可以选择一个子段&#xff0c;要求去掉子段最大值后的和最大&#xff0c;求出这个最大值 **思路&#xff1a;**a[i]的范围比较小&#xff0c;可以通过枚举最大值&#xff0c;再找到最大值可以辐射的区间。经典套路了。…

Codeforces Round #607 (Div. 2) E. Jeremy Bearimy dfs + 思维

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给你2∗k2*k2∗k个点的一棵树。定义GGG为任选kkk组不同的点&#xff0c;每组点的距离和的最小值。定义BBB为任选kkk组不同的点&#xff0c;每组点的距离和的最大值。让你求出GGG和BBB。 思路&#xff1a; …

EF Core 3 的 40 个中断性变更

为了修复 Entify Framework Core 中许多已发现的缺陷&#xff0c;微软在 EF Core 3 中引入了 40 个中断性变更。我们可以在微软文档中查看完整的中断性变更列表&#xff0c;本文仅列举几个主要的点。客户端查询为了突破 EF Core SQL 生成器的限制&#xff0c;默认只在客户端执行…

Codeforces Round #646 (Div. 2) E(贪心,bfs)

Codeforces Round #646 (Div. 2) E 题目大意&#xff1a; 给一棵树&#xff0c;每个节点有三个权值 A,B,C, (B,C为0或1)&#xff0c;每次你可以花费 A[u] *k的代价让A子树中的任意 k 个节点交换彼此的 B &#xff0c;问让所有节点的 BC 至少花费多少代价。 思路&#xff1a; …

Educational Codeforces Round 77 (Rated for Div. 2) D. A Game with Traps 贪心 +二分

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 给你nnn个人&#xff0c;每个人都有个能力值aia_iai​。让后在1−m1-m1−m的路上有kkk个陷阱&#xff0c;每个陷阱的范围是[li,ri][l_i,r_i][li​,ri​]&#xff0c;伤害是did_idi​&#xff0c;能力值低于d…

联手微软,Docker公司将推出Docker Desktop for WSL 2

微软最新推出的 WSL 2 在架构方面发生了重大的变化&#xff1a;它提供了一个在轻量级 VM 中运行的真正 Linux 内核。使用真正的 Linux 内核意味着可以在 Windows 上运行 ELF64 Linux 二进制文件。对 Docker 来说绝对是利好消息&#xff0c;因为这意味着 Linux 版本的 Docker 可…

SP1026 FAVDICE - Favorite Dice 期望dp

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 一个n面的骰子&#xff0c;求期望掷几次能使得每一面都被掷到。 思路&#xff1a; 考虑期望dpdpdp。定义f[i]f[i]f[i]表示有iii面了&#xff0c;还需要多少次能到nnn面。当前是iii面&#xff0c;所以选到新…

Educational Codeforces Round 88 (Rated for Div. 2) E(数学)

Educational Codeforces Round 88 (Rated for Div. 2)E 题目大意: 给你n&#xff0c;k(1<k<n<5e5)&#xff0c;从1到n中选k个数组成一个严格递增序列&#xff0c;如果对任何正整数&#xff0c;依次模上这k个数&#xff0c;无论这k个数如何排列得到的答案都相同&#…

HAProxy 2.0发布,长期支持版本

HAProxy 2.0 发布了。HAProxy 是一个使用 C 语言编写的自由及开源软件&#xff0c;其提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理&#xff0c;支持虚拟主机&#xff0c;它是免费、快速并且可靠的 Web 负载均衡解决方案。包括 GitHub、Bitbucket、Stack Overflow、R…

Codeforces Round #651 (Div. 2) D

D. Odd-Even Subsequence 题目大意&#xff1a;在a数组中 保留k个数字&#xff0c;如何代价最小的多少。 代价的算法 具体看题意&#xff1a;就是k数组中 min{max{奇数下标}&#xff0c;max{偶数下标}} 解题思路&#xff1a;贪心加二分&#xff0c;二分全部的答案&#xff08…

CF1042E Vasya and Magic Matrix 期望dp + 推公式

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 将矩阵中的数放到数组里排序&#xff0c;就是一个比较明显的期望dpdpdp了。 定义f[i]f[i]f[i]表示从第iii个出发的期望得分&#xff0c;所以转移方程也比较好写了&#xff1a;f[i]∑(f[j](…

C#并发编程之异步编程(一)

写在前面C#5.0中&#xff0c;对异步编程进行了一次革命性的重构&#xff0c;引入了async和await这两个关键字&#xff0c;使得开发人员在不需要深刻了解异步编程的底层原理&#xff0c;就可以写出十分优美而又代码量极少的代码。如果使用得当&#xff0c;你可以写出具有并行化并…

简单思维dp-- Gym - 102392B

Gym - 102392B点 题意&#xff1a;Steve想要在游戏中升到 两级&#xff0c;给你s1和s2 分别为1级需要的经验和二级需要的经验&#xff0c;然后给你n给任务&#xff0c;任务在1级前和在1级后的经验不同&#xff0c;完成的时间也不同&#xff0c;在刚刚升1级时&#xff0c;所溢出…

P4316 绿豆蛙的归宿 期望dp + DAG

传送门 文章目录题意&#xff1a;思路&#xff1a;题意&#xff1a; 思路&#xff1a; 首先要发现这是一个DAGDAGDAG图&#xff0c;让后我们可以用拓扑在图上跑期望dpdpdp。 定义f[i]f[i]f[i]表示iii到nnn的期望路径长度&#xff0c;知道终止状态f[n]0f[n]0f[n]0&#xff0c;所…

程序员修神之路--高并发下如何缩短响应时间

点击上方“蓝字”带你去看小星星菜菜哥&#xff0c;请你看电影呀&#xff0c;但是得帮我一个忙好呀&#xff0c;看什么&#xff1f;哥斯拉2&#xff1a;怪兽之王看过了~X战警&#xff1a;黑凤凰看过了追龙2和黑衣人呢&#xff1f;都看过了&#xff0c;你说帮什么忙吧我一个网站…