大神的字符串快速幂

 #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std; //字符串的乘法
string multi(string a, string b){
int i, j, arr[], len = a.length() + b.length();
memset(arr, , sizeof arr);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (int i = ; i<a.length(); i++)
for (int j = ; j<b.length(); j++)
arr[i + j] += (a[i] - '')*(b[j] - '');
for (int i = ; i<len; i++){
arr[i + ] += arr[i] / ;
arr[i] %= ;
}
string ret = string(len, '');
for (int i = ; i<len; i++)
ret[i] += arr[i];
reverse(ret.begin(), ret.end());//翻转
return ret;
}
string Str_Pow(string x, int p){
string ret = "";
while (p){
if (p & ) ret = multi(ret, x);
x = multi(x, x);
p >>= ;
}
return ret;
}
int main(){
string a;
int n;
while (cin >> a >> n){
int index = a.find('.');
if (index == -) index = ;
else {
//substr() 是字符串截取函数
a = a.substr(, index) + a.substr(index + );
index = (a.length() - index)*n;
}
a = Str_Pow(a, n);
a = a.substr(, a.length() - index) + "." + a.substr(a.length() - index);
int i;
for (i = ; i<a.length(); i++)
if (a[i] != '')
break;
//substr(i) 从i到end
a = a.substr(i);
for (int i = a.length() - ; i >= ; i--){
if (a[i] == '.'){
a = a.substr(, i);
break;
}
else if (a[i] != ''){
a = a.substr(, i + );
break;
}
}
cout << a << endl;
}
return ;
}

51Nod 1873 初中的算术的更多相关文章

  1. 51nod 1873 初中的算术【Java BigDecimal/高精度小数】

    1873 初中的算术 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 Noder现在上初三了,正在开始复习中考.他每天要计算型如 (a× a× a× ...

  2. 1873 初中的算术(java大数)

    1873 初中的算术 1 秒 131,072 KB 10 分 2 级题   Noder现在上初三了,正在开始复习中考.他每天要计算型如 (a× a× a× ⋯× a) ...

  3. 51nod 1873 高精度计算

    JAVA BigDecimal import java.util.*; import java.math.*; public class Main { public static void main( ...

  4. 51nod 1189 算术基本定理/组合数学

    www.51nod.com/onlineJudge/questionCode.html#!problemId=1189 1189 阶乘分数 题目来源: Spoj 基准时间限制:1 秒 空间限制:131 ...

  5. 51Nod 1005 有负数的高精度加法

    51Nod是个好地方啊 题意 51Nod基础题第二题,高精度加法,可能有负数. 解题 如果按照一般的高精度,我们发现要分情况讨论,还要写高精度加法和减法,代码实现有点烦.而初中数学里说,省略加号的和. ...

  6. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  7. 51Nod 1268 和为K的组合

    51Nod  1268  和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...

  8. 51Nod 1428 活动安排问题

    51Nod   1428  活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...

  9. 51Nod 1278 相离的圆

    51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...

随机推荐

  1. UIAutomator 2

    UIAutomator 2 While the API remains almost the same, the internal implementation has changed and we ...

  2. Oracle查询正在执行的SQL语句

    查看 Oracle 正在执行的 sql 语句以及发起的用户 SELECT b.sid oracleID, b.username 用户名, b.serial#, paddr, sql_text 正在执行 ...

  3. LightOJ1220 —— 质因数分解

    题目链接:https://vjudge.net/problem/LightOJ-1220 1220 - Mysterious Bacteria    PDF (English) Statistics ...

  4. lvs+keepalived+nginx高性能负载均衡集群

    项目发布时候,别人还能访问呢? 双机主从热备 LVS作用 LVS是一个开源的软件,可以实现传输层四层负载均衡.LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器.目前有 ...

  5. Swing项目编译成exe,并且打包成安装文件(二)

    前面我们讲到了将Swing项目编译成双击可执行的文件exe,这篇我就教大家怎么把exe打包成需要在电脑安装的那种,首先需要一个工具,Inno Setup 编译器, 下载地址,我这个是汉化版的,双击打开 ...

  6. hadoop源码剖析--RawLocalFileSystem

    RawLocalFileSystem是hadoop中实现的本地文件系统,在该类中与文件元数据和目录相关的操作,都是通过适配方式适配到java.io.File的对应API来完成的,适配过程简单,代码清晰 ...

  7. zookeeper入门到精通

  8. Battle Ships(复习泛化物品**)

    传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which i ...

  9. [转]Eclipse创建Maven项目

    构建Maven项目的完整过程--普通web项目(Eclipse) 进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) ...

  10. 4月超棒的JavaScript游戏开发框架推荐(1) – 51CTO.COM

    基于JavaScript开发的游戏是唯一一个能够跨桌面,Web和移动三种平台的.… 查阅全文 ›