Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

Triangle   P3,n=n(n+1)/2   1, 3, 6, 10, 15, ...
Square   P4,n=n2   1, 4, 9, 16, 25, ...
Pentagonal   P5,n=n(3n−1)/2   1, 5, 12, 22, 35, ...
Hexagonal   P6,n=n(2n−1)   1, 6, 15, 28, 45, ...
Heptagonal   P7,n=n(5n−3)/2   1, 7, 18, 34, 55, ...
Octagonal   P8,n=n(3n−2)   1, 8, 21, 40, 65, ...

The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

  1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
  2. Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
  3. This is the only set of 4-digit numbers with this property.

Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented
by a different number in the set.

又暴力破解了一次ㄟ( ▔, ▔ )ㄏ

一開始没看清题意,我以为这些数依次是满足triangle, square, pentagonal, hexagonal, heptagonal, and octagonal。结果发现无解┑( ̄Д  ̄)┍

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <time.h>
using namespace std; int triangle[100];
int pentagonal[10000];
int hextagonal[10000];
int heptagonal[10000];
int octagonal[10000];
int tri_count = 0; void getTriangle()
{
int count = 0;
for (int i = 1; i <= 200; i++)
{
int num = i*(i + 1) / 2;
if (num >1000&&num<10000)
triangle[count++] = num;
}
tri_count = count;
} bool isSqure(int n)
{
int i = sqrt(n);
if (i*i == n&&n>1000&&n<10000)
return true;
return false;
} void getPentagonal()
{
for (int i = 1; i <= 200; i++)
{
int num = i*(3 * i - 1) / 2;
if (num > 1000 && num < 10000)
pentagonal[num] = 1;
}
} bool isPentagonal(int n)
{
if (pentagonal[n] == 1)
return true;
return false;
} void getHexagonal()
{
for (int i = 1; i <= 200; i++)
{
int num = i*(2 * i - 1);
if (num>1000 && num < 10000)
hextagonal[num] = 1;
}
} bool isHexagonal(int n)
{
if (hextagonal[n] == 1)
return true;
return false;
} void getHeptagonal()
{
for (int i = 1; i <= 200; i++)
{
int num = i*(5 * i - 3) / 2;
if (num > 1000 && num < 10000)
heptagonal[num] = 1;
}
} bool isHeptagonal(int n)
{
if (heptagonal[n] == 1)
return true;
return false;
} void getOctagonal()
{
for (int i = 1; i <= 200; i++)
{
int num = i*(3 * i - 2);
if (num > 1000 && num < 10000)
octagonal[num] = 1;
}
} bool isOctagonal(int n)
{
if (octagonal[n] == 1)
return true;
return false;
} bool(*figurate[5])(int) = { isSqure, isPentagonal, isHexagonal, isHeptagonal, isOctagonal }; vector<int> GetRandomSequence()
{
unordered_map<int, int>tab;
vector<int>res;
int num;
for (int i = 0; i < 5; i++)
{
do{
num = rand() % 5;
} while (tab.find(num) != tab.end());
tab.insert(make_pair(num, 1));
res.push_back(num);
}
return res;
} int check()
{
int sum = 0;
srand((int)time(0));
vector<int>rs = GetRandomSequence();
for (int i = 0; i < tri_count; i++)
{
int a = triangle[i] / 100;
int b = triangle[i] % 100;
for (int s = 10; s <= 99; s++)
{
if ((*figurate[rs[0]])(b * 100 + s))
{
for (int p = 10; p <= 99; p++)
{
if ((*figurate[rs[1]])(s * 100 + p))
{
for (int hx = 10; hx <= 99; hx++)
{
if ((*figurate[rs[2]])(p * 100 + hx))
{
for (int hp = 10; hp <= 99; hp++)
{
if ((*figurate[rs[3]])(hx * 100 + hp))
{
if ((*figurate[rs[4]])(hp * 100 + a))
{
sum = triangle[i] + b * 100 + s + s * 100 + p + p * 100 + hx + hx * 100 + hp + hp * 100 + a;
return sum;
}
}
}
}
}
}
}
}
}
}
return -1;
} int main()
{
memset(pentagonal, 0, sizeof(pentagonal));
memset(hextagonal, 0, sizeof(hextagonal));
memset(heptagonal, 0, sizeof(heptagonal));
memset(octagonal, 0, sizeof(octagonal)); getTriangle();
getPentagonal();
getHexagonal();
getHeptagonal();
getOctagonal(); int flag;
while (true)
{
flag = check();
if (flag != -1)
break;
} cout << flag << endl; system("pause");
return 0;
}

把那个随机生成全排列换成next_permutation也是能搞出来的。

Project Euler:Problem 61 Cyclical figurate numbers的更多相关文章

  1. Project Euler:Problem 42 Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  2. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  3. Project Euler:Problem 88 Product-sum numbers

    A natural number, N, that can be written as the sum and product of a given set of at least two natur ...

  4. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  5. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  6. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  7. Project Euler:Problem 28 Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  8. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

  9. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

随机推荐

  1. VIO第二讲_allen方差工具

    1,首先,安装ceres依赖项,见高博14讲116页,然后下载编译安装ceres: git clone https://github.com/ceres-solver/ceres-solver cd ...

  2. phpstudy里升级mysql版本到5.7

    phpstudy里没有地方可以设置mysql数据库,很多人都疑惑在phpstudy里怎么升级mysql数据库版本,本文就教你如何在phpstudy中升级mysql的版本. PhpStudy集成环境中的 ...

  3. (6) openssl passwd(生成加密的密码)

    该伪命令用于生成加密的密码 [root@docker121 ssl]# man -f passwd passwd (1) - update user's authentication tokens p ...

  4. Python之微信-微信好友头像合成

    仔细看下图,你的头像就藏在里面哦!!! 有没有犯密集恐惧症?这并不震撼,如果你有 5000 位好友的话,做出来的图看着会更刺激些. 看完了图,你可能想知道这个图咋做出来的,不会是我闲着无聊把把好友头像 ...

  5. VS2013环境下Boost库配置

    序言 最近了解各大互联网公司的校招要求,发现了解Boost程序库也是不可或缺的一部分~ 于是,决定潜心研究下,这个准标准库~ 首先,在官网下载boost的最新版本Boost 1.59.0,这是当前的最 ...

  6. c++ 高精度 加减乘除 四则运算 代码实现

    很久以前写的啦 记得写了好久好久一直卡在特例的数据上面 想起都心塞 那时候变量和数组的取名对我来说简直是个大难题啊 完全乱来 abcdef就一路排下来 自己看的时候都搞不懂分别代表什么 好在后来英语学 ...

  7. The Coco-Cola Store

    UVA11877 The Coco-Cola Store Once upon a time, there is a special coco-cola store. If you return thr ...

  8. zoj 2727 List the Books

    List the Books Time Limit: 2 Seconds      Memory Limit: 65536 KB Jim is fond of reading books, and h ...

  9. 【ITOO 3】.NET 动态建库建表:实用EF框架提供的codeFirst实现动态建库

    导读:在上篇博客中,介绍了使用SQL字符拼接的方式,实现动态建库建表的方法.这样做虽然也能够实现效果,但是,太麻烦,而且,如果改动表结构,字段的话,会对代码修改很多.但是EF给我们提供了一种代码先行的 ...

  10. linux shell symbolic link & soft link, symbol link, link

    linux shell symbolic link symbolic link https://en.wikipedia.org/wiki/Ln_(Unix) https://stackoverflo ...