Project Euler problem 68

题意需要注意的一点就是,

序列是从外层最小的那个位置顺时针转一圈得来的。并且要求10在内圈

所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <set>
#include <vector>
#include <map>
#define MAXN 111
#define MAXM 55555
#define INF 1000000007
using namespace std;
int a[12];
int sum[6];
string ans, tmp;
int main()
{
    int n = 10;
    int first = 1;
    ans = "", tmp = "";
    for(int i = 1; i <= n; i++) a[i] = i;
    do {
        int pos = 1;
        for(int i = 1; i <= 5; i++) {
            if(a[i] < a[pos]) pos = i;
            int z = i + 6;
            if(z > 10) z = z - 5;
            sum[i] = a[i] + a[i + 5] + a[z];
        }
        if(pos != 1) continue;
        int flag = 1;
        for(int i = 2; i <= 5; i++)
            if(sum[i] != sum[i - 1]) flag = 0;
        if(sum[1] != sum[5]) flag = 0;
        for(int i = 6; i <= 10; i++) {
            if(a[i] == 10) flag = 0;
        }
        tmp = "";
        if(flag) {
            for(int i = 1; i <= 5; i++) {
                int z = i + 6;
                if(z > 10) z = z - 5;
                tmp += (a[i] + 'a');
                tmp += (a[i + 5] + 'a');
                tmp += (a[z] + 'a');
            }
            if(!first) {
                if(ans < tmp) ans = tmp;
            } else {
                ans = tmp;
                first = 1;
            }

        }
    }while(next_permutation(a + 1, a + n + 1));
    for(int i = 0; i < 15; i++) printf("%d", ans[i] - 'a');
	return 0;
}
时间: 2024-10-20 16:10:48

Project Euler problem 68的相关文章

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagore

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler:Problem 12 Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the fir

Project Euler: Problem 9 Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 首先联立式子,消除c,得到关于ab的等式5

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be

Project Euler Problem 675

ORZ foreverlasting聚聚,QQ上问了他好久才会了这题(所以我们又聊了好久的Gal) 我们先来尝试推导一下\(S\)的性质,我们利用狄利克雷卷积来推: \[2^\omega=I\ast|\mu|\] 这个很好理解吧,考虑一下它的组合意义即可 然后两边同卷上\(I\)有: \[2^\omega \ast I=I\ast I\ast |\mu|=d\ast |\mu|\] 后面还是同样,考虑\(d\ast |\mu|\)的组合意义,一正一反的情况下其实就是\(d(n^2)\) 因此我们

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' 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