"Fermat theorem + fast Power + inverse" bzoj3240-[noi2013] matrix game

Source: Internet
Author: User

"The main topic"

If you use F[i][j] to represent the elements of column I, J, in the Matrix, then F[i][j] satisfies the following recursion:
F[1][1]=1
F[i,j]=a*f[i][j-1]+b (j!=1) ①
F[i,1]=c*f[i-1][m]+d (i!=1) ②
The a,b,c,d in a recursive style is a given constant. F[N][M].

Ideas

Grind for a morning, however the extra data on the UOJ is not over yet. Bzoj on the AC first put up, follow slowly grind ...

* There is a point, the final output of the answer when the first +mod and then%mod.

*mod be diligent, or it will explode.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineMOD 10000000076 using namespacestd;7typedefLong Longll;8 Const intmaxn=1000000+5;9 structnodeTen { Onell Uni,ord;//Uni means A≠1,ord represents a=1 A }n,m; - ll A,b,c,d; -  the ll Quick (ll X,ll p) - { -ll res=p,retu=1, now=x; -      while(res>0) +     { -         if(res&1) retu= (retu*now)%MOD; +now= (now*now)%MOD; Ares>>=1; at     } -     returnRetu; - } -  - ll Ni (ll x) - { in     returnQuick (x,mod-2); - } to  +Node Get_value (Charstr[]) - { the     intlen=strlen (str); *Node ret= (node) {0,0};  $      for(intI=0; i<len;i++)Panax Notoginseng     { -Ret.uni= ((ret.uni*Ten)% (mod-1) +str[i]-'0')% (mod-1); theRet.ord= ((ret.ord*Ten)%mod+str[i]-'0')%MOD; +     } A     returnret; the }  +  - voidInit () $ { $     CharSTRM[MAXN],STRN[MAXN]; -scanf"%s%s", STRN,STRM); -n=get_value (STRN); them=get_value (STRM); -scanf"%lld%lld%lld%lld",&a,&b,&c,&d);Wuyi } the  - voidGet_ans () Wu { -ll F;//=f[n+1][1] About     if(a==1) $     { -ll D= ((((C* (m.ord-1))%mod) *b)%mod+d)%MOD; -         if(c==1) f= (1+N.ORD*D)%MOD; -             Else A             { +ll cn=Quick (C,n.uni); theF= (cn+ (d* (cn-1) *ni (C-1) (%mod))%MOD; -             }  $     } the     if(a!=1) the     { thell Am=quick (a,m.uni-1); thell a= (am*c)%MOD; -ll b= (((((b*c)%mod* (am-1))%mod*ni (A-1))) (%mod+d)%MOD; inll an=Quick (A,n.uni); theF=an+ ((b* (an-1)%mod) *ni (A-1))%MOD; the     } Aboutprintf"%lld", ((f-d) *ni (c)%mod+mod)%MOD);  the } the  the intMain () + { - init (); the Get_ans ();Bayi     return 0; the}

"Fermat theorem + fast Power + inverse" bzoj3240-[noi2013] matrix game

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.