B - Friends and Cookies Gym - 101810B
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 ith friend got.
Example
Input
1
5 3
Output
2 2 1
Hint
本题主要是判断每个人所能得到的饼干数,但要注意人数与蛋糕数的关系
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
using namespace std;
#define inf 0x3f3f3f3f
int main()
{
int t;
long long a[1010];
long long int n,m,i,j,z;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
if(n<m)//如果人数比饼干数多
{
for(i=1; i<=n; i++)
cout<<"1"<<" ";
for(i=n+1; i<=m; i++)
{
if(i==m)
cout<<"0"<<endl;
else
cout<<"0"<<" ";
}
}
else if(m==1)cout<<n<<endl;//当人数为1时,饼干都是他的了!!!
else if(m>=2)//当人数大于1时
{
memset(a,0,sizeof(a));
for(i=1; i<=m; i++)//先每个人都分一个,饼干数减少
{
a[i]++;
n--;
}
z = n/(m-1);//判断分完一次后,最多可以每个人都分几块
j = n%(m-1);//剩余不足一次的个数
if(z%2==0)//能够来回分得次数为偶数或0时
{
for(i=2; i<=m-1; i++)
a[i]+=z;
a[1]+=z/2;
a[m]+=z/2;
i=m-1;
while(j)
{
a[i]++;
i--;
j--;
}
}
else
{
for(i=2; i<=m-1; i++)
a[i]+=z;
a[1]+=(z+1)/2;
a[m]+=z/2;
i=2;
while(j)
{
a[i]++;
i++;
j--;
}
}
for(i=1; i<=m; i++)
{
if(i==m)
cout<<a[i]<<endl;
else
cout<<a[i]<<" ";
}
}
}
return 0;
}