[Codefroces] D - Power Products (number theory)

题意给k,和n个数 a i a_i ai,找 a i ∗ a j = x k a_i*a_j=x^k aiaj=xk的个数

方法: a i , a k a_i,a_k ai,ak质因数分解后,每个因子p的指数t一定是k的倍数。
可以把>=k的指数t%k,不影响统计
如果 a i a_i ai的因子为 P 1 t 1 ∗ P 2 t 2 ∗ . . . P_1^t1*P_2^t2*... P1t1P2t2...,那么 a j a_j aj的因子一定是 P 1 k − t 1 ∗ P 2 k − t 2 ∗ . . . P_1^{k-t1}*P_2^{k-t2}*... P1kt1P2kt2...
用map维护

#include <iostream>
#include <queue>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
 
using namespace std;
 
#define N 100000 
 
int a[N+10],v[N+10],p[N],tot;  
 
void init() {
	for (int i = 2; i <= N; i++) {
		if (!v[i]) p[++tot] = i;
		for (int j = 1; j <= tot; j++) {
			if (i*p[j]>N) break;
			v[i*p[j]] = 1;
			if (i%p[j]==0) break;
		}
	}
}
 
map<vector<pair<int,int> >,int> f;
 
int main() {
//	int T;
//	cin >> T;
	init();
//	while (T--) {
		int n,k,d;
		cin >> n >> k;
		long long ans = 0;
		for (int i = 1; i <= n; i++) {
			int t;
			scanf("%d",&t);
			vector<pair<int,int> > g,rg;
			for (int j = 1; j <= tot && t != 1; j++) 
			 if (t%p[j] == 0) {
			 	int cnt = 1;
			 	t /= p[j];
				while(t%p[j] == 0) {
					cnt++;
					t /= p[j];
				} 
				cnt %= k;
				if(cnt) {
				   g.push_back({p[j],cnt});
				   rg.push_back({p[j],k-cnt});
				}
			}
			ans += f[rg];
			f[g]++;
		}
		cout << ans << "\n";
//	}
    return 0;
}

You are given n positive integers a1,…,an, and an integer k≥2. Count the number of pairs i,j such that 1≤i<j≤n, and there exists an integer x such that ai⋅aj=xk.
Input
The first line contains two integers n and k (2≤n≤105, 2≤k≤100).
The second line contains n integers a1,…,an (1≤ai≤105).
Output
Print a single integer — the number of suitable pairs.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值