原题链接:蓝桥杯算法训练VIP-麦森数
先附上大佬对快速幂的解读:
https://blog.csdn.net/qq_19782019/article/details/85621386
嘤嘤嘤,大佬tql!
本题使用的方法就是快速幂和数组的大数乘法,看懂了之后会发现都不是特别难。
考试加油!!
参考代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<set>
#include<sstream>
#include<cstring>
#include<utility>
using namespace std;
typedef long long ll;
typedef long l;
const int N = 200020;
int a[510],b[510],m;
void xx(int x[510],int y[510]){
int c[510],k=0,t;
memset(c,0,sizeof(c));
for(int i=0;i<=499;i++){
k=0;
for(int j=0;j<=499;j++){
t=x[i]*y[j];
c[i+j]=c[i+j]+t+k;
k=c[i+j]/10;
c[i+j]%=10;
if(i+j>=500)break;
}
}
memcpy(x,c,sizeof(c));
}
int main(){
cin>>m;a[0]=1;b[0]=2;
int h=m*log10(2);cout<<h+1<<endl;
while(m>0){
if(m&1){
xx(a,b);
}
xx(b,b);
m>>=1;
}
for(int i=499,j=1;i>=0;i--,j++){
if(i!=0)cout<<a[i];
else cout<<a[i]-1;
if(j%50==0)cout<<endl;
}
}0.0分
2 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复