Problem Description
Give an array A, the index starts from 1.
Now we want to know Bi=maxi∤jAj , i≥2.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integer n : the size of array A.
Next one line contains n integers, separated by space, ith number is Ai.
Limits
T≤20
2≤n≤100000
1≤Ai≤1000000000
∑n≤700000
Output
For each test case output one line contains n-1 integers, separated by space, ith number is Bi+1.
Sample Input
2
4
1 2 3 4
4
1 4 2 3
Sample Output
3 4 3
2 4 4
Source
2017 Multi-University Training Contest - Team 6
Recommend
liuyiding | We have carefully selected several similar problems for you: 6119 6118 6117 6116 6115
b[i]表示a數組中下標不能被i整除的最大值。
經過排序之後,之後那麼每次找到的第一個數就是答案了,時間複雜度低於O(n*n)
#include<iostream>
#include<algorithm>
using namespace std;
//不要小瞧人生啊
const int N = 1e5 + 10;
int T, n, a[N], id[N];
int main()
{
scanf("%d", &T);
for(int i=0;i<T;i++)
{
scanf("%d", &n);
for(int i=1;i<n+1;i++)
scanf("%d", a + i), id[i] = i;
sort(id + 1, id + 1 + n, [&](int x, int y)
{
return a[x]>a[y];
});//又學習到了cmp的另一種套路 //[&](int x,int y){return a[x]>a[y]}
for(int i=2;i<n+1;i++)//暴力
{
int f = 0;
for(int j=1;j<n+1;j++)
if (id[j] % i != 0) //下標
{
f = a[id[j]];
break;
}
printf("%d%c", f, " \n"[i == n]);
}
}
return 0;
}