Google code jam 2008 R1A - Numbers

这是Google code jam 2008 R1A的第三道题,关于无理数n次幂的问题。这道题巧妙的运用了数论的原理,对巩固指数多项式的相关数学知识、快速求指数法等算法、中国余数理论等,有很强的参考价值和复习作用。最后给出源码。

Problem

In this problem, you have to find the last three digits before the decimal point for the number (3 + √5)n.

For example, when n = 5, (3 + √5)5 = 3935.73982... The answer is 935.

For n = 2, (3 + √5)2 = 27.4164079... The answer is 027.

Input

The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n.

Output

For each input case, you should output:

Case #X: Y
where X is the number of the test case and Y is the last three integer digits of the number (3 + √5) n. In case that number has fewer than three integer digits, add leading zeros so that your output contains exactly three digits.

 

Limits

1 <= T <= 100

Small dataset

2 <= n <= 30

Large dataset

2 <= n <= 2000000000

Sample


Input
 

Output
 
2
5
2
Case #1: 935
Case #2: 027


Analysis

This problem with a simple statement was in fact one of the hardest in Round 1. The input restrictions for the small input were chosen so that straightforward solutions in Java or Python, which have arbitrary-precision numbers, would fail. The trick was calculating √5 to a large enough precision and not using the default double value. It turned out that using the Windows calculator or the UNIX bc tool was enough to solve the small tests as well.

Solving the large tests was a very different problem. The difficulty comes from the fact that √5 is irrational and for n close to 2000000000 you would need a lot of precision and a lot of time if you wanted to use the naive solution.

The key in solving the problem is a mathematical concept called conjugation. In our problem, we simply note that (3 - √5) is a nice conjugate for (3 + √5). Let us define

(1)     α := 3 + √5,   β := 3 - √5,   and X n := α n + β n.
We first note that X n is an integer. This can be proved by using the binomial expansion. If you write everything down you'll notice that the irrational terms of the sums cancel each other out.
(2)    
Another observation is that β n < 1, so X n is actually the first integer greater than α n. Thus we may just focus on computing the last three digits of X .

 

A side note. In fact, βn tends to 0 so quickly that that our problem would be trivial if we asked for the three digits after the decimal point. For all large values of n they are always 999.

Based on (1) and (2), there are many different solutions for finding the last three digits of Xn.

Solution A. [the interleave of rational and irrational]

One solution goes like this: αn can be written as (an + bn√5), where an and bn are integers. At the same time, βn is exactly (an - bn√5) and Xn = 2an. Observe that

(3)     α (n + 1) = (3 + √5)(a n + b n√5) = (3a n + 5b n) + (3b n + a n)√5.
So a n + 1 = 3a n + 5b n and b n + 1 = 3b n + a n. This can be written in matrix form as
(4)    
Since α 0 = 1, we have (a 0, b 0) = (1, 0).

 

Now we use the standard fast exponentiation to get An in O(log n) time. Note that we do all operations modulo 1000 because we just need to return the last three digits of an.

Here's some Python code that implements this solution:

def matrix_mult(A, B):
C = [[0, 0], [0, 0]]
for i in range(2):
for j in range(2):
for k in range(2):
C[i][k] = (C[i][k] + A[i][j] * B[j][k]) % 1000
return C
def fast_exponentiation(A, n):
if n == 1:
return A
else:
if n % 2 == 0:
A1 = fast_exponentiation(A, n/2)
return matrix_mult(A1, A1)
else:
return matrix_mult(A, fast_exponentiation(A, n - 1))
def solve(n):
A = [[3, 5], [1, 3]]
A_n = fast_exponentiation(A, n)
return (2 * M_n[0][0] + 999) % 1000

 

Solution B. [the quadratic equation and linear recurrence]

Experienced contestants may notice there is a linear recurrence on the Xi's. Indeed, this is not hard to find -- the conjugation enters the picture again.

Notice that

(5)     α + β = 6, and α β = 4.
So α and β are the two roots of the quadratic equation x 2 - 6x + 4 = 0. i.e.,
(6)     α 2 = 6α - 4, and β 2 = 6β - 4.
Looking at (1) and (6) together, we happily get
(7)     X n+2 = 6X n+1 - 4X n.
Such recurrence can always be written in matrix form. It is somewhat redundant, but it is useful:
From here it is another fast matrix exponentiation. Let us see radeye's perl code that implements this approach here:
sub mul {
my $a = shift ;
my $b = shift ;
my @a = @{$a} ;
my @b = @{$b} ;
my @c = ($a[0]*$b[0] + $a[1]*$b[2],
$a[0]*$b[1] + $a[1]*$b[3],
$a[2]*$b[0] + $a[3]*$b[2],
$a[2]*$b[1] + $a[3]*$b[3]) ;
@c = map { $_ % 1000 } @c ;
return @c ;
}
sub f {
my $n = shift ;
return 2 if $n == 0 ;
return 6 if $n == 1 ;
return 28 if $n == 2 ;
$n -= 2 ;
my @mat = (0, 1, 996, 6) ;
my @smat = @mat ;
while ($n > 0) {
if ($n & 1) {
@mat = mul([@mat], [@smat]) ;
}
@smat = mul([@smat], [@smat]) ;
$n >>= 1 ;
}
return ($mat[0] * 6 + $mat[1] * 28) % 1000 ;
}
sub ff {
my $r = shift ;
$r = ($r + 999) % 1000 ;
$r = "0" . $r while length($r) < 3 ;
return $r ;
}
for $c (1..<>) {
$n = <> ;
print "Case #$c: ", ff(f($n)), "\n" ;
}

 

