Friends and Cookies Gym - 101810B ——数学,找规律

19 篇文章 1 订阅

原题链接:https://codeforces.com/gym/101810/problem/B

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:

  1. Give a cookie to the 1st friend.
  2. Give a cookie to the 2nd friend.
  3. Give a cookie to the 3rd friend.
  4. Give a cookie to the 4th friend.
  5. Give a cookie to the 3rd friend.
  6. Give a cookie to the 2nd friend.
  7. Give a cookie to the 1st friend.
  8. Give a cookie to the 2nd friend.
  9. 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?

解决思路:

假如有5个人,x的排列一定是这样的:

1 2 3 4 5

1 2 3 4 

 2 3 4 5

1 2 3 4 

.....

在x足够大的时候,第一排一定是5个,在剩下的x-5中,肯定是按照每排4(n-1)排列的,所以可以剩下的整行,最后余下的再做特殊处理。

注意,第一个人分到的饼干和最后一个人分到的饼干要特别计算。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const ll N = 1e3+ 20;
ll x;
ll n;
int main()
{
     int T;
     cin>>T;
     while(T--)
     {

          cin>>x>>n;
          if(x<=n)
          {
               for(int i=1; i<=n; i++)
               {
                    ll ans = 0;
                    ll ans1 = 1;
                    if(i<=x)
                         cout<<ans1<<' ';
                    else
                         cout<<ans<<' ';
               }
               cout<<endl;
          }
          else
          {
               if(n==1)//如果是n=1,那么n-1=0,不能够做分母。显然当n=1时,这个人分走所有饼干
               {
                    cout<<x<<endl;
               }
               else
               {

               x = x-n;
               ll temp = x / (n-1);
               x = x - temp * (n-1);
               if(temp%2==1)
               {
                    ll ans1 = 1 + temp / 2 + 1;
                    cout<<ans1<< ' ';
                    for(int i=2; i<= x+1; i++)
                    {
                         ll ans = temp + 1 +1;
                         cout<<ans<<' ';
                    }
                    for(int i=x+2; i<=n-1; i++)
                    {
                         ll ans = temp + 1;
                         cout<<ans<<' ';
                    }
                    ll ans = 1 + temp/ 2;
                    cout<<ans<<endl;
               }
               else
               {
                    ll ans1 = 1 + temp / 2;
                    cout<<ans1<<' ';
                    for(int i=2; i<n-x; i++)
                    {
                         ll ans = temp + 1;
                         cout<<ans<<' ';
                    }
                    for(int i=n-x; i<n; i++)
                    {
                         ll ans = temp + 1 +1;
                         cout<<ans<<' ';
                    }
                    ll ans = temp / 2 + 1;
                    cout<<ans<<endl;
               }
          }
          }

     }

     return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值