列表

详情


NC14614. Array

描述

Given an array A with length n  a[1],a[2],...,a[n] where a[i] (1<=i<=n) is positive integer.
Count the number of pair (l,r) such that a[l],a[l+1],...,a[r] can be rearranged to form a geometric sequence.
Geometric sequence is an array where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. i.e. A = [1,2,4,8,16] is a geometric sequence.

输入描述

The first line is an integer n (1 <= n <= 100000).
The second line consists of n integer a[1],a[2],...,a[n] where a[i] <= 100000 for 1<=i<=n.

输出描述

An integer answer for the problem.

示例1

输入:

5
1 1 2 4 1

输出:

11

说明:

The 11 pairs of (l,r) are (1,1),(1,2),(2,2),(2,3),(2,4),(3,3),(3,4),(3,5),(4,4),(4,5),(5,5).

示例2

输入:

10
3 1 1 1 5 2 2 5 3 3

输出:

20

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 252ms, 内存消耗: 780K, 提交时间: 2022-06-23 17:09:44

#include<bits/stdc++.h>

using namespace std;
const int maxn = 1e5+5;
void IO(){
	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
}
int n,a[maxn];

bool check(vector<int> v){
	if(v[1]==v[0])return 0;
	long double q=1.0*v[1]/v[0];
	for(int i=1;i<v.size();i++){
		if(fabs((long double)1.0*v[i]/v[i-1]-q)>1e-8)return 0;
	}
	return 1;
}

int main(void){
	IO();
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	long long ans=0;
	for(int i=1;i<=n;i++){
		int cnt=1;
		while(i<n&&a[i]==a[i+1])i++,cnt++;
		ans+=1ll*cnt*(cnt+1)/2;
	}
	for(int i=1;i<=n;i++){
		for(int len=2;len<=17;len++){
			if(i+len-1>n)continue;
			vector<int> v(a+i,a+i+len);
			sort(v.begin(),v.end());
			if(check(v))ans++;
		}
	}
	cout<<ans<<endl;
	
	return 0;
}

上一题