必须感叹下,大数模板就是好用!

AC代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;

struct BigInteger {
	vector<int>s;  //12345--54321

	void DealZero() { //处理前导0
		for(int i = s.size() - 1; i > 0; --i){
			if(s[i] == 0) s.pop_back();
			else break;
		}
	}

 	BigInteger operator = (long long num) { // 赋值运算符
 		s.clear();
 		vector<int>tmp;
       	do{
       		s.push_back(num % 10);
       		num /= 10;
		}while(num);
		return *this;
    }

	BigInteger operator = (const string& str) { // 赋值运算符
    	s.clear();
   		for(int i = str.size() - 1; i >= 0; --i) s.push_back(str[i] - '0');
   		this->DealZero();
   		return *this;
    }

    BigInteger operator + (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
    	for(int i = 0, g = 0; g > 0 || i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		if(i < len2) x += b.s[i];
    		c.s.push_back(x % 10);
    		g = x / 10;
		}
    	return c;
   }

   //大数减小数
   BigInteger operator - (const BigInteger& b) const {
    	BigInteger c;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
    	for(int i = 0, g = 0; i < len1 || i < len2; ++i) {
    		int x = g;
    		if(i < len1) x += s[i];
    		g = 0;
    		if(i < len2) x -= b.s[i];
    		if(x < 0) {
    			g = -1; //借位
    			x += 10;
			}
    		c.s.push_back(x);
		}
		c.DealZero();
    	return c;
   }

   BigInteger operator * (const BigInteger& b) const {
   		BigInteger c, tmp;
    	c.s.clear();
    	int len1 = s.size(), len2 = b.s.size();
   		for(int i = 0; i < len1; ++i) {
   			tmp.s.clear();tmp;
   			int num = i;
			while(num--) tmp.s.push_back(0);
			int g = 0;
			for(int j = 0; j < len2; ++j) {
				int x = s[i] * b.s[j] + g;
				tmp.s.push_back(x % 10);
				g = x / 10;
			}
			if(g > 0) tmp.s.push_back(g);
			c = c + tmp;
		}
		c.DealZero();
		return c;
   }

   //单精度除法
   BigInteger operator / (const int b) const {
   		BigInteger c, tmp;
   		c.s.clear();
   		int len = s.size();
   		int div = 0;
   		for(int i = len - 1; i >= 0; --i) {
   			div = div * 10 + s[i];
   			while(div < b && i > 0) {
   				div = div * 10 + s[--i];
			}
			tmp.s.push_back(div / b);
			div %= b;
		}
		for(int i = tmp.s.size() - 1; i >= 0; --i) c.s.push_back(tmp.s[i]);
		c.DealZero();
		return c;
   } 

   bool operator < (const BigInteger& b) const {
   	   	int len1 = s.size(), len2 = b.s.size();
   	   	if(len1 != len2) return len1 < len2;
   	   	for(int i = len1 - 1; i >= 0; --i) {
   	   		if(s[i] != b.s[i]) return s[i] < b.s[i];
		}
		return false; //相等
   }

   bool operator <= (const BigInteger& b) const {
   		return !(b < *this);
   }
   string ToStr() {
   		string ans;
   		ans.clear();
   		for(int i = s.size()-1; i >= 0; --i)
   			ans.push_back(s[i] + '0');
   		return ans;
   }

   //大数开方
/**大数开方用法说明:
   字符串必须从第二个位置开始输入,且s[0] = '0'
   scanf("%s", s+1);
*/
   BigInteger SQRT(char *s) {
   		string p = "";
		s[0]='0';
    	if(strlen(s)%2 == 1)
        	work(p, 2, s+1, 0);
    	else
        	work(p, 2, s, 0);
   		BigInteger c;
   		c.s.clear();
   		c = p;
   		return c;
   }

//开方准备
//------------------------------------
	int l;
	int work(string &p, int o,char *O,int I){
	    char c, *D=O ;
	    if(o>0)
	    {
	        for(l=0;D[l];D[l++]-=10)
	        {
	            D[l++]-=120;
	            D[l]-=110;
	            while(!work(p, 0, O, l))
	                D[l]+=20;
	            p += (char)((D[l]+1032)/20);

	        }
	    }
	    else
	    {
	        c=o+(D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9;
	        D[I]+=I<0 ? 0 : !(o=work(p, c/10,O,I-1))*((c+999)%10-(D[I]+92)%10);
	    }
	    return o;
	}
//-----------------------------------------
};

ostream& operator << (ostream &out, const BigInteger& x) {
	for(int i = x.s.size() - 1; i >= 0; --i)
		out << x.s[i];
	return out;
}

istream& operator >> (istream &in, BigInteger& x) {
  string s;
  if(!(in >> s)) return in;
  x = s;
  return in;
}

int main() {
	BigInteger a, b;
	while(cin >> a >> b) {
		cout << a * b << "\n";
	}
	return 0;
}

如有不当之处欢迎指出!

算法提高 P1001的更多相关文章

