写在前面:
原数组、前缀gcd数组、后缀gcd数组下表从0开始
数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的GCD,称为前缀GCD。
数组suffixgcd[],对于suffixgcd[i]=g,g为a[i]-a[n-1]的GCD,称为后缀GCD。
前缀gcd数组:prefixgcd[ ]
prefixgcd[ i ] 表示原数组a[0]--->a[i]的公共gcd
后缀gcd数组:suffixgcd[ ]
prefixgcd[ i ] 表示原数组a[i]--->a[n - 1]的公共gcd
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.file.attribute.AclEntryFlag;
import java.security.AlgorithmConstraints;
import java.sql.Struct;
import java.text.CollationElementIterator;
import java.text.DateFormatSymbols;
import java.util.*;
import java.util.stream.Collectors;
class rd
{
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokenizer = new StringTokenizer("");
static String nextLine() throws IOException { return reader.readLine(); }
static String next() throws IOException
{
while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static double nextDouble() throws IOException { return Double.parseDouble(next()); }
static long nextLong() throws IOException { return Long.parseLong(next());}
static BigInteger nextBigInteger() throws IOException
{
BigInteger d = new BigInteger(rd.nextLine());
return d;
}
}
class PII
{
int x,y;
public PII(int x ,int y)
{
this.x = x;
this.y = y;
}
}
class math_myself
{
int gcd(int a,int b)
{
if(b == 0) return a;
else return gcd(b,a % b);
}
int lcm(int a,int b)
{
return a * b / gcd(a, b);
}
// 求n的所有约数
List get_factor(int n)
{
List<Long> a = new ArrayList<>();
for(long i = 1; i <= Math.sqrt(n) ; i ++)
{
if(n % i == 0)
{
a.add(i);
if(i != n / i) a.add(n / i); // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况 ^-^复杂度能减少多少是多少
}
}
// 相同因子去重,这个方法,完美
a = a.stream().distinct().collect(Collectors.toList());
// 对因子排序(升序)
Collections.sort(a);
return a;
}
}
public class Main
{
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5;
static math_myself math_me = new math_myself();
static int a[] = new int[N]; // 原数组
static int prefixgcd[] = new int[N]; // 前缀gcd数组
static int suffixgcd[] = new int[N]; // 后缀gcd数组
public static void main(String[] args ) throws IOException
{
int n = rd.nextInt();
for(int i = 0 ; i < n ; i ++) a[i] = rd.nextInt();
// 计算前缀gcd数组
prefixgcd[0] = a[0];
for(int i = 1 ; i < n ; i ++) prefixgcd[i] = math_me.gcd(prefixgcd[i - 1],a[i]);
// 计算后缀gcd数组
suffixgcd[n - 1] = a[n - 1];
for(int i = n - 2 ; i >= 0 ; i --) suffixgcd[i] = math_me.gcd(suffixgcd[i + 1],a[i]);
for(int i = 0 ; i < n ; i ++) pw.print(prefixgcd[i] + " ");
pw.println();
for(int i = 0 ; i < n ; i ++) pw.print(suffixgcd[i] + " ");
pw.println();
pw.flush();
}
}