BZOJ4002[JLOI2015] Meaningful string

Source: Internet
Author: User

BZOJ4002[JLOI2015] Meaningful string

Test instructions

(b+√d)/2) The integer portion of the ^n. B*b<d<10^18,n<10^18,d%4==1,b*b%4==1, modulus approximately equal to 7*10^18

Exercises

Divine questions. By some properties can draw a series: an=ban-1+ (D-b*b)/4*an-2, and this sequence of the general formula is an= ((b+√d)/2) ^n+ ((b-√d)/2) ^n, and by the topic conditions (D-B*B)/4 is a positive integer, Therefore, a can be obtained by matrix multiplication, because (b-√d)/2∈ ( -1,0], the answer is (an)-1 when and only if n is even and b*b!=d. Matrix recursion:

An-1 An-2 B 1 an An-1

*                             =

0 0 (d-b*b)/4 0 0 0

Reflection: Konjac Konjac do not know the moment multiplication does not meet the Exchange law, adjusted a long example. At the same time, because the modulus is too large, in addition to the need to use unsigned long long, multiplication is also used fast multiplication (is to use a fast power method to calculate multiplication) to prevent overflow.

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #definell unsigned long long5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMoD 75284434125795769377 using namespacestd;8 9 structm{Tenll a[5][5]; OneM () {Inc (I,0,1) Inc (J,0,1) a[i][j]=0;} A }; - ll B,d,n; M St,ans; - ll Cheng (ll a,ll b) { the     if(b==0)return 0;if(b==1)returnA ll C=cheng (a,b>>1)%MoD; -     if(b&1)return((c+c)%mod+a)%mod;Else return(c+c)%MoD; - } - m Mul (M a,m b) { +M C; Inc (I,0,1) Inc (J,0,1) Inc (K,0,1) -C.a[i][j]= (C.a[i][j]+cheng (a.a[i][k],b.a[k][j]))%MoD; +     returnC; A } at m pow (M a,ll b) { -     if(b==1)returnA M C=pow (a,b>>1);if(b&1)returnMul (Mul (C,c), a);Else returnMul (c,c); - } - intMain () { -scanf"%lld%lld%lld",&b,&d,&n); -     if(n==0) {printf ("1");return 0;} inst.a[0][0]=b%mod; st.a[0][1]=2; -     if(n==1) ans=st;Else{ toans.a[0][0]=b%mod; ans.a[0][1]=1; ans.a[1][0]= (D-B*B)/4%MoD; +Ans=pow (ans,n-1); ans=Mul (St,ans); -     } the     if(b*b!=d&&! (n&1)) printf ("%lld", (ans.a[0][0]+mod-1)%mod);Elseprintf"%lld", ans.a[0][0]); *     return 0; $}

20160710

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.