Solution C. [the periodicity of 3 digits]

For this problem, we have another approach based on the recurrence (7). Notice that we only need to focus on the last 3 digits of Xn, which only depends on the last 3 digits of the previous two terms. The numbers eventually become periodic as soon as we have (Xi, Xi+1) and (Xj, Xj+1) with the same last 3 digits, where i < j. It is clear that we will enter a cycle no later than 106 steps. In fact, for this problem, you can write some code and find out that the cycle has the size 100 and starts at the 3rd element in the sequence. So to solve the problem we can just brute force the results for the first 103 numbers and if n is bigger than 103 return the result computed for the number (n - 3) % 100 + 3.

Solution D. [the pure quest of numbers and combinatorics]

Let us see one more solution of different flavor. Here is a solution not as general as the others, but tailored to this problem, and makes one feel we are almost solving this problem by hand.

Let us look again at (2). We want to know X n mod 1000. We know from the chinese remander theorem that if we can find X n mod 8 and X n mod 125, then X n mod 1000 is uniquely determined.
(a) For n > 2, X n mod 8 is always 0. Since 5 i ≡ 1 (mod 4), 3 n-2i ≡ 1 or -1 (mod 4) depending on n, so, for n > 2,
(b) To compute X n mod 125, we only need to worry about i=0,1,2. All the rest are 0 mod 125. In other words, all we need to compute is
There are various ways to compute the elements in the above quantity. The exponents can be computed by fast exponentiation, or using the fact that 3 n mod 125 is periodic with cycle length at most 124. The binomial numbers can be computed using arbitrary precision integers in languages like Java and Python, or with a bit careful programming in languages like C++.

 

Afterword

There were many other interesting solutions submitted by the contestants, and Nathan Collins actually went through many of them and tried to summarize them in his awesome blog post.

More information:

Binomial numbers - Linear recurrence - Exponentiation - Chinese remainder theorem


Source Code
#include  < iostream >

using   namespace  std;

#define  Rep(i,n) for (int i(0),_n(n); i<_n; ++i)

ExpandedBlockStart.gifContractedBlock.gif
int *  matrix_mult( int *  A,  int *  B,  int *  ret)  {
ExpandedSubBlockStart.gifContractedSubBlock.gif    Rep(i,
2{
ExpandedSubBlockStart.gifContractedSubBlock.gif        Rep(j,
2{
ExpandedSubBlockStart.gifContractedSubBlock.gif            Rep(k,
2{
                ret[i
*2+k] = (ret[i*2+k] + A[i*2+j] * B[j*2+k]) % 1000;
            }

        }

    }

    
return ret;
}


ExpandedBlockStart.gifContractedBlock.gif
int *  fast_exponentiation( int *  A,  int  n,  int *  A1)  {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if (n == 1{
ExpandedSubBlockStart.gifContractedSubBlock.gif        Rep(i,
4{
            A1[i] 
= A[i];
        }

        
return A1;
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
else if (n % 2 == 0{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
int A_tmp[] = {0,0,0,0};
        fast_exponentiation(A, n
/2, A_tmp);
        matrix_mult(A_tmp, A_tmp, A1);
        
return A1;
    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
else {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
int A_tmp[] = {0,0,0,0};
        fast_exponentiation(A, n 
- 1, A_tmp);
        matrix_mult(A, A_tmp, A1);
        
return A1;
    }

}


ExpandedBlockStart.gifContractedBlock.gif
int  solve( int  n)  {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int A[] = {3513};
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int ret[] = {0,0,0,0};
    fast_exponentiation(A, n, ret);
    
return (2 * ret[0+ 999% 1000;
}



int  main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
int T;
    scanf(
"%d"&T);
ExpandedSubBlockStart.gifContractedSubBlock.gif    Rep(t, T) 
{
        
int n;
        scanf(
"%d"&n);
        
int ret = solve(n);
        
        printf(
"Case #%d: %03d\n", t+1, ret);
    }

}

转载于:https://www.cnblogs.com/clive/archive/2009/08/13/Google_code_jam_2008_R1A_Numbers.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值