您的位置:首页 > 产品设计 > UI/UE

解题报告 之 HDU5288 OO' s Sequence

2015-08-13 14:26 357 查看


解题报告 之 HDU5288 OO' s Sequence

Description

OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) , that there's no j(l<=j<=r,j<>i) satisfy a imod a j=0,now OO want to know

























































Input

There are multiple test cases. Please process till EOF.

In each test case:

First line: an integer n(n<=10^5) indicating the size of array

Second line:contain n numbers a i(0<a i<=10000)



Output

For each tests: ouput a line contain a number ans.


Sample Input

5
1 2 3 4 5




Sample Output

23




题目大意:给你n个数的序列(其中数可能重复),对于每一个子区间,求该子区间中没有因子也在该子区间的的个数。再把所有子区间内这样的数的数量求和。比如样例中的 1
2 3 4 5,那么子区间[1,2,3]中这样的数就是1,2,3三个。然后对于子区间2 3 4,这样的数就只有两个,因为4有因子2也在该子区间中。

分析:很自然的想法是遍历每一个子区间,再统计有多少个数,再加起来。但这样做是不可行的,这样就中了题目的陷阱,被那个公式给误导了,所以我们必须要跳出惯性思维,将关注的单位从子区间变到每一个数。

考虑一个数,它能被统计多少次取决于什么呢?取决于它在多少个子区间内能够做到没有因子。所以我们很自然的去关注离他最近的左右两个因子,因为这两个因子范围以外的子区间都没有卵用。。。比如5
5 2 3 3 4 3 2 5 5,那么对于4来说,我们找到左右两个因子2之后,就可以发现从5开始和结束的子区间都不会算到4,因为有2在那里杵着。

至此,问题转化为,找到每个数左右离它最近的因子。然后就可以很容易的知道这个数能够被统计多少次了。那么怎么去寻找左右两边的因子呢?有两种做法,首先介绍我的方法。注意到可能的数字一共只有1e4个,先从左到右扫描依次更新两个数据,一是这个数最后出现的位置,用loc数组表示,另一个是这个数左边离它最近的因子的位置则用该数的每一个因子(遍历),求所有因子中最后出现的最大值。

然后再从右到左扫描,原理一样的。完成之后再遍历序列,对于每一个数求它被统计多少次即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

typedef long long ll;
const int MAXN = 1e5 + 10;
const int MAXM = 1e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int nums[MAXN]; //序列中的数
int lb[MAXN], rb[MAXN]; //序列中的数左右离他最近的因子的位置
int latest[MAXM];//某个数字最后出现的位置

int main()
{
	int n;
	while(scanf( "%d", &n ) == 1)
	{
		memset( lb, 0, sizeof lb );
		memset( rb, INF, sizeof rb );
		//reset

		for(int i = 1; i <= n; i++)
		{
			scanf( "%d", &nums[i] );
		}//input

		for(int i = 0; i < MAXM; i++)	latest[i] = 0;

		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= sqrt( nums[i] ); j++)
			{//遍历每个因子
				if(nums[i] % j == 0)
				{
					lb[i] = max( lb[i], latest[j] );
					lb[i] = max( lb[i], latest[nums[i] / j] );
				}
			}
			latest[nums[i]] = i; //更新位置,注意要遍历后更新,因为本身也是自己的因子

		}// tackle 1

		for(int i = 0; i < MAXM; i++)	latest[i] = n + 1;
		for(int i = n; i >= 1; i--)
		{
			for(int j = 1; j <= sqrt( nums[i] ); j++)
			{
				if(nums[i] % j == 0)
				{
					rb[i] = min( rb[i], latest[j] );
					rb[i] = min( rb[i], latest[nums[i] / j] );
				}
			}
			latest[nums[i]] = i;
		}// tackle 2 同理

		ll ans = 0;
		for(int i = 1; i <= n; i++)
		{
			ans = (ans + (i - lb[i])*(rb[i] - i)) % MOD; 
			//统计序列中每个数被统计的次数,可以理解为范围内左边选一个数的选法*右边选一个数的选法。
		}
		printf( "%lld\n", ans );
	}
	return 0;
}


还有一种方法是,记录每个数字出现的位置,每次更新的时候用二分去找距离它最近的因子的位置。但是很麻烦也更慢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: