原题链接: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:
- 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?
解决思路:
假如有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;
}