D. Power Products(map存vector+素因子分解)

题目

题意:

    给定n个数,输出有多少个点对满足a[i]*a[j]=任意数的k次方(i<j)。
     2 ≤ n ≤ 1 0 5 , 2 ≤ k ≤ 100 , 1 ≤ a i ≤ 1 0 5 2≤n≤10^5 , 2≤k≤100,1≤a_i≤10^5 2n105,2k100,1ai105

分析:

    将每个数分解为质因数,两个数相乘满足条件那么显然他们的质因数也满足条件。所以我们对一个数进行质因子分解,形成一个vector数组,数组里每个元素存对应的质因数及其指数,指数应对k取模,与其匹配的数组就是对应的指数为k-指数。用map存放这个vector,就可以log(n)找出。

#include <iostream>
#include <vector>
#include <map>
using namespace std;

typedef long long ll;

vector<pair<int,int> > temp;
map<vector<pair<int,int> >,int> ma;

void divide(int n,int k)
{
	int a = n;
	for (int i = 2; i * i <= n; i++)
	{
		if( a % i == 0 )
		{
			int num = 0;
			while( a % i == 0 )
			{
				a /= i;
				num ++;
			}
			num %= k;
			if( num != 0 ) temp.push_back(make_pair(i,num)); 
		}
	}
	if( a > 1 ) temp.push_back(make_pair(a,1)); 
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,k;
	cin >> n >> k;
	ll ans = 0;
	for (int i = 1; i <= n; i++)
	{
		int x;
		cin >> x;
		temp.clear();
		divide(x,k);
		vector<pair<int,int> > aim;
		for (int i = 0; i < temp.size(); i++)
		{
			aim.push_back(make_pair(temp[i].first,k-temp[i].second)); 
		}
		ans += ma[aim];
		ma[temp]++;
	} 
	cout << ans << '\n';
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值