Split the Number(思维)

You are given an integer x. Your task is to split the number x into exactly n strictly positive integers such that the difference between the largest and smallest integer among them is as minimal as possible. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

Each test case consists of a single line containing two integers x and n (1 ≤ x ≤ 109, 1 ≤ n ≤ 1000), as described in the statement above.

Output

For each test case, print a single line containing n space-separated integers sorted in a non-decreasing order. If there is no answer, print  - 1.

Example
Input
1
5 3
Output
1 2 2
Note

The strictly positive integers are the set defined as: . That is, all the integers that are strictly greater than zero: .

题目意思:对于T组输入输出,给你一个n将其分为k份,要求分成的份最大值和最小值相差最小。

解题思路:这道题很简单,我看了队友写的代码后发现这个题还挺有意思的,这里给出解释。

例如10分成4份,用10直接去整除4得到的是2,我们可以这样理解平均每一份是ans 2,即n/k得到的是一种平均的分配,但是这样还没有全部分完n,还会有余数m=n%k,因为m必然是比k小的数,也就可以理解为有m个分数需要在原来的ans上再加一个上1,这样得到的分配能够保证最大值和最小值相差最小。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int main()
 5 {
 6     int t,ans,m,i,k,n;
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         scanf("%d%d",&n,&k);
11         if(n<k)
12         {
13             printf("-1
");
14             continue;
15         }
16         ans=n/k;
17         m=n-ans*k;
18         for(i=0;i<k-m;i++)
19         {
20             printf("%d ",ans);
21         }
22         for(i=k-m;i<k;i++)
23         {
24 
25             if(i==k-1)
26             {
27                 printf("%d
",ans+1);
28             }
29             else
30             {
31                 printf("%d ",ans+1);
32             }
33         }
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/wkfvawl/p/9383557.html