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
题意:
给出下标从1开始,长度为n的A数组,求出下标从2开始,长度为n-1的B数组。满足 Bi =max Aj (i不是j的因子)。
想法:
直接暴力。
将A数组由大到小排序后,外层循环B的下标,内层循环A的下标,只要是因子,就将A的值赋给B。
但是要用结构体稍微优美一下,存储A的数值和位置。
#include<bits/stdc++.h>
using namespace std;
int b[110000];
struct p
{
int num,pos;
}a[110000];
int cmp(p x,p y)
{
return x.num>y.num;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof a);
memset(b,0,sizeof b);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i].num);
a[i].pos=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].pos%i)
{
b[i]=a[j].num;
break;
}
}
}
for(int i=2;i<=n;++i)
{///这里注意处理行末空格的问题
if(i!=2)
printf(" ");
printf("%d",b[i]);
}
printf("\n");
}
return 0;
}
ps:刚开始想复杂了,差点用质因数分解来做o(╯□╰)o

374

被折叠的 条评论
为什么被折叠?



