跳到正文

题解

题解:P10724 [GESP202406 七级] 区间乘积

Die_In_Silence约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 4 分钟
题目
P10724

版本

  1. v1当前

P10724 [GESP202406 七级] 区间乘积

题目传送门

十分可惜,赛时我并没有打出这道题,只拿了 1515pts ,可能是因为我太菜了。经过一个晚上的思考,我灵感乍现,想出了思路:

思路过程

这题要我们找一段区间,使区间内的数相乘之后为平方数,这道题暴力还是很好想的,这里跳过


我们已经知道一个平方数可以分解为许多质因数偶次方的乘积,所以我们就可以对每个a i​进行质因数分解,统计出现奇次方的质因数,然后开一个数组统计一下1~i所出现奇次方的质因数的乘积,我很快打出了代码,一交。---------RE,20分:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e7 + 10;
int n , m = 1 , ans;
int dp[N];
int pc(int x){//质因数分解
	int sum = 1 , j = 2;
	while(x != 1){
		while(x % (j * j) == 0)x = x / (j * j);
		if(x % j == 0) x /= j , sum *= j;
		j ++;
	}
	return sum;
}
signed main(){
	cin >> n;
	dp[1] = 1;
	for(int i = 1; i <= n; i ++){
		int a , j = 2;
		cin >> a;
		a = pc(a);
		m *= a;
		while(j * j <= m){
			while(m % (j * j) == 0) m = m / (j * j);
			j ++;
		}
		ans += dp[m];
		dp[m] ++;
	}
	cout << ans;
	return 0;
}

我寻思着不对呀,于是算了一下1~30的质因数的 2 * 3 * 5 * 7 * 9 * 11 * 13 * 17 * 23 * 29 = 6,469,693,230 ,噢,空间超限了,然后我就用了map。---------60分 , TLE。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n , m = 1 , ans;
map<int , int> dp;
int pc(int x){//质因数分解
	int sum = 1 , j = 2;
	while(x != 1){
		while(x % (j * j) == 0)x = x / (j * j);
		if(x % j == 0) x /= j , sum *= j;
		j ++;
	}
	return sum;
}
signed main(){
	cin >> n;
	dp[1] = 1;
	for(int i = 1; i <= n; i ++){
		int a , j = 2;
		cin >> a;
		a = pc(a);
		m *= a;
		while(j * j <= m){
			while(m % (j * j) == 0) m = m / (j * j);
			j ++;
		}
		ans += dp[m];
		dp[m] ++;
	}
	cout << ans;
	return 0;
}

原来是这样的质因数分解太慢了,于是我换成了异或,终于过了,实属不易。

AC代码,请勿抄袭

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n , m , ans;
map<int , int> dp;
int pc(int x){//质因数分解
	int sum = 0 , j = 2;
	while(x != 1){
		while(x % (j * j) == 0)
		    x = x / (j * j);
		if(x % j == 0)
		    sum ^= 1 << j , x = x / j;
		j ++;
	}
	return sum;
}
signed main(){
	cin >> n;
	dp[0] = 1;
	for(int i = 1; i <= n; i ++){
		int a;
		cin >> a;
		a = pc(a);
		m ^= a;
		ans += dp[m];
		dp[m] ++;
	}
	cout << ans;
	return 0;
}

希望屏幕前的你看完本人一步一步的思路后会有所收获

原文