【Nowcoder】数论知识点
一月 18, 2020
补充学习的知识点
数论前置知识部分学习笔记
题目
牛牛与LCM
【题意】
求给出的 \(n\) 个数能够选择其中的几个使得 LCM 为 \(x\) 。
【分析】
能够组成 LCM 的数 \(a_i\) 必然满足 \(x\; mod\; a_i =0\) 。
而显然,尽量多这样的 \(a_i\) 所求出来的 LCM 一定更接近于或者等于 \(x\) 。
于是我们对所有满足的 \(a_i\) 求 LCM ,判断是否等于 \(x\) 即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include<bits/stdc++.h> using namespace std; #define LL long long int n; LL m,a[100010]; LL GCD(LL x,LL y) { LL r=x%y; while(r) x=y,y=r,r=x%y; return y; } LL lcm(LL x,LL y) { LL z=GCD(x,y); return x/z*y; } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld",&a[i]); scanf("%lld",&m); LL ans=1; for (int i=1;i<=n;i++) if (m%a[i]==0) ans=lcm(ans,a[i]); if (ans==m) puts("Possible"); else puts("Impossible"); return 0; } |