题目链接:https://vjudge.net/contest/371907#problem/B
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length kk have the same sum. A subarray of an array is any sequence of consecutive elements.
Phoenix currently has an array aa of length nn. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 11 and nn inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.
Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤50) — the number of test cases.
The first line of each test case contains two integers n and k (1≤k≤n≤100).
The second line of each test case contains n space-separated integers (1≤ai≤n) — the array that Phoenix currently has. This array may or may not be already beautiful.
Output
For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.
The first line should contain the length of the beautiful array m (n≤m≤104). You don’t need to minimize m.
The second line should contain m space-separated integers (1≤bi≤n) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array aa. You may print integers that weren’t originally in array a.
If there are multiple solutions, print any. It’s guaranteed that if we can make array aa beautiful, we can always make it with resulting length no more than 104
Input
4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2
Output
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
翻译:
输入两个数n和k,接下来有一个长度为n的数组,1<=ai<=n。问能否添加一些范围在1~n之间的数,使得数组长度为k的子序列的和都相等。否则输出-1。
例如:
4 2
1 2 2 1
变成
5
1 2 1 2 1
长度为2的子序列的和都为3
分析:
要使一个数组对于某些k来说是漂亮的,这个数组必须以k为周期。
如果数组a中存在多于k个不同的数字,则没有答案,我们输出-1(因为数组不能以k为周期)。
如果数组a中不同的数字的数量小于k我们将添加一些数字,直到不同数字的数量为k。我们可以把这个列表打印n次。我们的数组b的长度是n*k。
n和k的取值范围为1≤k≤n≤100,最大不超过104
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e2+10;
const int M=1e4+10;
int a[N],b[N],index;
bool book[N];
void init()
{
index=0;
memset(b,0,sizeof(b));
memset(book,false,sizeof(book));
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
int n,k;
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int length=unique(a,a+n)-a;
if(length>k)
{
printf("-1\n");
continue;
}
for(int i=0; i<length; i++)
{
b[index++]=a[i];
book[a[i]]=true;
}
if(index<k)
{
for(int i=1; i<=n; i++)
{
if(!book[i])
{
book[i]=true;
b[index++]=i;
if(index==k)
break;
}
}
}
printf("%d\n",n*k);
for(int i=0; i<n; i++)
{
for(int j=0; j<index; j++)
printf("%d ",b[j]);
}
printf("\n");
}
return 0;
}