zoukankan      html  css  js  c++  java
  • BigInt类

    持续维护中,根据需求更新。

    struct BigInt {
    	int l, a[23333], base;
    	BigInt() {
    		l = 0, base = 10;
    		memset(a, 0, sizeof(a));
    	}
    	BigInt Trans(int x) {
    		BigInt y;
    		while (x)
    			y.a[++y.l] = x % y.base, x /= y.base;
    		return y;
    	}
    	friend BigInt operator +(BigInt x, BigInt y) {
    		BigInt z;
    		z.l = std::max(x.l, y.l);
    		for (int i = 1; i <= z.l; i++)
    			z.a[i] += x.a[i] + y.a[i], z.a[i + 1] += z.a[i] / x.base, z.a[i] %= x.base;
    		if (z.a[z.l + 1]) z.l++;
    		return z;
    	}
    	friend BigInt operator +(BigInt x, int y) {
    		BigInt tmp = tmp.Trans(y);
    		return x + tmp;
    	}
    	friend BigInt operator -(BigInt x, BigInt y) {
    		BigInt z;
    		z.l = std::max(x.l, y.l);
    		for (int i = 1; i <= z.l; i++) {
    			if (x.a[i] < y.a[i]) x.a[i] += x.base, x.a[i + 1]--;
    			z.a[i] = x.a[i] - y.a[i];
    		}
    		while (!z.a[z.l] && z.l) z.l--;
    		if (z.l == 0) z.a[1] = 1, z.l = 1;
    		return z;
    	}
    	friend BigInt operator *(BigInt x, BigInt y) {
    		BigInt z;
    		z.l = x.l + y.l;
    		if ((x.l == 1 && x.a[1] == 0) || (y.l == 1 && y.a[1] == 0)) {
    			z.l = 1;
    			return z;
    		}
    		for (int i = 1; i <= x.l; i++)
    			for (int j = 1; j <= y.l; j++)
    				z.a[i + j - 1] += x.a[i] * y.a[j], z.a[i + j] += z.a[i + j - 1] / x.base, z.a[i + j - 1] %= x.base;
    		while (!z.a[z.l] && z.l) z.l--;
    		if (!z.l) {z.l = 1, z.a[1] = 0;}
    		return z;
    	}
    	friend BigInt operator *(BigInt x, int y) {
    		BigInt z; int l = x.l;
    		for (int i = 1; i <= l; i++)
    			z.a[i] += x.a[i] * y, z.a[i + 1] += z.a[i] / x.base, z.a[i] %= x.base;
    		while (z.a[l + 1])
    			l++, z.a[l + 1] += z.a[l] / x.base, z.a[l] %= x.base;
    		z.l = l;
    		while (!z.a[z.l]) z.l--;
    		return z;
    	}
    	friend BigInt operator /(BigInt x, int y) {
    		BigInt z; z.l = x.l;
    		int t = 0;
    		for (int i = x.l; i >= 1; i--)
    			t = t * 10 + x.a[i], z.a[i] = t / y, t %= y;
    		while (!z.a[z.l]) z.l--;
    		return z;
    	}
    	void print() {
    		for (int i = l; i >= 1; i--)
    			printf("%d", a[i]);
    		printf("
    ");
    	}
    };
    
  • 相关阅读:
    General procedures for upgrading Debian
    Install documentation for GCC on Debian Buster
    本地学习环境minikube安装
    解决phpstorm中ftp读取远程目录出现嵌套循环情况
    Linux 文件句柄&文件描述符
    VBA如何实现筛选条件之“排除某些值”
    利用Python多线程快速爬取某网站数据
    利用云服务器搭建远程办公访问(frp实现内网穿透)
    printf()函数
    malloc和calloc的区别
  • 原文地址:https://www.cnblogs.com/gekoo/p/11222781.html
Copyright © 2011-2022 走看看