Project Euler:Problem 48 Self powers

The series, 11 + 22 + 33 + ... + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

#include <iostream>
#include <string>
using namespace std;

string powe(int n)
{
	string s = "1";
	for (int i = 1; i <= n; i++)
	{
		int len = s.length();
		int flag = 0;
		for (int j = len - 1; j >= 0; j--)
		{
			int tp = (s[j] - '0') * n + flag;
			flag = tp / 10;
			s[j] = tp % 10 + '0';
		}
		if (flag != 0)
		{
			while (flag)
			{
				char a = flag % 10 + '0';
				s = a + s;
				flag /= 10;
			}
		}
	}
	return s;
}

string pl(string a, string b)   //加法
{
	string res = "";
	if (a.length() < b.length())
	{
		string t = a;
		a = b;
		b = t;
	}
	int lo = a.length();
	int sh = b.length();
	string s(lo - sh, '0');
	b = s + b;
	int flag = 0;
	for (int i = lo - 1; i >= 0; i--)
	{
		int tmp = a[i] + b[i] - '0' - '0' + flag;
		int low = tmp % 10;
		flag = tmp / 10;
		char a1 = low + '0';
		res = a1 + res;
	}

	if (flag != 0)
		res = "1" + res;
	return res;
}

int main()
{
	string res = "0";
	for (int i = 1; i <= 1000; i++)
	{
		string tmp = powe(i);
		res = pl(res, tmp);
	}
	int len = res.length();

	string ans = "";
	for (int i = len - 1 - 9; i < len; i++)
		ans = ans + res[i];
	cout << ans << endl;
	system("pause");
	return 0;
}
时间: 2024-08-03 04:39:15

Project Euler:Problem 48 Self powers的相关文章

Project Euler:Problem 29 Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical order, with any repeats removed

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 30 Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits: 1634 = 14 + 64 + 34 + 44 8208 = 84 + 24 + 04 + 84 9474 = 94 + 44 + 74 + 44 As 1 = 14 is not a sum it is not included. The sum of these numbers

Project Euler:Problem 18 Maximum path sum I

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 of the triangle below

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 58 Spiral primes

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18  5  4  3 12 29 40 19  6  1  2 11 28 41 20  7  8  9 10 27 42 21 22 23 24 25 26 43 44 45 46

Project Euler:Problem 39 Integer right triangles

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. {20,48,52}, {24,45,51}, {30,40,50} For which value of p ≤ 1000, is the number of solutions maximised? #include <iostre