SDU ACM实验室 2019新生赛——K.史莱尼克号【唯一分解定理 快速幂加速(选用)】

在这里插入图片描述


题解

  • 由题意可知只有 2 操作2 是减小总重量的,而 1 , 3 操作1,3 a b a,b 交换因子,所以 a b a*b 的偶数个因子都可以除掉,最后剩下的就是答案。
  • 利用唯一分解定理、快速幂加速程序

AC-Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;

vector<pair<ll, int>> prime_factors(ll n) {
	vector<pair<ll, int>> f;
	int k = sqrt(n) + 1;
	ll p=0;	int q;
	for (ll i = 2; i <= k; ++i) {
		q = 0;		if (n % i == 0)	p = i;
		while (n % i == 0)	++q, n /= i;
		if (p)	f.push_back(make_pair(p, q));
	}
	if (n > 1)	f.push_back(make_pair(n, 1));
	return f;
}

ll q_pow(ll a, ll n) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) 	res = res * a;
		a *= a;
		n >>= 1;
	}
	return res;
}

int main() {
	int T;	cin >> T;
	while (T--) {
		ll a, b;	cin >> a >> b;
		ll ans = a * b;
		vector<pair<ll, int>> f = prime_factors(ans);
		for (auto i : f) {
			if (i.second % 2 == 0)	ans /= q_pow(i.first, i.second);
		}
		cout << ans << endl;
	}
}
发布了157 篇原创文章 · 获赞 99 · 访问量 9819

猜你喜欢

转载自blog.csdn.net/Q_1849805767/article/details/104194191