1. 程式人生 > >解題報告 之 HDU5288 OO' s Sequence

解題報告 之 HDU5288 OO' s Sequence

bold repr frame roman efi int tom relative 記錄

解題報告 之 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在那裏杵著。



至此,問題轉化為,找到每個數左右離它近期的因子。然後就行非常easy的知道這個數可以被統計多少次了。那麽怎麽去尋找左右兩邊的因子呢?有兩種做法,首先介紹我的方法。註意到可能的數字一共僅僅有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;
}

另一種方法是,記錄每一個數字出現的位置,每次更新的時候用二分去找距離它近期的因子的位置。可是非常麻煩也更慢。


解題報告 之 HDU5288 OO&#39; s Sequence