B - Friends and Cookies Gym - 101810B

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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值