Fast Power (integer + matrix): Hdu 2817+poj3233+poj1995+poj3070

Source: Internet
Author: User
Tags modulus stdin

The idea of a fast power: two points

Reference: http://blog.csdn.net/shiwei408/article/details/8818386

http://blog.csdn.net/hkdgjqr/article/details/5381292

hdu 2817 (integer fast power) A sequence of numbers

Problem Solving Ideas:

Write the expression of the linear, geometric series, and general an:

Fast power modulo + basic modulo operation is done, with code:

#include 
#include 
#include 
#include #include #include
using namespace std;
typedef long long LL;
const int MAXN = 65535;
const int mod=200907;
ll A1,a2,a3,n,an;
ll solve1 ()
{
   ll d= (A2-A1)%mod;
   Return (a1%mod+ (n-1)%mod*d)%mod)%mod;
}
ll Solve2 ()
{
    ll ans=1,b=n-1,a=a2/a1;
    while (b) {
        if (b&1) ans= (ans*a)%mod;
        b>>=1;
        A= (a*a)%mod;
    }
    Return (A1%mod*ans)%mod;
}
int main ()
{
  //Freopen ("Input.txt", "R", stdin);
   int t;cin>>t;
   while (t--) {
     cin>>a1>>a2>>a3>>n;
     if (A1+A3==2*A2) an=solve1 (); else An=solve2 ();
     cout<

POJ 3233 (Matrix fast Power + binary) matrix Power Series

Title Link: http://poj.org/problem?id=3233

Problem-solving ideas: Because K relatively large, if each of the fast power in addition, will definitely time out, need two solution, reference code:

#include 
#include 
#include 
#include #include #include
using namespace std;
typedef long long LL;
const int MAXN =;
ll N,mod,b;
struct matrix
{
  ll m[maxn][maxn];
} a,p;
Matrix Add (Matrix A,matrix B)            //matrices addition
{
    matrix C;
    for (int i=0;i>=1;
        A=multi (a,a);
    }
    return ans;
}
Matrix bsearch (int k)
{
   if (k==1) return A;
   Matrix Temp=bsearch (K/2), B;
   if (k&1) {
       b=quickpow_mod (a,k/2+1);
       Temp=add (Temp,multi (temp,b));
       Temp=add (temp,b);
   }
   else{
    B=quickpow_mod (A,K/2);
    Temp=add (Temp,multi (temp,b));
   }
   return temp;
}
int main ()
{
  //Freopen ("Input.txt", "R", stdin);
   while (Cin>>n>>b>>mod) {for
    (int i=0;i>a.m[i][j];
           A.m[i][j]%=mod;        After reading, first take the mold, avoid overflow and reduce the number of modulus
           p.m[i][j]= (i==j);
        }
     Matrix Ans=bsearch (b);
     for (int i=0;i

POJ 1995 (integer fast Power) raising Modulo Numbers

Title Link: http://poj.org/problem?id=1995

Problem Solving idea: integer fast power, in order to add modulus can be.

Reference code:

#include 
#include 
#include 
#include #include #include
using namespace std;
typedef long long LL;
const int MAXN =;
ll Mod,a,b;
ll Quickpow_mod (ll A,ll b)
{
    ll ans=1;
    while (b) {
        if (b&1) ans= (ans*a)%mod;
        b>>=1;
        A= (a*a)%mod;
    }
    return ans;
}
int main ()
{
  //Freopen ("Input.txt", "R", stdin);
  int t;cin>>t;
  while (t--) {
    cin>>mod;
    int n;cin>>n;
    ll Ans=0;
    while (n--) {
      cin>>a>>b;
      Ans= (Ans+quickpow_mod (b))%mod;
    }
    cout<

POJ 3070 (Matrix fast power) Fibonacci

Title Link: http://poj.org/problem?id=3070

Problem Solving Ideas:

To solve the Fibonacci, F (n) =f (n-1) +f (n-2), if we are a recursive solution, when n is particularly large when the complexity will be very high, but we use the following matrix operation, we can greatly reduce the time complexity, know this formula we use the matrix of fast power to solve the F (n).


Reference code:

#include 
#include 
#include 
#include #include #include
using namespace std;
typedef long long LL;
const int MAXN =;
const int mod=10000;
ll N;
struct matrix
{
    ll m[2][2];
} a,p;
Matrix multi (Matrix A,matrix B)
{
    matrix C;
    for (int i=0;i<2;i++) for
    (int j=0;j<2;j++) {
        c.m[i][j]=0;
        for (int k=0;k<2;k++)
            c.m[i][j]+=a.m[i][k]*b.m[k][j];
        C.m[i][j]%=mod;
    }
    return c;
}
Matrix Quickpow_mod (Matrix A,ll B)
{
    matrix ans=p;
    while (b) {
        if (b&1) Ans=multi (ans,a);
        b>>=1;
        A=multi (a,a);
    }
    return ans;
}
int main ()
{
 //  freopen ("Input.txt", "R", stdin);
   A.M[0][0]=A.M[0][1]=A.M[1][0]=1;A.M[1][1]=0;//A, p matrix initialization
   p.m[0][0]=p.m[1][1]=1;p.m[0][1]=p.m[1][0]=0;
   Matrix ans;
   while (cin>>n&&n!=-1) {
      ans=quickpow_mod (a,n);
      cout<





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.