Codeforces Round #639 (Div. 2)B. Card Constructions
程序员文章站
2022-06-02 22:17:40
...
题面
题意
定义用牌搭成金子塔,给定一个n,每建一个金字塔,n就减去相应的牌数,直到不能再建(每次都是建最高的金字塔)
思路
首先得出金字塔的递推公式,a(k)=a(k-1)+k-1+2k,把k层金字塔用的牌数存下来(保险起见开long long存),然后从一层的金字塔开始往上查找,直到找到找到第m层的金字塔要的牌数>=n,记下m,从第m层开始向下查找,如果该层的牌数<=n,n减去相应层数对应的牌数,能建成的金字塔数+1,直到n<=1。
代码
#include <bits/stdc++.h>
using namespace std;
long long zz[100005];
int main()
{
for (int i = 1; i <= 100000; i++)//预处理
{
if (i == 1)
zz[i] = 2;
else
zz[i] = zz[i - 1] + i - 1 + 2 * i;
}
int t;
cin >> t;
long long a;
while (t--)
{
int ans = 0;
int mm = 0;
cin >> a;
for (int i = 1; i <= 100000; i++)//找到牌数比n大的金字塔层数
{
if (zz[i] >= a)
{
mm = i;
break;
}
}
while (a > 1)
{
for (; mm >= 1; mm--)
{
if (zz[mm] <= a)
{
a -= zz[mm];
ans++;
break;
}
}
}
cout << ans << endl;
}
return 0;
}
推荐阅读
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #461 (Div. 2) B. Magic Forest(异或的性质)
-
Codeforces Round #663 (Div. 2) B. Fix You
-
Codeforces Round #658 (Div. 2) B. Sequential Nim
-
B. Power Sequence(数学+枚举)Codeforces Round #666 (Div. 2)
-
Codeforces Round #666 (Div. 2)B. Power Sequence(等比数列)
-
Codeforces Round #664 (Div. 2) B. Boboniu Plays Chess
-
Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何
-
Codeforces Round #258 (Div. 2) B. Sort the Array (模拟)
-
Codeforces Round #657 (Div. 2) B. Dubious Cyrpto(思维,数学)