青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Google code jam 2008 R1A - Numbers

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 Xn := αn + βn.
We first note that Xn 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 Xn 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)(an + bn√5) = (3an + 5bn) + (3bn + an)√5.
So an + 1 = 3an + 5bn and bn + 1 = 3bn + an. This can be written in matrix form as
(4)    
Since α0 = 1, we have (a0, b0) = (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 x2 - 6x + 4 = 0. i.e.,
(6)     α2 = 6α - 4, and β2 = 6β - 4.
Looking at (1) and (6) together, we happily get
(7)     Xn+2 = 6Xn+1 - 4Xn.
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 Xn mod 1000. We know from the chinese remander theorem that if we can find Xn mod 8 and Xn mod 125, then Xn mod 1000 is uniquely determined.
(a) For n > 2, Xn mod 8 is always 0. Since 5i ≡ 1 (mod 4), 3n-2i ≡ 1 or -1 (mod 4) depending on n, so, for n > 2,
(b) To compute Xn 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 3n 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)

int* matrix_mult(int* A, int* B, int* ret) {
    Rep(i,
2{
        Rep(j,
2{
            Rep(k,
2{
                ret[i
*2+k] = (ret[i*2+k] + A[i*2+j] * B[j*2+k]) % 1000;
            }

        }

    }

    
return ret;
}


int* fast_exponentiation(int* A, int n, int* A1) {
    
if (n == 1{
        Rep(i,
4{
            A1[i] 
= A[i];
        }

        
return A1;
    }

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

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

}


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



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

}

posted on 2009-08-12 21:19 Chauncey 閱讀(602) 評(píng)論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


導(dǎo)航

<2025年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

統(tǒng)計(jì)

常用鏈接

留言簿

隨筆檔案(4)

文章檔案(3)

搜索

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久人人97超碰国产公开结果| 国产一区av在线| 亚洲精品日韩在线观看| 久久精品国产在热久久 | 妖精成人www高清在线观看| 欧美在线影院| 欧美在线免费播放| 亚洲在线电影| 久久av老司机精品网站导航| 午夜精品视频在线| 久久露脸国产精品| 亚洲国产精品第一区二区| 欧美在线free| 欧美国产日本高清在线| 亚洲国内高清视频| 亚洲制服欧美中文字幕中文字幕| 亚洲在线观看免费| 久久精品亚洲一区二区三区浴池| 美女图片一区二区| 欧美性猛交一区二区三区精品| 国产精品有限公司| 亚洲精品视频免费观看| 久久精品一区二区三区中文字幕| 狠狠v欧美v日韩v亚洲ⅴ| 欧美精品亚洲一区二区在线播放| 欧美不卡三区| 欧美性淫爽ww久久久久无| 狠狠色香婷婷久久亚洲精品 | 国产毛片一区| 亚洲人成免费| 久久综合激情| 欧美亚洲在线播放| 国产精品麻豆va在线播放| 亚洲国产精品高清久久久| 久久精品在这里| 新狼窝色av性久久久久久| 欧美日韩国产在线| 一本色道久久综合亚洲精品不卡 | 亚洲国产电影| 欧美一进一出视频| 国语自产精品视频在线看抢先版结局 | 久久午夜视频| 久久青草福利网站| **欧美日韩vr在线| 欧美国产国产综合| 欧美日韩精品一区视频| 久久高清国产| 亚洲国产乱码最新视频| 欧美国产视频日韩| 欧美日韩国产黄| 亚洲欧美日韩国产中文在线| 亚洲一区二区三区影院| 在线观看国产日韩| 日韩亚洲欧美综合| 国产又爽又黄的激情精品视频| 久久天天狠狠| 欧美视频免费在线观看| 噜噜噜在线观看免费视频日韩| 欧美激情一区二区三级高清视频| 亚洲欧美不卡| 欧美日韩精品一区二区三区四区| 麻豆国产精品一区二区三区 | 亚洲特黄一级片| 影音先锋日韩精品| 亚洲影视九九影院在线观看| 亚洲大胆在线| 久久久精品五月天| 久久狠狠亚洲综合| 国产欧美日韩麻豆91| 亚洲视频你懂的| 在线中文字幕日韩| 欧美1级日本1级| 免费av成人在线| 在线免费观看日韩欧美| 久久激情视频久久| 欧美成人资源网| 亚洲黄一区二区三区| 欧美刺激性大交免费视频| 欧美69wwwcom| 99精品视频免费全部在线| 欧美丰满高潮xxxx喷水动漫| 欧美国产精品久久| 欧美一区二区三区免费大片| 国产精品久久91| 先锋影音一区二区三区| 老牛嫩草一区二区三区日本 | 一区二区视频免费在线观看 | 亚洲欧美制服中文字幕| 久久精品论坛| **欧美日韩vr在线| 欧美日韩国产123| 亚洲欧美日韩综合aⅴ视频| 鲁鲁狠狠狠7777一区二区| 亚洲欧洲精品一区二区三区不卡| 免费成人小视频| 亚洲在线电影| 91久久中文| 久久精品在线| 亚洲视频在线观看| 亚洲国产成人久久综合| 欧美日韩中文在线| 欧美大片在线观看一区二区| 亚洲一二三四久久| 亚洲精品一级| 亚洲高清不卡一区| 老色鬼精品视频在线观看播放| 这里只有精品电影| 日韩视频免费在线观看| 亚洲第一免费播放区| 一区二区三区在线免费视频| 国产欧美日韩视频在线观看 | 欧美亚洲视频一区二区| 亚洲人成77777在线观看网| 国产一区二区三区在线观看免费| 美女精品在线| 亚洲国产精品va| 最新成人av网站| 99riav1国产精品视频| 亚洲精品系列| 午夜精品免费视频| 欧美一二三视频| 久久福利电影| 欧美 日韩 国产 一区| 欧美日韩网站| 国产日韩欧美高清| 亚洲成人自拍视频| 亚洲天堂av高清| 欧美一区二区三区四区视频| 午夜精品免费视频| 亚洲第一精品久久忘忧草社区| 亚洲大片免费看| 欧美激情视频给我| 一区二区国产精品| 妖精成人www高清在线观看| 亚洲一区欧美一区| 欧美gay视频| 国产欧美日韩一区| 激情综合色丁香一区二区| 亚洲视频一区在线| 亚洲二区三区四区| 欧美制服丝袜| 国产精品主播| 亚洲一区二区视频在线观看| 久久激情五月婷婷| av成人福利| 欧美国产一区视频在线观看| 国产欧美欧美| 宅男精品导航| 亚洲精品中文字幕在线观看| 久久久综合网站| 激情久久久久久久久久久久久久久久| 一区二区三区欧美在线| 久久午夜国产精品| 久久在线视频在线| 亚洲二区免费| 亚洲国产日韩欧美在线动漫| 久久久久se| 欧美成人免费全部观看天天性色| 欧美午夜激情在线| 亚洲一区成人| 亚洲精品在线视频观看| 久久这里只有精品视频首页| 极品尤物av久久免费看| 99精品视频免费观看视频| 亚洲精品一区二区三区av| 欧美日韩免费在线观看| 欧美一级理论片| 欧美成人综合在线| 亚洲欧美一区二区激情| 久久国产精品一区二区| 亚洲欧洲午夜| 亚洲综合大片69999| 亚洲人体偷拍| 午夜免费日韩视频| 亚洲茄子视频| 久久亚洲图片| 欧美一级播放| 欧美韩日视频| 麻豆精品一区二区综合av| 欧美久久久久中文字幕| 免费观看成人www动漫视频| 国产精品看片你懂得| 亚洲盗摄视频| 在线看欧美视频| 久久久福利视频| 久久影院午夜片一区| 国产精品视频免费在线观看| 亚洲国产美女精品久久久久∴| 国产乱子伦一区二区三区国色天香| 欧美国产精品| 亚洲国产精品专区久久| 久久九九精品| 老**午夜毛片一区二区三区| 国产一区二区三区精品久久久| 亚洲欧美99| 久久免费视频在线观看| 亚洲国产精品va在线看黑人| 男男成人高潮片免费网站| 亚洲电影免费观看高清完整版 | 老牛国产精品一区的观看方式|