leetcode:. 372 super power (math) ------ mold problem

topic:

Here Insert Picture Description

analysis:

(1)(axb)% c =(a1+n1 c)(a2+n2 c)%c=(a1xa2)%c=(a1%c xa2%c)%c
(2)a ^ b % c
= (a % c) ^ b % c
= ((a % c) ^ (b - t) * (a % c) ^ t) % c

Proof of a formula:

(x+y)%c=(x%c+y%c)%c

aaa。。。aaa%c=( (a-c)a…a%c+c*a。。。a%c)%c=(a-c)a…a%c

(3) To master the fast power algorithm.
(4): Look at the following chart, you should be the (Pirates)
Here Insert Picture Description

Code:

int main()
{
 int a=2;
 vector<int> v;
 v.push_back(1);
 v.push_back(0);
 a=a%1337;
 int A[10];//存放a的i次方。
 A[0]=1;
 for(int i=1;i<10;i++)
 {
  A[i]=(A[i-1]*a)%1337;
  } 
  int b=1;
 if(v.size()==1) return A[v[0]];
 for(int i=0;i<v.size()-1;i++)
 {
  b=(b*A[v[i]])%1337;
  //2
  int b1=b*b;
  b1=b1%1337;
  //4
  int b2=b1*b1;
  b2=b2%1337;
  //8
  int b3=b2*b2;
  b3=b3%1337;
  b=(b3*b1)%1337;
 }
 cout<<(b*A[v[v.size()-1]])%1337;
 
}
Published 207 original articles · won praise 134 · Views 6751

Guess you like

Origin blog.csdn.net/weixin_42721412/article/details/104570293