洛谷 P1045 麦森数 (快速幂+高精度+算位数骚操作)
这道题太精彩了!
我一开始想直接一波暴力算,然后叫上去只有50分,50分超时
然后我改成万位制提高运算效率,还是只有50分
然后我丧心病狂开long long用10的10次方作为一位,也就是100亿进制
去做,然后交上去多过了一个点,60分
附上丧心病狂的代码
#include<cstdio>
#include<cctype>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 1123456 / 10;
const ll base = 1e10;
ll a[MAXN];
int len, n;
void cal()
{
_for(i, 1, len) a[i] <<= 1;
_for(i, 1, len)
{
a[i+1] += a[i] / base;
a[i] %= base;
}
if(a[len+1])
{
len++;
a[len+1] += a[len] / base;
a[len] %= base;
}
}
int main()
{
scanf("%d", &n);
len = 1; a[1] = 2;
REP(i, 1, n) cal();
int p = 1;
a[p] -= 1;
while(a[p] < 0)
{
a[p + 1]--;
a[p] += base;
p++;
}
if(a[len] == 0) len--;
ll t = a[len], num = 0;
while(t) t /= 10, num++;
printf("%d\n", len * 10 - 10 + num);
for(int i = 50, t = 0; i >= 1; i--, t++)
{
if(t == 5) puts(""), t = 0;
printf("%010lld", a[i]);
}
return 0;
}
然后我最后还是看了题解
然后看到算位数真的是给折服的
还有这种操作???
由于10的x次方的位数是x+1
然后可以把2的p次方转化成类似10的x次方来算位数
然后这个时候就用到了log
一波骚操作可以得出位数就是p * lg(2) + 1
牛逼!
然后算前500位。
我竟然没看出这是个快速幂??????
用高精度做快速幂还是头一次。
不过稍微改一下就好了。
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
typedef long long ll;
const int MAXN = 1123;
struct bignum
{
ll s[MAXN]; int len;
bignum() { memset(s, 0, sizeof(s)); len = 1; }
};
bignum operator * (const bignum& a, const bignum& b)
{
bignum c;
c.len = min(a.len + b.len - 1 , 500);
_for(i, 1, a.len)
_for(j, 1, b.len)
{
c.s[i+j-1] += a.s[i] * b.s[j];
c.s[i+j] += c.s[i+j-1] / 10;
c.s[i+j-1] %= 10;
}
if(c.s[c.len+1] && c.len < 500) c.len++;
return c;
}
int main()
{
int b;
scanf("%d", &b);
printf("%d\n", (int)(b * log10(2)+ 1));
bignum res; res.s[1] = 1;
bignum a; a.s[1] = 2;
while(b)
{
if(b & 1) res = res * a;
b >>= 1;
a = a * a;
}
res.s[1] -= 1;
for(int i = 500; i >= 1; i--)
{
if(i != 500 && i % 50 == 0) puts(""); //这个写法很简便
printf("%d", res.s[i]);
}
puts("");
return 0;
}
洛谷 P1045 麦森数 (快速幂+高精度+算位数骚操作)的更多相关文章
- 洛谷 P1045 麦森数
题目描述 形如2^{P}-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^{P}-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=30213 ...
- 洛谷P1045 麦森数
题目描述 形如2^{P}-12 P −1的素数称为麦森数,这时PP一定也是个素数.但反过来不一定,即如果PP是个素数,2^{P}-12 P −1不一定也是素数.到1998年底,人们已找 ...
- NOIP2003 普及组 洛谷P1045 麦森数 (快速幂+高精度)
有两个问题:求位数和求后500位的数. 求位数:最后减去1对答案的位数是不影响的,就是求2p的位数,直接有公式log10(2)*p+1; 求后500位的数:容易想到快速幂和高精度: 1 #includ ...
- P1045麦森数
P1045麦森数 #include<iostream> #include <cmath> #include <cstring> const int maxn = 1 ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂
洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...
- 【题解】[P1045] 麦森数
题目 题目描述 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1 不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=30213 ...
- 洛谷P1226 【模板】快速幂||取余运算
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 S1: ...
- 洛谷P1313 计算系数【快速幂+dp】
P1313 计算系数 题目描述 给定一个多项式(by+ax)^k,请求出多项式展开后x^n*y^m 项的系数. 输入输出格式 输入格式: 输入文件名为factor.in. 共一行,包含5 个整数,分别 ...
随机推荐
- why updating the Real DOM is slow, what is Virtaul DOM, and how updating Virtual DOM increase the performance?
个人翻译: Updating a DOM is not slow, it is just like updating any JavaScript object; then what exactly ...
- CommonJS 与 ES6 的依赖操作方法(require、import)
CommonJS:http://www.commonjs.org/specs/modules/1.0/ ES2015的 export:https://developer.mozilla.org/en- ...
- SQL中Group By的使用(转)
1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...
- node工具是是什么东西
Node到底是个啥? Node是一个服务器端JavaScript解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不深我还不知道,不过确实不浅 最近写 ...
- 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
作者: Yi Ming Huang, 软件工程师, IBM Dong Fei Wu, 软件工程师, IBM Qing Guo, 软件工程师, IBM 出处: http://www.ibm.com/de ...
- thinkphp queue
composer create-project topthink/think composer require topthink/think-queue php think queue:work -- ...
- leetcode小题解析
描述Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- VUE:class与style强制绑定
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ASP.NET-关于Global.asax的作用
这个文件相当于一个应用程序量级的 全局文件,比如你想写一个变量在项目中的所有文件中都能读取是就写在这里面 Application["name"] = "zhangran& ...
- maven打包可运行的jar包(包含依赖工程)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...