Exemple #1
0
const LI LI :: operator / (const int & second)
{
	LI res;
	LI zero;
	zero.From_Int(0);
	if (second == 0)
	{
		cout << "Div by 0\n";
		exit(1);
	}
	if ((*this).active_n == 1)
	{
		int r = (*this).digits[0] / second;
		res.From_Int(r);
		return res;
	}
	
	res.active_n = (*this).active_n;
	int ost = 0, dig;
	for (int i = 0; i < (*this).active_n; ++i)
	{
		dig = ost*p + (*this).digits[(*this).active_n - 1 - i];
		res.digits[(*this).active_n - 1 - i] = dig / second;
		ost = dig - res.digits[(*this).active_n - 1 - i] * second;
	}
	while (res.active_n > 1 && res.digits[res.active_n - 1] == 0)
		--res.active_n;
	return res;
}
Exemple #2
0
const LI LI :: powLI (const int & n)
{
	LI one;
	one.From_Int(1);
	LI res = one;
	for (int i = 0; i <= n; ++i)
		res = res.Karatsuba (*this);
	return res;
}
Exemple #3
0
const LI LI ::  gcd(const LI & second)
{
	LI a = (*this), b = second;
	LI zero;
	zero.From_Int(0);
	if (a == zero) return b;
	if (b == zero) return a;
	while (b != zero)
	{
			a = a % b;
			a.swap(b);
	}
		return a;
}
Exemple #4
0
const LI LI :: pow_mod(LI & n, const LI & mod)
{
	LI res, zero, one, two, a = (*this);
	zero.From_Int(0);
	one.From_Int(1);
	two.From_Int(2);
	res = one;
	while (n != zero)
	{
		LI d;
		d.From_Int(n%2);
		if (d == zero)
		{
			n = n /	2;
			a = (a * a) % mod;
		}
		else
		{
			n = n - one;
			res = (res * a) % mod;
		}
	}
	return res;
}