CF1349A. Orac and LCM(策略 + 维护后缀gcd)
原题链接
题意:给你n个数,给你两种定义一个为gcd,lcm
如:gcd({8,12}) =4, gcd({12,18,6})=6,lcm({4,6})=12.
1.对于这个n个数我们定义一个集合t={lcm({ai,aj}) | i<j}
2.之后我们要进行求解答案gcd({lcm({ai,aj}) | i<j})
解法:我们可以把所有i相同的定义为一组,那么gcd({lcm{ai,ai+1…an}})
那么对于i=1时gcd1 = gcd(lcm(a1,a2),lcm(a1,a3)…lcm(a1,an))都有公因子a1
那么此时就gcd1就等价于求lcm(a1,gcd(a2,a3…an)),
gcd2同理求lcm(a2,gcd(a3,a4…an))
最终我们需要求的答案就是gcd(gcd1,gcd2,gcd3…gcdn)
那么我们实际只需要求解后面i个数的gcd即可
#include<bits/stdc++.h>
#define sc scanf
#define pr printf
#define ll long long
using namespace std;
const int maxn = 1e5 + 100;
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;
}
ll lcm(ll a, ll b){return a*b/gcd(a,b);
}
ll a[maxn];
ll pre[maxn];
void solve(){int n;sc("%d",&n);for(int i = 1; i <= n; i++){sc("%lld",&a[i]);}//求出后缀gcd之后 for(int i = n; i >= 1; i--){pre[i] = gcd(a[i],pre[i + 1]);} //那么我获取每一个可以按照公式进行求解 ll res = 0;for(int i = 1; i <= n; i++){res = gcd(res,lcm(a[i],pre[i + 1]));} pr("%lld\n",res);
}
int main(){// freopen("2.in","r",stdin);int t;
// sc("%d",&t);t = 1;while(t--)solve();
}
CF1349A. Orac and LCM(策略 + 维护后缀gcd)相关推荐
- C. Orac and LCM(数论lcm, gcd)
C. Orac and LCM 思路 题目非常简单,就是求gcd(lcm(i,j))foriinrange(n),forjinrange(n),i<jgcd(lcm_(i,\ j))\ for\ ...
- C. Orac and LCM(gcd与lcm的性质)
Problem - 1350C - Codeforces 题意: 对于正整数的多集合s={s1,s2,...,sk},定义s的最大公除数(GCD)和最小公倍数(LCM)如下. gcd(s)是最大的正整 ...
- Orac and LCM(数论)
题目链接: Orac and LCM 大致题意: 给你一个长度为 n 的数组,求 gcd {lcm({ai , aj}) | i < j} 解题思路: gcd1=gcd[lcm(a1,a2),l ...
- 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 ...
- Orac and LCM #641(div2) c题--求质因数次小指数
Orac and LCM cf地址 For the multiset of positive integers s={s1,s2,-,sk}, define the Greatest Common D ...
- C. Orac and LCM
链接:https://codeforces.ml/contest/1350/problem/C For the multiset of positive integers s={s1,s2,-,sk} ...
- CodeForces - 1350C Orac and LCM(数论)
题目链接:点击查看 题目大意:给出 n 个数,先求出两两 lcm 后的集合 t ,再求这个集合 t 的 gcd 题目分析:做这个题得知道一个前置知识:对于 lcm 和 gcd 运算来说,每个质因子都相 ...
- CF1621G Weighted Increasing Subsequences(离散化+树状数组优化dp+栈维护后缀最大值+计数)
problem luogu-link solution 显然单独考虑每个 iii 的贡献,即被多少个合法上升子序列包含. 令 x=max{j∣j>i∧aj>ai}x=\max\{j\ | ...
- 【Codeforces 1349A】Orac and LCM
思路 gcd1=gcd[lcm(a1,a2),lcm(a1,a3)-lcm(a1,an)]=gcd(a1,lcm(a2,a3-an)) gcd2=gcd[lcm(a2,a3),lcm(a2,a4)-l ...
最新文章
- android 数据传递详解(Serialization、Parcelable、Parcel、Intent、Bundle)
- WordPress中的cookie 机制
- vue点击按钮上传图片_关于javascript:Vue-点击按钮提交表单
- python删除文件和linux删除文件区别_使用Python批量删除文件列表
- 常喝酒的人,为什么会出现手抖的现象?
- spring 整合struts
- Objective--C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)
- C++函数模板特化,类模板特化
- 内外盘分仓跟单系统源码CTP程序化交易系统源码
- RapidMiner Studio 自动模型
- 程序员英语学习指南(建议收藏)
- 摄影基础知识——焦点
- win10系统下计算器界面变成英文的解决方法
- 用proteus来看二极管的压降
- 电脑预装Office2016打开Word时点击保存弹出“word无法启动转换器RECOVR32.CNV”对话框问题的修复方法
- 2016-7-20 奶奶走了
- 天源财富:“星际访客”和太阳系彗星中探测到镍
- 集成基于Websocket的Stomp连接
- STM32调试FM24C04B-G心得及注意事项,解决不应答等
- 美军如何在不可信设备上安全访问国防部网络?