  1. 蓝桥杯算法提高 P1001(大数乘法)

      算法提高 P1001   时间限制:1.0s   内存限制:256.0MB   当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法. 具体 ...

  2. Java实现 蓝桥杯 算法提高 p1001

    算法提高 P1001 时间限制:1.0s 内存限制:256.0MB 提交此题  当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说 ...

  3. 算法提高 P1001【大数乘法】

    当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说,首先以字符串的形式输入两个整数,每个整数的长度不会超过8位,然后把它们相乘的结果 ...

  4. 算法笔记_097:蓝桥杯练习 算法提高 P1001(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 当两个比较大的整数相乘时,可能会出现数据溢出的情形.为避免溢出,可以采用字符串的方法来实现两个大数之间的乘法.具体来说,首先以字符串的形式输入两个整 ...

  5. 机器学习实战 - 读书笔记(07) - 利用AdaBoost元算法提高分类性能

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习笔记,这次是第7章 - 利用AdaBoost元算法提高分类性能. 核心思想 在使用某个特定的算法是, ...

  6. 算法提高 c++_ch02_01

    http://lx.lanqiao.org/problem.page?gpid=T237  算法提高 c++_ch02_01   时间限制:1.0s   内存限制:512.0MB      编写一个程 ...

  7. 算法提高 9-3摩尔斯电码 map

    算法提高 9-3摩尔斯电码 时间限制:1.0s   内存限制:256.0MB     问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...

  8. 算法提高 金属采集_树形dp

    算法提高 金属采集   时间限制:1.0s   内存限制:256.0MB        问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫它节点好了.一些节点之间有道路相连 ...

  9. 算法提高 最小方差生成树(Kruskal)_模板

     算法提高 最小方差生成树   时间限制:1.0s   内存限制:256.0MB        问题描述 给定带权无向图,求出一颗方差最小的生成树. 输入格式 输入多组测试数据.第一行为N,M,依次是 ...

随机推荐

  1. python_爬百度百科词条

    如何爬取? 明确目标:爬取百度百科,定初始百度词条:python,初始URL:http://baike.baidu.com/item/Python,爬取数据量为1000条,值爬取简介,标题,和简介中u ...

  2. hibernate解读之session--基于最新稳定版5.2.12

    前言 hibernate是一个实现了JPA标准的,用于对象持久化的orm框架.博主近一年开发都在使用. 前段时间在工作中遇到了一个hibernate的问题,从数据库查找得到对象后,修改了其中部分字段值 ...

  3. Git初入

    Git记录 使用git 也有一段时间了, git的入门级了解也就不再多说, 但平常使用中, 仍然会遇到很多问题, 在此记录一二. 在查资料的过程中, 发现了两个比较好的资料: 特别是第二个, 相当详细 ...

  4. 用CSS写气泡

    新学到的一个小效果 用CSS实现如下图效果,其中demo结构为:<div id="square"></div> 实现这个效果需要用到两个知识点: 1.用bo ...

  5. Linux中的shell到底是什么

    (引自:https://zhidao.baidu.com/question/557066905.html) [一] shell的含义: 首先shell的英文含义是"壳": 它是相对 ...

  6. GitHub For Beginners: Commit, Push And Go

    In Part 1 of this two-part GitHub tutorial, we examined the main uses for GitHub and bega5n the proc ...

  7. absort函数和exit函数

    1. exit()函数会结束并退出程序. 1. abosrt()函数会触发程序的异常,然后程序后面的语句就不会执行了.用来提示错误.会出现:  

  8. 通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值

    通过重写 class 的 ToString() 来简化获取 enum 的 DescriptionAttribute 值 目录 一.常见的 enum 类型 二.演变:class 版本的 enum 类型 ...

  9. shiro权限控制(二):分布式架构中shiro的实现

    前言:前段时间在搭建公司游戏框架安全验证的时候,就想到之前web最火的shiro框架,虽然后面实践发现在netty中不太适用,最后自己模仿shiro写了一个缩减版的,但是中间花费两天时间弄出来的shi ...

  10. 有标号DAG计数 [容斥原理 子集反演 组合数学 fft]

    有标号DAG计数 题目在COGS上 [HZOI 2015]有标号的DAG计数 I [HZOI 2015] 有标号的DAG计数 II [HZOI 2015]有标号的DAG计数 III I 求n个点的DA ...