UVALive - 7500 Boxes and Balls(思路)

描述

Little Tom’s friend Jack just showed him a great magic trick. At the
beginning of the trick, there is one box on the ground with some
number of balls in it. Jack then performs this operation over and over
again:
1. put a new empty box down on the ground
2. move one ball from each other box into that new empty box
3. remove any boxes that are now empty
4. sort the boxes in nondecreasing order by the number of balls in them Tom noticed that it is possible for this operation to leave the
state of the boxes and balls unchanged! For example: • Suppose that at
the beginning of the trick, the one box contains 3 balls. • In the
first operation, Jack adds a new empty box, puts 1 ball from the
existing box into it, and sorts the boxes, so after that operation,
there will be 2 boxes on the ground, one with 1 ball and one with 2
balls. • In the second operation, Jack adds a new empty box and puts 1
ball from each of the existing 2 boxes into it; this creates one empty
box, which Jack removes, and then he sorts the boxes. So there are 2
boxes on the ground, one with 1 ball and one with 2 balls. But this is
exactly the state that was present before the second operation! Tom
thought about the trick some more, and realized that for some numbers
of balls, it is not possible for the operation to leave the state
unchanged. For example, if there are 2 balls at the beginning, then
after one operation, there will be two boxes with 1 ball each, and
after 2 operations, there will be one box with 2 balls, and so on,
alternating between these two states forever. Tom looked around in his
room and found infinitely many empty boxes, but only N balls. What is
the maximum number of those balls that he could use to perform this
trick, such that one operation leaves the state unchanged?

Input

The first line of the input gives the number of test cases, T. T lines
follow. Each line consist of one integer N, the number of balls Tom
could find.

Output

For each test case, output one line containing ‘Case #x: y’, where x
is the test case number (starting from 1) and y is the maximum number
of balls that Tom could use to perform the trick, as described above.
Limits: • 1 ≤ T ≤ 100. • 1 ≤ N ≤ 1018 . Note: The trick can be
performed with 1 ball or 3 balls, but not with 2 balls. So, for Case
#2, Tom can use at most 1 of the 2 balls.

Sample Input

3
1
2
3

Sample Output

Case #1: 1
Case #2: 1
Case #3: 3

思路

先说题意,你有n个球,然后你要拿出其中一些球(设为x)放在你面前的一个盒子里,然后你又拿出一个盒子,从第一个盒子里面拿出1个球,放进去并且按照盒子里面球的数量从小到大对盒子排序,然后又拿出一个盒子,从以前的旧盒子里面各拿出一个球放进去,然后排序。当出现题目中规定的这种情况使拿盒子放球可以无限次的进行操作,问当拿出多少个球时,满足题意的这种情况

我们可以枚举一下前几个满足条件的这个数,分别是1,3,6,10,我们可以发现,这就是一个公差为1的等差数列,所以我们求出满足前i项和就行了。

公差为1的等差数列求和公式是: n(n+1)2 n ( n + 1 ) 2 ,那么题目求的n的取值就应该在 2(n+1) 2 ∗ ( n + 1 ) 这个区间之间,枚举符合条件的 i(i+1)2 i ∗ ( i + 1 ) 2 就可以了

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t,n,q=1,tmp;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        printf("Case #%lld: ",q++);
        tmp=n;
        n=sqrt(n*2)+10;
        ll i;
        for(i=n; i>=1; i--)
            if(i*(i+1)/2<=tmp)
                break;
        cout<<i*(i+1)/2<<endl;
    }
    return 0;
}
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页