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
题解:
多校的题有点难理解啊。
i∤j’表示j%i!=0
排序优化一下,以数据大小排序,同时记录索引值,从2开始枚举,计算b数组的值,O(n)扫排序后的a数组,当遇到第一个j%i!=0,记录即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100000+10;
struct node
{
int id;
LL data;
};
bool cmp(node q,node p)
{
return q.data>p.data;
}
int main()
{
int T;
cin>>T;
LL n;
node a[maxn];
vector<LL> b;
while(T--)
{
cin>>n;
LL tmp;
for(int i=1;i<=n;i++)
{
scanf("%lld",&tmp);
a[i].data=tmp;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[j].id%i!=0)
{
b.push_back(a[j].data);
break;
}
}
}
int siz = b.size();
for(int i=0;i<siz-1;i++)
{
printf("%lld ",b[i]);
}
printf("%lld",b[siz-1]);
cout<<endl;
b.clear();
}
return 0;
}