Project Euler Problem 45: Triangular, pentagonal, and hexagonal【二分】【暴力】

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(3n1)/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(2n1)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;
}

阅读好习惯:点赞 + 收藏~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值