Abood's birthday has come, and his n friends are aligned in a single line from 1 to n, waiting for their cookies, Abood has x cookies to give to his friends.
Here is an example to understand how Abood gives away the cookies. Suppose Abood has 4 friends and x cookies, then Abood will do the following:
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 4th friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- And so on until all the x cookies are given away.
Your task is to find how many cookies each friend will get. 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 ≤ 1018, 1 ≤ n ≤ 1000), in which x is the number of cookies Abood has, and n is the number of his friends.
Output
For each test case, print a single line containing n space-separated integers a1, ..., an, in which ai represents how many cookies the ithfriend got.
Example
input
Copy
1 5 3
output
Copy
2 2 1
题解:分配蛋糕,特判一下n==1和n==2的情况和n<=k,然后其他情况就是一种规律。
代码如下:
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define fori(a) for(ll i=0;i<a;i++)
#define forj(a) for(ll j=0;j<a;j++)
#define ifor(a) for(ll i=1;i<=a;i++)
#define jfor(a) for(ll j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define iFor(a,b,d) for(ll i=a;i<b;i+=d)
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const ll INF = 0x3f3f3f3f;
const ll inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const ll MOD = 1e9+7;
ll a[maxn];
int main()
{
ll t,n,k;
cin >> t ;
while(t--)
{
mem(a,0);
cin>>n>>k;
if( n<= k)
{
ifor(n)
a[i] = 1;
ifor(k)
cout << a[i] <<" ";
cout << endl;
}
else if (k == 1)
cout << n << endl;
else if(k == 2)
{
cout << n/2+n%2<<" "<<n/2 <<endl;
}
else
{
ll l=0,sum=2*(k-2)+2;
ll temp=-1,flag=0;
while(n)
{
if(n>=sum)
{
l=n/sum;
n%=sum;
}
else
{
if(n>k)
{
n-=k;
flag=1;
temp=n;
n=0;
}
else
{
temp=n;
n=0;
}
}
}
for(ll i=0; i<k; i++)
{
if(i==0||i==k-1)
a[i]=l;
else
a[i]=l*2;
}
if(flag==1&&temp!=-1)
{
for(ll i=0; i<k; i++)
{
a[i]++;
}
for(ll i=k-2;i>k-2-temp;i--)
a[i]++;
}
if(flag==0&&temp!=-1)
{
for(ll i=0; i<temp; i++)
{
a[i]++;
}
}
for(ll i=0; i<k; i++)
{
printf("%I64d",a[i]);
if(i!=k-1)
printf(" ");
else
printf("\n");
}
}
}
return 0;
}