Codeforces 185A Fast Power

Source: Internet
Author: User

At the beginning of the matrix to find the fast power of the problem to see this problem, test instructions is to let you find the nth triangle pointing up the number of small triangles. It is easy to see the recursive relationship, we use f[n] to represent the number of small triangles upward in the nth large triangle, g[n] represents the number of small triangles downward in the nth large triangle, and then the recursive relationship is:

F[n]= 3*f[n-1]+1*g[n-1];

G[n]= 3*g[n-1]+1*f[n-1];

Where F[0]=1,g[0]=0 (the first pure triangle starts from n=0), then ... There is no then, intuitively feel f[n] is difficult to express into a f[i], a moment did not think how to link up with the matrix (in fact, two different variables can be constructed out of the matrix: [F (N), g (n)]t= [[3,1], [1,3]]t * [F (n-1), G ( n-1)]t can, the matrix is [[3,1], [1,3]]t, in this writing matrix after all very inconvenient, I wrote on the draft paper to see it, more intuitive, then a moment of brain fever did not go to this direction to think, then turned to the second idea.

I once again observed that it is indeed very regular, whether it is f[n] or g[n] are 1+2+3+......+k and well, K is the large triangle layer, and the number of layers is also very regular, the nth triangle layer is twice times the previous (it is not difficult to see, because each sub-triangle is divided into two layers each time), so K = 2n, so f[n]= (1+2n) *2n-1,g[n] is also easy to express, but at this time no need to g[n], then, is a simple bare fast power can!

1#include <cstdio>2typedefLong LongLL;3 ConstLL mod=1000000007;4 5 ll Quick_mod (ll A, ll B, ll m) {6LL ans=1;7a%=m;8      while(b) {9         if(b&1) ans= ans*a%m;TenA= a*a%m; Oneb>>=1; A     } -     returnans; - } the  - intMain () { - LL N; -scanf"%i64d",&n); +     if(!n) puts ("1"); -     Elseprintf"%i64d\n",(1+quick_mod (2, N,mod)) *quick_mod (2, N-1, MoD)%MoD); +     return 0; A}

Later, I changed the input into while (~SCANF (...)) to try, too, you can see that the CF is a single set of data read, even if this is done with while, pay attention to add a good file terminator eof.

After the water, I looked at 1000000007 this value think with a long long to deal with seems to be a bit wasteful, multiplication is overflow, but the sum will never overflow, but is already very close to the upper limit of Int. So, I installed the next B, the rapid power in the multiplication to add (also using the principle of fast power, binary processing continuous addition), so that can avoid a long long (although not save any resources, but can be trained to handle the data overflow some of the ability. However, in the game is absolutely not to make trouble for themselves, insurance is the most important, and write good fast after the mold, but forget the last (1+2n) *2n-1 here there is a multiplication, so also debugging a little time.

1#include <cstdio>2typedefLong LongLL;3 Const intMod=1000000007;4 5 intQuick_mul (intAintBintm) {6     intans=0;7a%=m;8      while(b) {9         if(b&1) ans= (ans+a)%m;TenA= (a+a)%m; Oneb>>=1; A     } -     returnans; - } the  - intQuick_mod (intA, LL B,intm) { -     intans=1; -a%=m; +      while(b) { -         if(b&1) ans=Quick_mul (ans,a,m); +A=Quick_mul (a,a,m); Ab>>=1; at     } -     returnans; - } -  - intMain () { - LL N; in      while(~SCANF ("%i64d",&N)) { -         if(!n) puts ("1"); to         Else { +             intTmp1= Quick_mod (2, N-1, MoD); -             intTmp2= tmp1*2%MoD; theprintf"%d\n", Quick_mul (1+tmp2), tmp1,mod)); *         } $     }Panax Notoginseng     return 0; -}

At this point, the installation force to complete, successful water.

Codeforces 185A Fast Power

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.