This is a useful problem for searching substrings within a document. Hashing can be applied to search for a substring efficiently. In this problem, calculating the product is required, but special care must be taken to handle overflow and multiplication by zero.
The product of the digits in a number with a base of 13 can yield a result as large as \(9^{13} = 2,541,865,828,329\). Since a 32-bit integer can represent values up to around 4 billion, a 64-bit integer is necessary for handling these larger values.
As given in the problem example, the number of consecutive digits to be multiplied is 4.
For example, consider the 10-digit number 7316717653.
1st group: \(7 \times 3 \times 1 \times 6\)
2nd group: \(3 \times 1 \times 6 \times 7\)
3rd group: \(1 \times 6 \times 7 \times 1\)
...
We need to multiply 3 numbers at a time for each sequence. For a 10-digit number, we perform the multiplication \((10 - 3) \times 3 = 21 \) times. This means we’ll multiply a total of 21 numbers.
Let’s break down the multiplication:
For the 10-digit number 7316717653, the groups of three digits to be multiplied are:
7 | 3 | 1 | 6 | ||||
---|---|---|---|---|---|---|---|
3 | 1 | 6 | 7 | ||||
1 | 6 | 7 | 1 | ||||
6 | 7 | 1 | 7 | ||||
7 | 1 | 7 | 6 |
There are 3 numbers that get multiplied repeatedly in each step. For a 4-number sequence, only 3 multiplications are duplicated, while for a 13-number sequence, 12 multiplications are duplicated.
To optimize the process, I start with the full multiplication for the first group.
1st group: \( 7 \times 3 \times 1 \times 6 \)
For the 2nd group, I reuse the previous result:
2nd group: \(\text{(previous result)} \times 7 / 7\)
For the 3rd group, I continue using the previous result:
3rd group: \(\text{(previous result)} \times 1 / 3\)
...
By reusing the previous product and adjusting with the incoming and outgoing digits, I save 1 multiplication for each step after the first. When calculating for a 13-digit sequence, I can save up to 9 multiplications each time.
Additionally, there’s the issue of zeros. If a zero appears in the sequence, the entire product becomes zero. To handle this, I maintain a zero count variable. If the zero count is greater than 0, I skip checking for the maximum product, since the result is guaranteed to be zero.
My codes are below.
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
typedef int64_t int64;
int main()
{
char *s = "73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
"53697817977846174064955149290862569321978468622482"
"83972241375657056057490261407972968652414535100474"
"82166370484403199890008895243450658541227588666881"
"16427171479924442928230863465674813919123162824586"
"17866458359124566529476545682848912883142607690042"
"24219022671055626321111109370544217506941658960408"
"07198403850962455444362981230987879927244284909188"
"84580156166097919133875499200524063689912560717606"
"05886116467109405077541002256983155200055935729725"
"71636269561882670428252483600823257530420752963450";
char *h = s, *t = s;
int64 max, c = 1, a, b, v = 0;
for( int i = 0 ; i < 13 ; i++, t++ )
{
b = *t - '0';
if( b != 0 ) c *= b;
else v++;
}
if( v == 0 ) max = c;
else max = 0;
while( *t )
{
a = *h++ - '0';
b = *t++ - '0';
if( a != 0 ) c /= a; else v--;
if( b != 0 ) c *= b; else v++;
if( v == 0 && c > max ) max = c;
}
printf("Ans = %jd\n", max);
}
Pointer variable h is pointing the number to be erased, and pointer variable t is pointing the number to be added. Variable max is saving current maximum product value, c is current phase product value, v saves how many zero's.
'Programming > Project Euler' 카테고리의 다른 글
9, Project Euler #9 : Special Pythagorean triplet (0) | 2015.04.20 |
---|---|
Current my project euler status. (0) | 2015.04.16 |
7. Project Euler #7 : 10001st prime (0) | 2015.02.27 |
6. Project Euler #6 : Sum square difference (0) | 2015.02.13 |
5. Project Euler #5 : The smallest number that can be divided by numbers below 20. (0) | 2015.01.29 |