PE其他解题报告请参考这里,本题答案在留言首条
Triangular, pentagonal, and hexagonal
Problem 45
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
Triangle
T
n
=
n
(
n
+
1
)
/
21
,
3
,
6
,
10
,
15
,
.
.
.
Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
Tn=n(n+1)/21,3,6,10,15,...
Pentagonal
P
n
=
n
(
3
n
−
1
)
/
21
,
5
,
12
,
22
,
35
,
.
.
.
Pn=n(3n−1)/2 1, 5, 12, 22, 35, ...
Pn=n(3n−1)/21,5,12,22,35,...
Hexagonal
H
n
=
n
(
2
n
−
1
)
1
,
6
,
15
,
28
,
45
,
.
.
.
Hn=n(2n−1) 1, 6, 15, 28, 45, ...
Hn=n(2n−1)1,6,15,28,45,...
It can be verified that
T
285
=
P
165
=
H
143
=
40755.
T_{285} = P_{165} = H_{143} = 40755.
T285=P165=H143=40755.
Find the next triangle number that is also pentagonal and hexagonal.
题意:
让你寻找下一个彼此相等的三角数,五角数,六角数。
分析:
我们先打表后,然后枚举三角数即可,二分的查找,当然我们也可以直接来枚举,复杂度不会差很多(枚举会快些)
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn = 1e7 + 10;
ll tri[maxn];
ll pen[maxn];
ll Hex[maxn];
int main(){
for (int i = 1; i < maxn; i++) {
tri[i] = i * 1ll *(i + 1) / 2;
pen[i] = i * 1ll *(3 * i - 1) / 2;
Hex[i] = i * 1ll *(2 * i - 1);
}
int now = 286;
while (now < maxn) {
if (*lower_bound(pen + 1, pen + maxn, tri[now]) == tri[now] &&
*lower_bound(Hex + 1, Hex + maxn, tri[now]) == tri[now]) {
cout << tri[now] << endl;
break;
}
now++;
}
return 0;
}
阅读好习惯:点赞 + 收藏~