Bzoj 3665:maths

Source: Internet
Author: User
Tags mul

Sol

Matrix multiplication + Fast Power + Euler theorem.

The first observation of the topic can be found that \ (a_n\) can be expressed in the form of several powers of \ (k\) and \ (a_0\).

\ (a_0\) is relatively simple \ (m^n\) so the first part \ (ans1=a_0^{m^n}\).

Look at \ (k\) Find the law can be found, \ (k\) The power of the form is \ (m^{n-1}+2m^{n-2}+3m^{n-3}+...+nm^0\).

This thing can construct a matrix to hand out. a \ (3\) variable \ (ans,b,1\) is required in the matrix.

where \ (ans\) is the current answer, \ (b\) is the loop variable, \ (1\) is the value of \ (b\) increment each time.

\[\begin{bmatrix}ans\\ b\\ 1\end{bmatrix}\]

That's the kind of a matrix that we want it to become

\[\begin{bmatrix}ans*m+b\\ b+1\\ 1\end{bmatrix}\]

Obviously, the matrix is very well constructed.

\[\begin{bmatrix}m&1&0\\0&1&1\\0&0&1\end{bmatrix}\]

This matrix can be quickly idempotent, but note is the power of \ (n-1\).

Then the problem has a pit point is \ (p\) is not a prime, but to ensure that coprime, if it is \ (k\) or \ (a_0\) we only need to mold about \ (p\) The inverse of the recurses can be, here can be directly used Euler theorem.

\[a^{\varphi (n)}\equiv 1 (mod n) \]

Then it became a template problem ... Slowly all is the template ...

MD!SBT Card constant. Write a quick multiply. Turn into \ (log^2\) T to die. This constant needs to be optimized, and a \ (log\) is optimized.

Fast multiplication can be segmented to write first before \ (10^6\) number and multiply after \ (10^6\) number on it.

I did not want to quickly ride this \ (log\) feel can be too ... Then for the card constant ... Everything has been written ...

Code
/************************************************************** problem:3665 User:beiyu language:c++ Result : Accepted time:12284 ms memory:20836 kb****************************************************************/#include &L t;cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector># Include<iostream>using namespace Std; typedef long LONG Ll;//typedef vector<ll> vec;//typedef vector<vec> Mat;    struct mat{LL a[3][3]; Mat () {memset (a,0,sizeof (a));}}; LL T,p,m,n,k,a0,phi; LL Ans1,ans2; Char *ps= (char *) malloc (20000000); inline ll in (ll X=0) {for (;*ps> ' 9 ' | |    *ps< ' 0 ';p s++); for (; *ps>= ' 0 ' &&*ps<= ' 9 ';p s++) x= (x<<3) + (x<<1) +*ps-' 0 '; return x;    }inline void Out (LL x) {int L=0;char ch[65];    if (!x) {Putchar (' 0 '); return;}    if (x<0) Putchar ('-'), x=-x;    while (x) ch[++l]=x%10+ ' 0 ', x/=10; for (int i=l;i;i--) Putchar (Ch[i]);}   inline ll Getphi (ll p) {ll res=p,m=sqrt (p) +0.5; for (int i=2;i<=m;i++) if (p%i==0) {res=res/i* (i-1);    while (p%i==0) p/=i; } if (p>1) res=res/p* (p-1); return res;}    inline ll Mul (ll a,ll b,ll p) {ll t1=b/1000000,t2=b%1000000; Return ((t1*a)%p*1000000%p+t2*a%p)%p;//if (p<=1000000000) return a*b%p;//return (a*b-(LL) (A/long double) p*b+1e-3 ) *p+p)%p;//for (; b;b>>=1,a= (a+a)%p) if (b&1) res= (res+a)%p;return Res;} inline ll Pow (LL a,ll b,ll p,ll Res=1) {for (; B;b>>=1,a=mul (a,a,p)) if (b&1) Res=mul (res,a,p); return res;}    MAT operator * (const MAT &a,const mat &b) {Mat C; for (int i=0;i<3;i++) for (int j=0;j<3;j++) for (int k=0;k<3;k++) c.a[i][j]= (C.a[i][j]+mul (a.a[i][k],b.a[k][j    ],phi))%phi; return C;}    Mat operator ^ (Mat A,ll b) {mat res;    for (int. i=0;i<3;i++) for (int j=0;j<3;j++) res.a[i][j]= (i==j)? 1:0;    for (; b;b>>=1,a=a*a) if (b&1) res=res*a; return res;}  int main () {//Freopen ("Maths.in", "R", stdin);//Freopen ("Maths.out", "w", stdout);//Ios::sync_with_stdio (FALSE);    Fread (Ps,1,20000000,stdin);    Mat A; For (T=in (), P=in (), Phi=getphi (p);         t--;) {m=in ()%phi,a0=in ()%p,k=in ()%p,n=in (); Cout<<mul (N,pow (n,5)) <<endl;//cout<< "0" <<endl;//cout<<m<< "" &LT;&L         t;a0<< "" <<k<< "" <<n<<endl;        cout<<ans1<<endl;         Ans1=pow (A0,pow (M,n,phi), p);//cout<<ans1<<endl;                 cout<< "1" <<endl;        a.a[0][0]=m,a.a[0][1]=1,a.a[0][2]=0;        A.a[1][0]=0,a.a[1][1]=1,a.a[1][2]=1;        A.a[2][0]=0,a.a[2][1]=0,a.a[2][2]=1;         a=a^ (n-1);                 cout<< "2" <<endl;         Ans2=pow (k, ((A.a[0][0]+2*a.a[0][1])%phi+a.a[0][2])%phi,p);        cout<< "3" <<endl;//cout<<ans1<< "" <<ans2<<endl;    Out (Mul (ans1,ans2,p)), Putchar (' \ n ');//printf ("%i64d\n", Mul (ans1,ans2,p)); } return 0;}

  

Bzoj 3665:maths

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.