You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.3 KiB

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
//最大公约数
int gcd(int x, int y) {
return y ? gcd(y, x % y) : x;
}
const int N = 1e6 + 10;
int a[N], Left[N], Right[N];
int n;
/**
* 思想:预处理+前缀和+后缀和
* 前缀和:这里不是真正的前缀和,是预处理出来的前面的最大公约数数组
* 后缀和:这里不是真正的后缀和,是预处理出来的后面的最大公约数数组
*/
/**
* 测试用例:
5
12 36 24 18 48
* @return
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
//生成left数组
Left[1] = a[1];
for (int i = 2; i <= n; i++) Left[i] = gcd(Left[i - 1], a[i]);
//生成right数组
Right[n] = a[n];
for (int i = n - 1; i >= 1; i--)Right[i] = gcd(a[i], Right[i + 1]);
//输出调试结果
//for (int i = 1; i <= n; i++) cout << Left[i] << " ";
//cout << endl;
//for (int i = 1; i <= n; i++) cout << Right[i] << " ";
//cout << endl;
//输出ans
//删除第一个
cout << Right[1 + 1] << " ";
//删除中间的
for (int i = 2; i < n; i++)
cout << gcd(Left[i - 1], Right[i + 1]) << " ";
//删除最后一个
cout << Left[n - 1] << endl;
return 0;
}