CF1881D 质因数分解与重新分配

题目

You are given an array a consisting of n positive integers. You can perform the following operation on it:

  1. Choose a pair of elements ai and aj (1i,jn and ij);
  2. Choose one of the divisors of the integer ai, i.e., an integer x such that aimodx=0;
  3. Replace ai with aix and aj with ajx.

Determine whether it is possible to make all elements in the array the same by applying the operation a certain number of times (possibly zero).

For example, let's consider the array a = [100,2,50,10,1] with 5 elements. Perform two operations on it:

  1. Choose a3=50 and a2=2, x=5. Replace a3 with a3x=505=10, and a2 with a2x=25=10. The resulting array is a = [100,10,10,10,1];
  2. Choose a1=100 and a5=1, x=10. Replace a1 with a1x=10010=10, and a5 with a5x=110=10. The resulting array is a = [10,10,10,10,10].

After performing these operations, all elements in the array a become equal to 10.
https://codeforces.com/contest/1881/problem/D

Input

The first line of the input contains a single integer t (1t2000) — the number of test cases.

Then follows the description of each test case.

The first line of each test case contains a single integer n (1n104) — the number of elements in the array a.

The second line of each test case contains exactly n integers ai (1ai106) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 104.

7
5
100 2 50 10 1
3
1 1 1
4
8 2 4 2
4
30 50 27 20
2
75 40
2
4 4
3
2 3 1

Output

For each test case, output a single line:

  • "YES" if it is possible to make all elements in the array equal by applying the operation a certain (possibly zero) number of times;
  • "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).

YES
YES
NO
YES
NO
YES
NO

题解

解题思路

显然这是一道数论题,我们观察到,无论怎么进行操作,总数的积不变

那么操作的本质就是质因数的分解和重新分配,只需让相同质因数的个数是n的倍数,就可以平均分配。

Code

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
using namespace std;
typedef long long LL;

signed main(){
	IOS;
	int _;cin>>_;
	while(_--){
		int n;cin>>n;

		map<int,int> mp;
        //质因数分解
		auto f=[&](int x){
			for(int i=2;i<=sqrt(x);i++){
				while(x%i==0){
					mp[i]++;
					x/=i;
				}
			}
			if(x>1) mp[x]++;
		};

		for(int i=1;i<=n;i++){
			int t;cin>>t;
			f(t);
		}
		bool fl=0;
		for(auto i:mp){
			auto t=i.second;
			if(t%n!=0){
				cout<<"NO"<<endl;
				fl=1;break;
			}
		}
		if(!fl) cout<<"YES"<<endl;
	}
	return 0;
}

posted on   TaopiTTT  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示