Fast Power

╰+攻爆jí腚メ 2021-12-09 14:53 273阅读 0赞

一、

整数:求a^n,把n化为2*(k1+k2+..+km);

矩阵:注意矩阵乘法。

复杂度:O(logn)

tip:取了模(根据题目要求)

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<bits/stdc++.h>
  2. 2 #define mem(a) memset(a,0,sizeof(a))
  3. 3 #define ll long long
  4. 4 using namespace std;
  5. 5 ll mod=1e9;
  6. 6 ll quickpow(ll a,ll n){
  7. 7 ll ans=1;
  8. 8 while(n){
  9. 9 if(n&1)
  10. 10 ans=(ans*a)%mod; //取模
  11. 11 n>>=1;
  12. 12 a=(a*a)%mod;
  13. 13 }
  14. 14 return ans%mod;
  15. 15 }
  16. 16 int main(){
  17. 17 cout<<quickpow(2,19);
  18. 18 return 0;
  19. 19 }

整数

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<bits/stdc++.h>
  2. 2 #define mem(a) memset(a,0,sizeof(a))
  3. 3 #define ll long long
  4. 4 const int N=1e6+5;
  5. 5 using namespace std;
  6. 6 ll mod=1e9;
  7. 7 struct matrix{
  8. 8 int m[N][N];
  9. 9 };
  10. 10 matrix mat_multi(matrix a, matrix b){
  11. 11 matrix ans;
  12. 12 for(int i=0;i<N;i++){
  13. 13 for(int j=0;j<N;j++){
  14. 14 ans.m[i][j]=0;
  15. 15 for(int k=0;k<N;k++){
  16. 16 ans.m[i][j]+=(a.m[i][k]%mod*b.m[k][j]%mod)%mod;
  17. 17 ans.m[i][j]%=mod;
  18. 18 }
  19. 19 }
  20. 20 }
  21. 21 return ans;
  22. 22 }
  23. 23 matrix quickpow(matrix a,ll n){
  24. 24 matrix ans;
  25. 25 for(int i=0;i<N;i++){
  26. 26 for(int j=0;j<N;j++){
  27. 27 if(i==j) ans.m[i][j]=1;
  28. 28 else ans.m[i][j]=0;
  29. 29 //初始化为单位矩阵,类比整型时的1;单位矩阵*矩阵A=矩阵A
  30. 30 }
  31. 31 }
  32. 32 while(n!=0){
  33. 33 if(n&1)
  34. 34 ans=mat_multi(a,ans);
  35. 35 n>>=1;
  36. 36 a=mat_multi(a,a);
  37. 37 }
  38. 38 return ans;
  39. 39 }

矩阵

二、题目

①基础

(1)http://noi.openjudge.cn/ch0204/2991/

关键是循环节

tips:string->int:m=atoi(n.c_str());

  1. 去字符串:substr(起始,长度)

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<bits/stdc++.h>
  2. 2 #define mem(a) memset(a,0,sizeof(a))
  3. 3 #define ll long long
  4. 4 #define inf 0x3f3f3f3f
  5. 5 const int N=1e6+5;
  6. 6 using namespace std;
  7. 7 ll mod=1e4;
  8. 8 ll Mod=500;
  9. 9 ll ans[205];
  10. 10 ll quickpow(ll a,ll n){
  11. 11 ll ans=1;
  12. 12 while(n){
  13. 13 if(n&1) ans=(ans*a)%mod;
  14. 14 n>>=1;
  15. 15 a=(a*a)%mod;
  16. 16 }
  17. 17 return ans%mod;
  18. 18 }
  19. 19 int main(){
  20. 20 int k;
  21. 21
  22. 22 string n;
  23. 23 cin>>k;
  24. 24 while(k--){
  25. 25 cin>>n;
  26. 26 int len=n.size();
  27. 27 int m;
  28. 28 if(len>5){
  29. 29 string n1=n.substr(len-4,4); //起始,长度
  30. 30 m=atoi(n1.c_str()); //返回一个const *char,内容相同
  31. 31 }
  32. 32 else
  33. 33 m=atoi(n.c_str());
  34. 34 cout<<quickpow(2011,m%Mod)<<endl;
  35. 35 }
  36. 36 return 0;
  37. 37 }

2991:2011

转载于:https://www.cnblogs.com/XXrll/p/11104978.html

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读

    相关 fail-fast机制

    在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: > 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步

    相关 Fast Power

    一、 整数:求a^n,把n化为2\(k1+k2+..+km);  矩阵:注意矩阵乘法。 复杂度:O(logn) tip:取了模(根据题目要求) ![Contracte

    相关 Power Strings

    写在题目前:POJ太好用啦! 描述 给定两个字符串a和b,我们将\ b定义为它们的串联。例如,如果a =“abc”而b =“def”则a \ b =“abcdef”。如果我