Project Euler:Problem 61 Cyclical figurate numbers
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.
- 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).
- 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.
- 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的更多相关文章
- 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 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
*题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数) public class 第三十九题按条件计算 ...
- 文件默认权限umask掩码
umask命令 作用:用于显示.设置文件的缺省权限 格式:umask [-S] -S表示以rwx形式显示新建文件缺省权限 系统的默认掩码是0022 文件创建时的默认权限 = 0666 - umas ...
- 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数
<?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...
- 对数组内容使用了json_encode返回汉字内容返回了空值
如果使用json_encode对数组进行转成JSON字符串时候,发现汉字的全部为空,这样可以说明的一点是你的页面上用的一定不是UTF8编码,在PHP手册中对json_encode中待编码的值已经说明所 ...
- HTML5新增的非主体元素header元素、footer元素、hgroup元素、adress元素
---恢复内容开始--- header header元素是一种具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,但是也可以包含其他内容,例如数据表格.搜索表单或相关的lo ...
- CSS3---关于文本
1.text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 2.但是text-overflow只是用来说明文字溢出时用什么方式显示,要实现溢出时产生省略号的效果,还须定 ...
- InnoDB体系架构总结(一)
缓冲池: 是一块内存区域,通过内存的速度来弥补磁盘速度较慢对数据库性能的影响.在数据库中读取的页数据会存放到缓冲池中,下次再读取相同页的时候,会首先判断该页是否在缓冲池中.对于数据库中页的修改操 ...
- 虚拟机如何设置静态IP
一.本机环境 Mac.VMware Fusion 10, CentOS6.8 二.设置静态IP地址 1.选择网络连接模式,选择NAT模式 注意: 1)必须要选择NAT模式,否则你的虚拟机与主机始终会在 ...
- 【HIHOCODER 1048】 状态压缩·二
描述 历经千辛万苦,小Hi和小Ho终于到达了举办美食节的城市!虽然人山人海,但小Hi和小Ho仍然抑制不住兴奋之情,他们放下行李便投入到了美食节的活动当中.美食节的各个摊位上各自有着非常多的有意思的小游 ...
- 简单的发红包的PHP算法
假设有有10元钱 ,发给10个人.保证每个人都有钱拿,最少分得0.01.我们最先想到的肯定就是随机.0.01-10随机.但是会出现第一个人就分得9.99的情况.下面就没人可分了.然后就是我的错误思路 ...