D - Different Sums URAL - 2065

Description

Alex is a very serious mathematician and he likes to solve serious problems. For example, this problem.
You are to construct an array of  n integers in which the amount of different integers is not less than  k. Among all such arrays you have to construct the one with the minimal amount of different sums on non-empty subarrays. In other words, lets compute the sums of every non-empty subarray and remove repeating sums. You have to minimize the number of remaining sums.

Input

In the only line of input there are two integers nk (1 ≤ k ≤ n ≤ 500), separated by a space.

Output

Print n integers separated by spaces — the answer for the problem. All the numbers must not be greater than 10 6 by absolute value. It is guaranteed that there exists an optimal solution with numbers up to 10 5by absolute value. If there are multiple possible answers, you may print any of them.

Sample Input

1 1
Sample Output

-987654
Sample Input

3 2
Sample Output

0 7 0

Notes

Let’s take a closer look on the second sample. We will denote the sum on the segment [  lr] by  sumlr) (elements are numbered starting with 1).  sum(1, 1) =  sum(3, 3) = 0,  sum(1, 2) =  sum(1, 3) =  sum(2, 2) = sum(2, 3) = 7, so there are only two different sums.

题解:l,r即为数组元素的位置,都从1开始,给你n,代表有一个1~n的数组,有k个不同的数,然后遍历一遍,使其中任意元素和的种类最少,即存在n-k个0,其他随机选非0数,但要注意两个数互为相反数时求和种类最少。

代码如下:

#include
#include
#include
#include
#include
#include
using namespace std;
const int MAX = 1e5 + 10;
typedef long long LL;
int a[MAX];
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=0;i<=n-k;i++)
printf("0 ");
int s=1,qaq=0;
for(int i=n-k+1;i {
if(i!=n-1)
{
if(qaq==0)
{
qaq=1;
printf("%d ",s);
}
else
{
qaq=0;
printf("%d ",-s);
s++;
}
}
else
if(qaq==0)
{
printf("%d",s);
}
else
{
printf("%d",-s);
}
}
    return 0;
}

你可能感兴趣的:(思维)