BZOJ4002 [JLOI2015] meaningful string

Source: Internet
Author: User

It is said that these two games add up as long as 170 = = And this is the simplest topic qaq

See $ (\frac {b + \sqrt {d}} {2}) ^n$, the first reaction is a conjugate radical $ (\frac {B-\sqrt {d}} {2}) ^n$

First there are $ (\frac {b + \sqrt {d}} {2}) ^n + (\frac {b-\sqrt {d}} {2}) ^n$ as integers

From high school textbook knowledge, the above is actually a three-item recursive sequence of the formula, and the recurrence of the sequence is very simple

$ $f [x] = b * f[x-1]-\frac{b^2-d} {4} * f[x-2]$$

where $f[0] = 2, f[1] = b$, this way direct matrix multiplication is good ~

See $ A = (\frac {b-\sqrt {d}} {2}) ^n \in [-1, 1]$

And then... And then found Bzoj on the wrong side of the Qaq, is actually "for 20% of data $b=1,d=5$"

So $ A > 0$ when and only if the $d \not= b^2$ and $n$ is even, this time to reduce $f[n]$ $1$, otherwise do not reduce

1 /**************************************************************2 problem:40023 User:rausen4 language:c++5 result:accepted6 time:48 Ms7 memory:1272 KB8 ****************************************************************/9  Ten#include <cstdio> One#include <iostream> A#include <cstring> -#include <algorithm> -   the using namespacestd; -typedef unsignedLong Longll; - Constll mod =7528443412579576937ull; -   +Template <classT>T Sqr (t x) { -     returnX *x; + } A   atTemplate <classT>T Mul (t X, t y) { -     StaticT Res; -     if(X <y) Swap (x, y); -res =0; -      while(y) { -         if(Y &1) Res = (res + x)%MoD; inx = (x <<1)% mod, y >>=1; -     } to     returnRes; + } -   the ll B, D, N; *   $ structMat {Panax Notoginsengll x[3][3]; -       theInlinevoidClear () { +memset (x,0,sizeof(x)); A     } theInlinevoidOne () { +memset (x,0,sizeof(x)); -x[1][1] = x[2][2] =1; $     } $InlinevoidPre () { -          This-Clear (); -x[1][1] =0, x[1][2] = (D-SQR (b)) >>2, x[2][1] =1, x[2][2] =b; the     } -      WuyiInline ll*operator[] (inti) { the         returnX[i]; -     } Wu       -Inline matoperator* (ConstMat &p)Const { About         StaticMat Res; $         Static intI, J, K; -          for(Res.clear (), i =1; I <=2; ++i) -              for(j =1; J <=2; ++j) -                  for(k =1; K <=2; ++k) ARES[I][J] = (Res[i][j] + mul (x[i][k], p.x[k][j]))%MoD; +         returnRes; the     } -Inline mat&operator*= (ConstMat &p) { $         return* This= * This*p; the     } the } A; the   theInline Mat Pow (ConstMat &p, ll y) { -     StaticMat X, res; inRes.one (), x =p; the      while(y) { the         if(Y &1) Res *=x; Aboutx *= x, y >>=1; the     } the     returnRes; the } +   - intMain () { theCIN >> B >> D >>N;Bayi A.pre (); theA =Pow (A, n); thecout << ((a[1][1] <<1)% mod + mul (b, a[2][1])-(d! = Sqr (b) &&! (N &1))% mod + MoD)% mod <<Endl; -     return 0; -}
View Code

BZOJ4002 [JLOI2015] meaningful string

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.