B - Friends and Cookies (思维题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BHliuhan/article/details/81947600

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
题意:第一行中每个人都有饼干,然后第二行就倒着往回走,最后一个人没有饼干,走到第一个位置后给完饼干,再往回走,第一个人又没有饼干,这样干说,不知道以后自己再看会不会看懂,关键点就是到头之后头只走一遍,立刻返回,不会再重复赋值;
我这代码有点low,没关系,比赛时A了,哈哈哈!

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long long int a[10001];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long int x,n;
        memset(a,0,sizeof(a));
        scanf("%lld%lld",&x,&n);
        if(x<n)
        {
            for(long long int i=1; i<=x; i++)
            {
                a[i]++;
            }
        }
        else
        {
            if(n==1)
            {
                a[1]+=x;
            }
            else
            {
                for(long long int i=1; i<=n; i++)
                {
                    a[i]++;
                }
                long long int m=(x-n)/(n-1);
                if(m==0)
                {
                    long long int q=(x-n)%(n-1);
                    long long int p=n-1;
                    for(long long int i=1; i<=q; i++)
                    {
                        a[p--]++;
                    }
                }
                else
                {
                    if(m%2==0)
                    {
                        a[1]+=m/2;
                        a[n]+=m/2;
                        for(long long int i=2; i<=n-1; i++)
                        {
                            a[i]+=m;
                        }
                    }
                    else
                    {
                        a[1]+=m/2+1;
                        a[n]+=m/2;
                        for(long long int i=2; i<=n-1; i++)
                        {
                            a[i]+=m;
                        }
                    }
                    double w=1.0*(x-n)/(n-1);
                    if(w-m!=0)
                    {
                        long long int r=(x-n)%(n-1);
                        if(m%2==0)
                        {
                            long long int p=n-1;
                            for(long long int i=1; i<=r; i++)
                            {
                                a[p--]++;
                            }
                        }
                        else
                        {
                            long long int p=2;
                            for(long long int i=1; i<=r; i++)
                            {
                                a[p++]++;
                            }
                        }
                    }
                }
            }
        }
        for(long long int i=1; i<=n; i++)
        {
            if(i==1)printf("%lld",a[i]);
            else printf(" %lld",a[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/BHliuhan/article/details/81947600
今日推荐