Fast Power + Fermat theorem for CSU1516 matrices

Source: Internet
Author: User

  Through this problem really learned a lot of things, at least the matrix fast power is a primer, the common fast power also thoroughly understand (formerly are playing template), understand the Fermat theorem

Key (A^fib[b])%p values where p is a prime number, 0<a<p, B is within the INT range

Let's just assume that fib[b]>p-1

(A^fib[b])%p = (a^ (p-1) *a^ (p-1) *....*a^ (p-1) *a^m)%p

(Here m = fib[b]% (p-1) )

Since P is a prime number and a<p so gcd (a,p) = 1, so the fee is very low (a^ (p-1))%p=1

The above formula = (a^ (fib[b]% (p-1)))%p

Then fib[b]% (p-1) can be obtained by the Matrix fast Power

At last, we can get (a^m)%p with the common fast power.

The rest is just an interval DP.

In a hurry to write, the code is very ugly, ll and int everywhere definition ...

Here is the result of the operation, time limit of 2s, a little risk. There are also 1s of ...

3 105459 Round_0 1572 KB 1860 MS C++ 2109 B 2015-04-06 01:51:32

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 Const intMAXN = the;7 Const intINF =0x3f3f3f3f;8typedefLong LongLL;9 intA[MAXN],B[MAXN];Ten intN; One LL p; A LL SUM[MAXN],DP[MAXN][MAXN]; - structnode - { theLL a[2][2]; - node () -     { -a[0][0] = a[1][0] = a[0][1] =1; +a[1][1] =0; -     } + }; A node Mul (node A,node B) at { - node ans; -      for(inti =0;i<2; ++i) for(intj =0;j<2;++j) -     { -ANS.A[I][J] =0; -          for(intK =0;k<2; ++k) Ans.a[i][j] = (ans.a[i][j]+a.a[i][k]*b.a[k][j])% (P-1); in     } -     returnans; to } + ll Mypow (ll x,ll m,ll MoD) - { theLL ret =1; *      while(m) $     {Panax Notoginseng         if(m&1) ret = (ret*x)%MoD; -x = (x*x)%MoD; theM/=2; +     } A     returnret; the } + intFibintx) - { $ node ans,s; $      while(x) -     { -         if(x&1) ans =Mul (ans,s); thes =Mul (s,s); -X/=2;Wuyi     } the     returnans.a[1][0]% (P-1); - } Wu intSolveintx) - { About     intm = fib (b[x]-1); $     returnMypow (a[x],m,p); - } - ll gcd (ll X,ll y) - { A     returnY?GCD (y,x%y): x; + } the intMain () - { $ //freopen ("In.txt", "R", stdin); the     intKase =1; the      while(cin>>n>>p) the     { the          for(inti =1; i<=n;++i) for(intj =1; j<=n;++j) Dp[i][j] = i==j?0: INF; -          for(inti =1; i<=n;++i) scanf ("%d", A +i); in          for(inti =1; i<=n;++i) scanf ("%d", B +i); the          for(inti =1; i<=n;++i) A[i] =solve (i); the  Aboutsum[0] =0; sum[1] = a[1]; the          for(inti =2; i<=n;++i) Sum[i] = sum[i-1]+(LL) a[i]; theprintf"Case %d:", kase++); the         if(n==1) {printf ("0\n");Continue;} +          for(intR =2; r<=n;++r) for(inti =1; i<=n-r+1;++i) -         { the             intj = i+r-1;Bayi              for(intK = i;k<j;++k) theDp[i][j] = min (dp[i][j],dp[i][k]+dp[k+1][J]+GCD (sum[k]-sum[i-1],sum[j]-sum[k-1])); the         } -cout<<dp[1][n]<<Endl; -     } the     return 0; the}

  

Fast Power + Fermat theorem for CSU1516 matrices

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.