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.

  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也是能搞出来的。

时间: 2024-10-14 12:47:21

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 triangle numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... By converting each letter in a word to a number corresponding to its alphabetical position and

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

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 natural numbers, {a1, a2, ... , ak} is called a product-sum number: N = a1 + a2 + ... + ak = a1 × a2 × ... × ak. For example, 6 = 1 + 2 + 3 = 1 × 2 × 3. Fo

Project Euler:Problem 55 Lychrel numbers

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Al

Project Euler:Problem 21 Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 67 Maximum path sum II

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (righ