More than 2019 cattle off summer school camp (fifth) generator 1

Time limit: C / C ++ 2 seconds and 4 seconds languages other
space restrictions: C / C ++ 262144K, other languages 524288K
64bit the IO the Format: LLD%

Title Description

You are given four positive integers x0,x1,a,b x0,x1,a,b. And you know  for all i≥2.

Given two positive integers n, and MOD, please calculate xnx_n xn modulo MOD.

Does the problem look simple? Surprise! The value of n may have many many digits!

Enter a description:

The input contains two lines.
The first line contains four integers x0,x1,a,b(1≤x0,x1,a,b≤10^9).
The second line contains two integers n, MOD (1≤n<10(10^6),10^9<MOD≤2×10^9, n has no leading zero).

Output Description:

Print one integer representing the answer.

Entry

1 1 1 1 10 1000000001

Export

89

Explanation

The resulting sequence x is Fibonacci sequence. The 11-th item is 89.

Entry

1315 521 20185 5452831 9999999999999999999999999999999999999 1000000007

Export

914730061

The meaning of problems: Known x0, x1, a, b, n, mod, and formulas , demand for modulo mod.

Solution: Decimal rapid power.            

 

Code:

#include<bits/stdc++.h>
using namespace std;
long long x0,x1,a,b,mod;
struct node
{
  long long Martix[2][2];
  node operator *(const node&n) const
  {
   node sum;
   int i,j,k;
   for(i=0;i<2;i++)
     for(j=0;j<2;j++)
     {
       sum.Martix[i][j]=0;
       for(k=0;k<2;k++)
         sum.Martix[i][j]=(sum.Martix[i][j]+Martix[i][k]*n.Martix[k][j])%mod;
     }
    return sum;
  }
};
node ans,t;
char n[1000005];
node quick(node A,int k);
int main()
{
  int i,k,l;
  scanf("%lld%lld%lld%lld",&x0,&x1,&a,&b);
  scanf("%s%lld",n, &mod);
  ans.Martix[0][0]=ans.Martix[1][1]=1;
  ans.Martix[1][0]=ans.Martix[0][1]=0;
  t.Martix[0][0]=a;t.Martix[0][1]=b;
  t.Martix[1][0]=1;t.Martix[1][1]=0;
  l=strlen(n);
  for(i=l-1;i>=0;i--)
  {
    ans=ans*quick(t,n[i]-'0');
    t=quick(t,10);
  }
  printf("%lld",(x1*ans.Martix[1][0]+x0*ans.Martix[1][1])%mod);
  system("pause");
  return 0;
}
node quick(node A,
int k) { node sum; sum.Martix[0][0]=sum.Martix[1][1]=1; sum.Martix[0][1]=sum.Martix[1][0]=0; while(k) { if(k&1) sum=sum*A; k>>=1; A=A*A; } return sum; }

Guess you like

Origin www.cnblogs.com/VividBinGo/p/11291591.html