模拟暴力解法失败,这个方法正确是正确的,但是提交的时候会运行超时

#include <stdio.h>
#include <iostream>
#pragma warning(disable:4996);
using namespace std;
const int maxn = 100010;
struct BigInteger {
	int digit[maxn];
	int length;
	BigInteger();
	BigInteger(int x);
	BigInteger(const BigInteger& b);
	BigInteger operator*(const BigInteger& b);
	BigInteger operator/(const BigInteger& b);
	BigInteger operator%(const BigInteger& b);
	BigInteger operator-(const BigInteger& b);
	bool operator<(const BigInteger& b);
	BigInteger convertToK(const BigInteger& k);
	int convertToint(const BigInteger& b);
	int sum();
	void printBigInteger();
};

BigInteger::BigInteger()
{
	fill(digit, digit + maxn, 0);
	length = 0;
}

BigInteger::BigInteger(const BigInteger& b)
{
	length = b.length;
	for (int i = 0; i < length; i++)
	{
		digit[i] = b.digit[i];
	}
}

BigInteger::BigInteger(int x)
{
	fill(digit, digit + maxn, 0);
	length = 0;
	while (x != 0)
	{
		digit[length++] = x % 10;
		x /= 10;
	}
}

bool BigInteger::operator<(const BigInteger& b)
{
	if (length < b.length)
		return true;
	if (length > b.length)
		return false;
	if (length == b.length)
	{
		for (int i = length - 1; i >= 0; i--)
		{
			if (digit[i] > b.digit[i])
				return false;
		}
		return true;
	}
}

BigInteger BigInteger::operator-(const BigInteger& b)
{
	int carray = 0;
	BigInteger answer;
	answer.length = length;
	for (int i = 0; i < length; i++)
	{
		int now = digit[i] - b.digit[i] - carray;
		carray = 0;
		if (now < 0)
		{
			now = now + 10;
			carray = 1;
		}
		answer.digit[i] = now;
	}
	while (answer.digit[answer.length - 1] == 0 && answer.length > 1) {
		answer.length--;
	}
	return answer;
}

BigInteger BigInteger::operator*(const BigInteger& b)
{
	BigInteger ans;
	ans.length = length + b.length;
	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < b.length; j++)
		{
			ans.digit[i + j] += digit[i] * b.digit[j];
		}
	}
	int carray = 0;
	for (int i = 0; i < ans.length; i++)
	{
		int now = carray + ans.digit[i];
		ans.digit[i] = now % 10;
		carray = now / 10;
	}
	//如果还有进位
	if (carray > 0)
		ans.digit[ans.length++] = carray;
	//然后开始除掉多余的0
	while (ans.digit[ans.length-1] == 0 && ans.length > 1)
	{
		ans.length--;
	}
	return ans;
}


BigInteger BigInteger::operator/(const BigInteger& b)
{
	//从高位开始相除
	BigInteger answer;
	BigInteger remainder;
	answer.length = length;
	BigInteger tempB(b);
	for (int i = length - 1; i >= 0; i--)
	{
		//看看余数的大小
		if (!(remainder.length == 1 && remainder.digit[0] == 0))
		{
			//如果remainder不是0的话
			for (int j = remainder.length - 1; j >= 0; j--)
			{
				remainder.digit[j + 1] = remainder.digit[j];
			}
			remainder.length++;
		}
		remainder.digit[0] = digit[i];
		while (tempB < remainder)
		{
			remainder = remainder - tempB;
			answer.digit[i]++;
		}
	}
	while (answer.digit[answer.length - 1] == 0 && answer.length > 1)
	{
		answer.length--;
	}
	return answer;
}

BigInteger BigInteger::operator%(const BigInteger& b)
{
	//从高位开始相除
	BigInteger answer;
	BigInteger remainder;
	answer.length = length;
	BigInteger tempB(b);
	for (int i = length - 1; i >= 0; i--)
	{
		//看看余数的大小
		if (!(remainder.length == 1 && remainder.digit[0] == 0))
		{
			//如果remainder不是0的话
			for (int j = remainder.length - 1; j >= 0; j--)
			{
				remainder.digit[j + 1] = remainder.digit[j];
			}
			remainder.length++;
		}
		remainder.digit[0] = digit[i];
		while (tempB < remainder)
		{
			remainder = remainder - tempB;
			answer.digit[i]++;
		}
	}
	return remainder;
}

int BigInteger::convertToint(const BigInteger& b)
{
	int ans = 0;
	int exp = 1;
	for (int i = 0; i < b.length; i++)
	{
		ans += exp * b.digit[i];
		exp *= 10;
	}
	return ans;
}

BigInteger BigInteger::convertToK(const BigInteger& k)
{
	BigInteger answer;
	BigInteger tempB(*this);
	while (!(tempB.length == 1 && tempB.digit[0] == 0))
	{
		BigInteger kkk = tempB % k;
		answer.digit[answer.length++] = convertToint(BigInteger(tempB % k));
		tempB = tempB / k;
	}
	return answer;
}
void BigInteger::printBigInteger()
{
	for (int i = length - 1; i >= 0; i--)
	{
		printf("%d", digit[i]);
	}
}

int BigInteger::sum()
{
	int answer = 0;
	for (int i = 0; i < length; i++)
	{
		answer += digit[i];
	}
	return answer;
}

BigInteger faseExp(int a, int b)
{
	BigInteger tempA(a);
	BigInteger Expans(1);
	while (b != 0)
	{
		if (b % 2 == 1)
		{
			Expans = Expans * tempA;
		}
		tempA = tempA * tempA;
		b = b / 2;
	}
	return Expans;
}

void Root(BigInteger n, BigInteger k)
{
	if (n < k)
	{
		n.printBigInteger();
        return;
	}
	else {
		BigInteger ansK = n.convertToK(BigInteger(k));
		int newN = ansK.sum();
		BigInteger N(newN);
		Root(newN, k);
	}
}

int main()
{
	int a, b, k;
	scanf("%d %d %d", &a, &b, &k);
	BigInteger ans = faseExp(a, b);
	BigInteger BigK(k);
	Root(ans, k);

}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