Jelajahi Sumber

Multiplication

mawinkle 6 tahun lalu
induk
melakukan
fe4b3519ce
3 mengubah file dengan 34 tambahan dan 25 penghapusan
  1. 29 19
      BigInt.hpp
  2. 2 2
      semi_bitset.hpp
  3. 3 4
      test.cpp

+ 29 - 19
BigInt.hpp

@@ -3,6 +3,7 @@
 #include "semi_bitset.hpp"
 #include <cstdlib>
 #include <string>
+#include <iostream>
 template<size_t size>
 class BigInt{
 private:
@@ -42,27 +43,35 @@ public:
 		return ret;
 	}
 	template<size_t osize>
-	inline BigInt<size> mult(const BigInt<osize>& o)const{
+	inline BigInt<size + osize> mult(const BigInt<osize>& o)const{
 		BigInt<size + osize> ret;
 		uint64_t mat[size][size] = {0};
 		uint64_t carry = 0;
 		for(size_t i = 0;i < size;i++){
 			for(size_t ih = size - 1;ih != ~(0ULL);ih--){
-				mat[i][ih] = (*this)[i] * o[ih] + carry;
-				carry = mat[i][ih] >> 32;
-				mat[i][ih] &= lower_half;
+				mat[size - i - 1][ih] = (*this)[i] * o[ih] + carry;
+				carry = mat[size - i - 1][ih] >> 32;
+				mat[size - i - 1][ih] &= lower_half;
 				if(ih == 0)break;
 			}
 		}
+		for(size_t i = 0;i < size;i++){
+			for(size_t ih = 0;ih < size;ih++){
+				std::cout << mat[i][ih] << " ";
+			}
+			std::cout << std::endl;
+		}
 		carry = 0;
-		for(size_t i = size - 1;i != ~(0ULL);i--){
+		for(std::int64_t i = size - 1;i > -((std::int64_t)size);i--){
 			uint64_t accum = 0;
-			for(size_t ih = 0;ih < size;ih++){
-				accum += ((i + ih) >= size) ?  0 : mat[i + ih][ih];
+			for(std::int64_t ih = 0;ih < size;ih++){
+				accum += ((i + ih) >= size || (i + ih) < 0) ?  0 : mat[ih][i + ih];
 			}
 			accum += carry;
-			ret[i] = accum & lower_half;
+			ret[i + osize] = accum & lower_half;
+			//std::cout << accum << " ";
 			carry = accum >> 32;
+			if(i == 0)break;
 		}
 		return ret;
 	}
@@ -97,15 +106,16 @@ public:
 		}
 		return true;
 	}
-	template<typename stream, size_t osize>
-	inline friend stream& operator<<(stream& s, const BigInt<osize>& o){
-		std::string a = "";
-		BigInt dis = o;
-		while(!dis.isZero()){
-			a = std::to_string(dis.mod(10)) + a;
-			dis = dis.div(10);
-		}
-		return s << a;
-	}
+	
 };
-#endif
+template<size_t osize>
+inline std::ostream& operator<<(std::ostream& s, const BigInt<osize>& o){
+	std::string a = "";
+	BigInt<osize> dis = o;
+	while(!dis.isZero()){
+		a = std::to_string(dis.mod(10)) + a;
+		dis = dis.div(10);
+	}
+	return s << a;
+}
+#endif

+ 2 - 2
semi_bitset.hpp

@@ -266,11 +266,11 @@ struct semi_bitset<size, 0>{
 		}
 		return *this;
 	}
-	template<typename stream, size_t osize,bool ocontainertype>
+	/*template<typename stream, size_t osize,bool ocontainertype>
 	inline friend stream& operator<<(stream& s, const semi_bitset<osize, ocontainertype>& o){
 		for(size_t i = 0;i < osize;i++)
 			s << std::bitset<32>((uint32_t)(o.data[i] & lower_half));
 		return s;
-	}
+	}*/
 };
 #endif

+ 3 - 4
test.cpp

@@ -21,11 +21,10 @@ std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
 	return out;
 }
 int main(){
-	BigInt<100> a ((1ULL << 40) + 1);
-	std::cout << a << std::endl;
-	for(int i = 0;i < 1000;i++)
-	a = a.add(a);
+	BigInt<3> a ((unsigned int)(1e7 + 345345));
 	std::cout << a << std::endl;
+	auto b = a.mult(a);
+	std::cout << b << std::endl;
 }
 int mian(){
 	cppsocket sock("192.168.178.79", 80);