【Atcoder - AGC003D】Anticube

版权声明:本文为博主原创文章……懂吗?要尊重别人的劳动成果呐 https://blog.csdn.net/Tiw_Air_Op1721/article/details/84173931


@Problem Statement@

Snuke got positive integers s1,…,sN from his mother, as a birthday present. There may be duplicate elements.

He will circle some of these N integers. Since he dislikes cubic numbers, he wants to ensure that if both si and sj(i≠j) are circled, the product si*sj is not cubic. For example, when s1=1,s2=1,s3=2,s4=4, it is not possible to circle both s1 and s2 at the same time. It is not possible to circle both s3 and s4 at the same time, either.

Find the maximum number of integers that Snuke can circle.

Constraints
1≦N≦10^5
1≦si≦10^10
All input values are integers.

Input
The input is given from Standard Input in the following format:
N
s1
:
sN
Output
Print the maximum number of integers that Snuke can circle.

Sample Input 1
8
1
2
3
4
5
6
7
8
Sample Output 1
6
Snuke can circle 1,2,3,5,6,7.

Sample Input 2
6
2
4
8
16
32
64
Sample Output 2
3

Sample Input 3
10
1
10
100
1000000007
10000000000
1000000009
999999999
999
999
999
Sample Output 3
9

@Translation@

Snuke 收到了一份特殊的生日礼物:N 个正整数!
Snuke 认为两个数是“不相配”的,当且仅当这两个数的乘积为立方数。
现在 Snuke 想要从 N 个数中选出尽量多的数,使得这些数两两“相配”。

@Solution@

首先,可以想到的第一个操作肯定是把每一个数的立方因子去掉。

然后从唯一分解式考虑:令 x = p 1 a 1 p 2 a 2 . . . p k a k x = p_1^{a_1}*p_2^{a_2}*...*p_k^{a_k}
去掉立方因子得到: x = p 1 a 1 m o d    3 p 2 a 2 m o d    3 . . . p k a k m o d    3 x' = p_1^{a_1\mod3}*p_2^{a_2\mod3}*...*p_k^{a_k\mod3}
则如果 y y 使得 y = p 1 ( 3 a 1 ) m o d    3 p 2 ( 3 a 2 ) m o d    3 . . . p k ( 3 a k ) m o d    3 y' = p_1^{(3-a_1)\mod3}*p_2^{(3-a_2)\mod3}*...*p_k^{(3-a_k)\mod3} ,x 和 y 的乘积为立方数。

可以发现一个 x’ 对应着唯一的 y’。
假如有 p 个数去掉立方因子后得到 x’, q 个数去掉立方因子后得到 y’。
则我们要么选这 p 个数,要么选这 q 个数。因此对答案的贡献 max(p, q)。

我们对于每一个数 x 求出它对应的 x’ 与 y’,然后统计相应的个数,最后再取max。

但是还有特殊情况:如果 x’ = y’。即:如果这个数本身就是一个完全平方数。
我们不能选两个以上的完全平方数,但一定可以选一个完全平方数。
因此标记一下是否有完全平方数即可。

讲讲实现上的一些问题:
可以发现最多有两个质因子 > x 的立方根。
我们先筛出 ≤ x 的立方根的质数,然后用这些质数对 x 进行处理。
假如从 x 中除去这些因数后:
1)变为了 1,则 x 没有任何 > x 的立方根的质因子。
2)变为了一个完全平方数,则 x 有两个相同的 > x 的立方根的质因子。
3)变为了一个非完全平方数,则 x 要么有一个 > x 的立方根的质因子,要么有两个不相同的 > x 的立方根的质因子。
三种情况皆不影响 x’,但会影响 y’。根据唯一分解式推导一下就可以了。

@Code@

理论上来说……这道题应该会爆long long才对。
但是好像是因为我用的map,所以变成了负数也影响不大……
如果对于我的题解或者我的代码有任何的疑问,可以留言在下面,我会尽力解疑的qwq。

#include<map>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAX = 2160;
const int MAXN = 100000;
int prm[MAX + 5], pcnt = 0;
bool is_prm[MAX + 5];
void init() {
	for(int i=2;i<=MAX;i++) {
		if( !is_prm[i] )
			prm[++pcnt] = i;
		for(int j=1;i*prm[j]<=MAX;j++) {
			is_prm[i*prm[j]] = true;
			if( i % prm[j] == 0 ) break;
		}
	}
}
ll s[MAXN + 5], a[MAXN + 5], b[MAXN + 5];
ll IsSqr(ll x) {
	ll le = 1, ri = 100000;
	while( le < ri ) {
		ll mid = (le + ri) >> 1;
		if( mid * mid >= x ) ri = mid;
		else le = mid + 1;
	}
	return (ri * ri == x) ? ri : 0;
}
map<ll, int>mp;
int siz[2*MAXN + 5];
int main() {
	init(); int N;
	scanf("%d", &N);
	int ans = 0, flag = false;
	for(int i=1;i<=N;i++) {
		a[i] = b[i] = 1;
		scanf("%lld", &s[i]);
		for(int j=1;j<=pcnt;j++) {
			int cnt = 0;
			while( s[i] % prm[j] == 0 ) {
				cnt++;
				s[i] /= prm[j];
			}
			cnt %= 3;
			if( cnt == 1 ) {
				a[i] *= prm[j];
				b[i] *= prm[j] * prm[j];
			}
			else if( cnt == 2 ) {
				a[i] *= prm[j] * prm[j];
				b[i] *= prm[j];
			}
		}
		if( IsSqr(s[i]) ) {
			a[i] *= s[i];
			b[i] *= IsSqr(s[i]);
		}
		else {
			a[i] *= s[i];
			b[i] *= s[i] * s[i];
		}
		if( !flag && a[i] == 1 && b[i] == 1 )
			flag = true;
	}
	for(int i=1;i<=N;i++) {
		if( a[i] == 1 && b[i] == 1 ) continue;
		if( mp.count(a[i]) )
			siz[mp[a[i]]]++;
		else {
			mp[a[i]] = i;
			mp[b[i]] = i+N;
			siz[i]++;
		}
	}
	for(int i=1;i<=N;i++) {
		if( mp[a[i]] == i )
			ans += max(siz[i], siz[i+N]);
	}
	if( flag ) ans++;
	printf("%d\n", ans);
}

@End@

就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。

猜你喜欢

转载自blog.csdn.net/Tiw_Air_Op1721/article/details/84173931
今日推荐