nowcoder 牛牛的最大兴趣组 质因子 + 思维

传送门

文章目录

题意:

在这里插入图片描述
在这里插入图片描述

思路:

首先 n n n很小的话可以暴力连边,让后染个色求一个颜色最多的即可。但是这个题显然不行,由于是三次方,所以考虑质因子入手。
首先很容易就能想到将所有的数的质因子的幂次模上 3 3 3,之后他对应的乘起来为三次方数的数是唯一的。因为对于同一个质数来说, a a a的幂次为 0 , 1 , 2 0,1,2 0,1,2对应的 b b b的幂次为 0 , 2 , 1 0,2,1 0,2,1,所以现在问题就变成了如何快速将所有数的幂次模 3 3 3,并且求出来他对应的数,然后答案就是二者取一个 m a x max max即可,因为二者一定是一个取一个不取,即染上不同的色。下面考虑如何将所有数幂次模 3 3 3
我们可以筛出来 2000 2000 2000以内的质数的三次幂,让后每次遍历跑一遍即可。这样筛出来的数 x x x的质因子幂次都 < 3 <3 <3,但是我们要求他对应的数怎么办呢?显然不能直接分解质因子,这样的复杂度还是 ( 2 e 9 ) \sqrt{(2e9)} (2e9) 的,这里有一个巧妙的做法,就是对 x ∗ x x*x xx再进行一次上面的分解,得出来的数即为 x x x对应的数,因为 x ∗ x x*x xx可以将原来 1 1 1变成 2 2 2 2 2 2变成 4   m o d   3 = 1 4\bmod 3=1 4mod3=1,正符合上面的规律。
复杂度约为 O ( n ∗ 200 ) O(n*200) O(n200)

// Problem: 牛牛的最大兴趣组
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/7604/C
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N];
LL prime[N],cnt;
map<LL,int>mp,has;
bool st[N];

void get_prime(int n) {
	for(int i=2;i<=n;i++) {
		if(!st[i]) {
			prime[++cnt]=1ll*i*i*i;
			for(int j=i+i;j<=n;j+=i)
				st[j]=true;
		}
	}
}

LL divide(LL x) {
	for(int i=1;i<=cnt;i++) {
		if(x%prime[i]==0) {
			while(x%prime[i]==0) x/=prime[i];
		}
		if(prime[i]>x) return x;
	}
	return x;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	get_prime(1300);
	cout<<cnt<<endl;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		a[i]=divide(a[i]);
		mp[a[i]]++;
		has[a[i]]=divide(1ll*a[i]*a[i]);
	}
	int ans=0;
	for(auto &x:mp) {
		if(x.X==1) {
			ans++;
			continue;
		}
		LL fa=has[x.X];
		ans+=max(mp[fa],x.Y);
		x.Y=0; mp[fa]=0;
	}
	cout<<ans<<endl;
	


	return 0;
}
/*

*/









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值