HHU Boxes and Balls(找规律)

本文探讨了一个有趣的数学问题:如何通过特定的操作保持一组球的状态不变。具体来说,文章介绍了一种魔术操作,在这种操作下,一定数量的球可以使得操作前后的状态完全相同,并给出了寻找最大球数的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Tom’s friend Jerry, from KBW, 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. Jerry 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, Jerry 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, Jerry adds a new empty box and puts 1 ball from each of the existing 2 boxes into it; this creates one empty box, which Jerry 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? 

输入

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. 
Limits: • 1 ≤ T ≤ 100. • 1 ≤ N ≤ 1018 .

输出

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.  

样例输入

3
1
2
3

样例输出

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

提示

总结:
解决办法嘛就是找找规律啥的,最后发现只要某个状态是 1 2 。。。k-1 k那么进行一次操作必然还会回到这个状态,所以答案自然是找到最大的正整数k使得k(k+1)/2<=n ,然后k(k+1)/2就是答案啊
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef unsigned long long ull;
ull n;
int main()
{
	//freopen("input.txt","r",stdin);
	int T;
	scanf("%d",&T);
	for(int t=1;t<=T;t++)
	{
		scanf("%lld",&n);
		ull k=(sqrt(1+8*n)-1.0)/2.0;
		k=k*(k+1)/2;
		printf("Case #%d: %lld\n",t,k);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值