题目大意:给你一串数$a_i$,求$sgcd(a_1,a_i)$,$sgcd(x,y)$表示$x,y$的次大公约数,若没有,则为$-1$
题解:即求最大公约数的最大约数,把$a_1$分解质因数,求出最大公约数,再判断是否可以被整除就行了
卡点:无
C++ Code:
#include <cstdio> #include <vector> #define maxn 100010 std::vector<long long> v; int n, sz; long long s[maxn]; long long gcd(long long a, long long b) {return b ? gcd(b, a % b) : a;} int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", s + i); long long t = s[1]; for (long long i = 2; i * i <= t; i++) { if (t % i == 0) { while (t % i == 0) t /= i; v.push_back(i); sz++; } } if (t > 1) v.push_back(t), sz++; for (int i = 1; i <= n; i++) { long long tmp = gcd(s[1], s[i]); if (tmp == 1) printf("-1"); else { for (int i = 0; i < sz; i++) if (tmp % v[i] == 0) { printf("%lld", tmp / v[i]); break; } } putchar(i == n ? ' ' : ' '); } return 0; }