BeyondStars @ 2018-07-23 11:51:23
#include <iostream>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
void mul(int a[],int b[]){
int ans[1001];
memset(ans,0, sizeof(ans));
for(register int i = 1;i<=500;i++)
for(register int j = 1;j<=500;j++)
ans[i+j-1] += a[i]*b[j];
for(int i = 1;i<=500;i++){
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
for(int i = 1;i<=500;i++)a[i] = ans[i];
}
int main() {
int p;
cin>>p;
cout<<ceil(log10(2)*p)<<endl;
int a[501];
int c[501];
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
a[1] = c[1] = 2;
while(p>=1){
if(p==1){
mul(a,c);
break;
}
if(p%2==1)mul(a,c);
p=p>>1;
mul(a,a);
}
for(int i = 500;i>=1;i--)cout<<a[i];
return 0;
}
mul是做乘法的函数,c数组是储存值2,我的思路是当遇到奇数次幂,就p-1,然后乘2,偶数次幂就直接平方然后除以2.
谢谢大佬orz
by _King__ @ 2018-09-01 19:34:50
@DengHaoyu 你没注意题目中的神奇输出格式吗?