百练#2706麦森数

描述
形如2p-1的素数称为麦森数,这时P一定也是个素数。但反过来不一定,即如果P是个素数。2p-1不一定也是素数。到1998年底,人们已找到了37个麦森数。最大的一个是P=3021377,它有909526位。麦森数有许多重要应用,它与完全数密切相关。
任务:从文件中输入P (1000<P<3100000) ,计算2p-1的位数和最后500位数字(用十进制高精度数表示)
输入
文件中只包含一个整数P(1000<P<3100000)
输出
第1行:十进制高精度数2p-1的位数。
第2-11行:十进制高精度数2p-1的最后500位数字。(每行输出50位,共输出10行,不足500位时高位补0)
不必验证2p-1与P是否为素数。
样例输入

1279

样例输出

386
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000104079321946643990819252403273640855
38615262247266704805319112350403608059673360298012
23944173232418484242161395428100779138356624832346
49081399066056773207629241295093892203457731833496
61583550472959420547689811211693677147548478866962
50138443826029173234888531116082853841658502825560
46662248318909188018470682222031405210266984354887
32958028878050869736186900714720710555703168729087

#include<iostream>
#include<cstring>
#include<cmath>
#define LEN 125
using namespace std;
int result[LEN];
int tpow[LEN];
void mul(int *a,int *b){
	int temp[LEN];
	int jin,t;
	memset(temp,0,sizeof(int) * LEN);
	for(int i = 0;i < LEN; ++i){
		jin = 0;
		for(int j = 0;j < LEN - i; ++j){
			t = temp[i + j] + a[i] * b[j] + jin;
			temp[i + j] = t % 10000;
			jin = t / 10000; 
		}
	}
	memcpy(a,temp,sizeof(int) * LEN);
}
int main(){
	int p;
	int i,j;
	scanf("%d",&p);
	printf("%d\n",(int)(p * log10(2)) + 1);
	memset(result,0,sizeof(int) * LEN);
	result[0] = 1;
	memset(tpow,0,sizeof(int) * LEN);
	tpow[0] = 2;
	while(p > 0){
		if(p & 1){
			mul(result,tpow);
		}
		mul(tpow,tpow);
		p >>= 1;
	}
	result[0]--;
	j = 0;
	for(i = LEN - 1;i >= 0; --i){
		if((j + 2) % 50 == 0)
			printf("%02d\n%02d",result[i] / 100,result[i] % 100);
		else if((j + 4) % 50 == 0)
			printf("%04d\n",result[i]);
		else
			printf("%04d",result[i]);
		j += 4;
	}
	printf("\n");
	return 0;
}
 

错误:只会TLE的方法
别人的代码:
1.万进制数,缩减时间;
2.位运算缩减时间。
就是一个求指数的模板。

看了紫书后,借鉴紫书上的大整数类模板:

#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
struct BigInteger{
	static const int BASE = 10000;
	static const int WIDTH = 4;
	static const int MAX = 125;
	vector<int> s;
	BigInteger(long long num = 0){
		*this = num;
	}
	BigInteger operator = (long long num){
		s.clear();
		do{
			s.push_back(num % BASE);
			num /= BASE;
		}while(num > 0);
		return *this;
	}
	BigInteger operator = (const string& str){
		s.clear();
		int len = str.length();
		int x,l = (len - 1) / WIDTH + 1;
		for(int i = 0;i < l; ++i){
			int end = len - i * WIDTH;
			int start = max(0, end - WIDTH);
			sscanf(str.substr(start, end - start).c_str(),"%d",&x);
			s.push_back(x);
		}
		return *this;
	}
	BigInteger operator + (const BigInteger& b) const{
		BigInteger c;
		c.s.clear();
		int al = s.size();
		int bl = b.s.size();
		for(int i = 0,g = 0;; ++i){
			if(g == 0 && i >= al && i >= bl)
				break;
			int x = g;
			if(i < al)
				x += s[i];
			if(i < bl)
				x += b.s[i];
			c.s.push_back(x % BASE);
			g = x / BASE;
		}
		return c;
	}
	BigInteger operator * (const BigInteger& b) const {
		BigInteger c;
		c.s.clear();
		int d[MAX];
		memset(d,0,sizeof(d));
		int al = s.size();
		int bl = b.s.size();
		for(int i = 0;i < MAX && i < al; ++i){
			int g = 0;
			for(int j = 0;j < MAX && j < bl; ++j){
				if(i + j < MAX){
					int x = d[i + j] + s[i] * b.s[j] + g;
					d[i + j] = x % BASE;
					g = x / BASE;
				}
			}
		}
		for(int i = 0;i < MAX; ++i){//如果按照粉书上的方法,全部求好后再算进位是容易溢出而WA的。
			c.s.push_back(d[i]);
		}
		return c;
	}
};
int main(){
	int p,i,j;
	scanf("%d",&p);
	printf("%d\n",(int)(log10(2) * p) + 1);
	BigInteger res = 1;
	BigInteger x = 2;
	while(p > 0){
		if(p & 1){
			res = res * x;
		}
		x = x * x;
		p >>= 1;
	}
	res.s[0]--;
	j = 0;
	for(int i = BigInteger::MAX - 1;i >= 0; --i){
		if(j % 25 == 12)
			printf("%02d\n%02d",res.s[i] / 100,res.s[i] % 100);
		else{
			printf("%04d",res.s[i]);
			if(i % 25 == 0)
				printf("\n");
		}
		++j;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值