B. Stairs(构造+规律寻找)Codeforces Round #671 (Div. 2)

127 篇文章 3 订阅

原题链接: https://codeforces.com/contest/1419/problems

在这里插入图片描述
测试样例

input
4
1
8
6
1000000000000000000
output
1
2
1
30

Note

In the first test case, it is possible to build only one staircase, that consists of 1 stair. It’s nice. That’s why the answer is 1.

In the second test case, it is possible to build two different nice staircases: one consists of 1 stair, and another consists of 3 stairs. This will cost 7 cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That’s why the answer is 2.

In the third test case, it is possible to build only one of two nice staircases: with 1 stair or with 3 stairs. In the first case, there will be 5 cells left, that may be used only to build a staircase with 2 stairs. This staircase is not nice, and Jett only builds nice staircases. That’s why in this case the answer is 1. If Jett builds a staircase with 3 stairs, then there are no more cells left, so the answer is 1 again.

题意: 楼梯的阶数不限,但如果对于 n n n阶楼梯,它的第 i i i列应该是由 i i i个方块叠加形成。如果n个楼梯由单元格组成的n个不相交的正方形覆盖,则称为nice楼梯。 现在给你 x x x个方块,问你能制造多少个不同的楼梯。

解题思路: 我们首先要知道nice楼梯的规律。我们想想:由于是要用 n n n个不相交的正方形覆盖,这就相当于我们在上一个nice正方形之后是新添加一个全新正方形使其分割,再将一个上一个nice楼梯放在上面。这样就达成了不相交的目的。 我们发现,由于有这样的规律,故nice楼梯的阶数为1,3,7,15。(因为我们再之后要添加一个大的正方形)。所以我们自然可以得知。那么我们 怎么知道 n n n阶楼梯的方块数呢?很简单,这就是一个等差数列。我们利用公式 i × ( i + 1 ) 2 \frac{i\times (i+1)}{2} 2i×(i+1)即可计算。OK,具体看代码。

AC代码

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e8;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t;
ll x;
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
		while(t--){
			cin>>x;
			int cnt=0;
			ll temp;
			for(ll i=1;;){
				temp=(i+1)*i/2;
				if(x<temp)break;
				x-=temp;
				cnt++;
				i=i*2+1;
			}
			cout<<cnt<<endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeZephyr

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值