elvis presley Blind Date

Source: Internet
Author: User

Main Topic

Given a pyramid, the number in each layer from the second layer is equal to the sum of the two numbers around the next layer. Given a positive integer $n (n\le10^5) $, the first layer of the pyramid is a sequence of $1$ to $n$ from small to large, seeking the number of the top of the pyramid.

Sequence characteristics

Only the arithmetic progression of the first behavior $1~n$ can be known from the given condition.

Assuming that the first row of a three consecutive number is $a-d, A, a+d$, ($d $ for arithmetic progression tolerance, here $d=1$), then the second row corresponds to the number of $2a-d,2a+d$, the difference $2d$, because of the generality of $a$, so for the second row any two of the difference is $2d$, And so on each row of the sequence is composed of arithmetic progression, the $i$ row series of tolerance is $d_i=2^{i-1}$.

The value of the first item

After deriving the tolerance, as long as the first item of the sequence is known, the whole sequence can be obtained quickly, and the last one is the number of the top of the pyramid, which is equivalent to the first of the topmost layer, so the $f_i$ is the first item of the $i$ layer.

According to the sum of each number equal to the next layer about two numbers, then there is
$$\begin{array}\\f_i&=&f_{i-1}+ (F_{i-1}+d_{i-1}) \\&=&2f_{i-1}+2^{i-1} \end{array}$$

Substituting $n$, there is

$$\begin{array} \ F_n&=&2f_{n-1}+2^{n-2} \ &=&2 (2f_{n-2}+2^{n-3}) +2^{n-2}&=&4f_{n-2}+2\ CDOT 2^{n-2} \ &=&4 (2f_{n-3}+2^{n-4}) +2^{n-2}&=&8f_{n-3}+3\cdot 2^{n-2} \\&=&......\\ _{line i} &=&2^if_{n-i}+i\cdot 2^{n-2} \ _{when i=n-1}&=&2^{n-1}f_1+ (n-1) \cdot 2^{n-2} \ &=& (n+1) \cdot 2 ^{n-2} \end{array}$$

So as long as the high-precision plus a fast power is good.

Code
1#include <cstdio>2#include <cstring>3 4 structBigNumber5 {6     Static Const Long LongBASE =1000000000;7     Static Const intBasedigs =9;8     9     intNdigs;Ten     Long Long*digs; One      A     voidInitintNConst Long Long*d) -     { -          while(N >0&& D[n-1] ==0) the--N; -Ndigs =N; -Digs =New Long Long[n]; -          for(inti =0; I < n; ++i) +Digs[i] =D[i]; -     } +      ABigNumber (intNConst Long Long*d) at     { - init (n, d); -     } -      -BigNumber (Long Longx =0) -     { in         Long Longd[2]; -d[0] = x%BASE; tod[1] = x/BASE; +Init2, d); -     } the      *BigNumberoperator*(ConstBigNumber &a)Const $     {Panax Notoginseng         intn = ndigs +A.ndigs; -         Long LongD[n], p; the          for(inti =0; I < n; i++) +D[i] =0; A          for(inti =0; i < Ndigs; i++) the         { +p =0; -              for(intj =0; J < A.ndigs; J + +) $             { $                 Long Longv = digs[i] *A.digs[j]; -                 Long LongV1 = v/base, V0 = VBASE; -D[i + j] + = V0 +p; thep = v1 + d[i + j]/BASE; -D[i + j]%=BASE;Wuyi             } the              for(intj = i + A.ndigs; p >0; ++j) -             { WuD[J] + =p; -p = d[j]/BASE; AboutD[J]%=BASE; $             } -         } -         returnbignumber (n, d); -     } A  +BigNumberoperator*(Long LongXConst the     { -         intn = ndigs +1; $         Long LongD[n]; the         Long LongA =0; the          for(inti =0; i < Ndigs; i++) the         { theA + = digs[i] *x; -D[i] = a%BASE; inA/=BASE; the         } theD[ndigs] =A; About         returnbignumber (n, d); the     } the  the     voidWrite ()Const +     { -         if(Ndigs = =0) printf ("0"); the         ElseBayi         { theprintf"%lld", Digs[ndigs-1]); the              for(inti = Ndigs-2; I >=0; i--) -printf"%0*lld", Basedigs, Digs[i]); -         } the     } the }; the  the intN; -  theBigNumber Pow (BigNumber x,intp) the { the     if(p = =0)returnBigNumber (1);94     if(p = =1)returnx; the     if(p = =2)returnX *x; the     if(P &1)returnPow (POW (x, P/2),2) *x; the     returnPow (POW (x, P/2),2);98 } About  - intMain ()101 {102scanf"%d", &n);103     if(n = =1) printf ("1");104     Else(Pow (BigNumber (2), N-2) * (n +1) . write (); the}

elvis presley Blind Date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.