[LightOJ-1220]

给出x,求满足x=b^p的最大p

int t;
int cnt, p[N];
bool book[N];
void x_x(ll n)
{
	f(i, 2, n)
	{
		if (!book[i])p[cnt++] = i;
		for (int j = 0;p[j] <= n / i;++j)
		{
			book[p[j] * i] = true;
			if (i%p[j] == 0)break;
		}
	}
}
int e[N],cot2;
void fenjie(ll n)
{
	for (int i = 0;i < cnt && (ll)p[i] * p[i] <= n;++i)
	{
		if (n%p[i] == 0)
		{
			int cot = 0;
			while (n%p[i] == 0)
			{
				n /= p[i];
				cot++;
			}
			e[cot2++] = cot;
		}
	}
	if (n > 1)e[cot2++] = 1;
}
int main()//给出x ,求满足x=B^P的最大p   ,举个例子  12^2=2^4*3^2  其实就是求分解质因子的指数的gcd把
{		//然后负数要把gcd搞成奇数   (-6)^2-》(-36)^1		
	cin >> t;
	x_x(N - 1);
	ll n;
	int c = 0;
	while (t--)
	{
		cin >> n;
		cot2 = 0;
		ll ans = 0;
		if (n > 0)
		{
			fenjie(n);
			int now = e[0];
			f(i, 1, cot2 - 1)
			{
				now = gcd(now, e[i]);
			}
			ans = now;
		}
		else
		{
			n = -n;fenjie(n);//负数出现次数为偶数的退成奇数   (-6)^2-》(-36)^1	
			int now = e[0];while (now % 2 == 0)now /= 2;
			f(i, 1, cot2 - 1)
			{
				while (e[i] % 2 == 0)e[i] /= 2;
				now = gcd(now, e[i]);
			}
			ans = now;
		}
		printf("Case %d: %lld\n", ++c, ans);
		
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 2 · 访问量 370

猜你喜欢

转载自blog.csdn.net/qq_43543086/article/details/104651852