Problem Solving report HDU5288 OO ' s Sequence

Source: Internet
Author: User
Tags ranges two factor

Problem Solving report HDU5288 OO ' s Sequence

Description

OO have got a array a of size n, defined a function f (l,r) represent the number of I (L&LT;=I&LT;=R), that there ' s no J (l& Lt;=j<=r,j<>i) satisfy a imod a j=0,now OO want to know ()

Input

There is 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

Sample Output


The main topic: give you a sequence of n number (where the number may be repeated), for each sub-interval, to find that the sub-interval has no factor in the number of the sub-range. Sum the number of such numbers in all the sub-ranges. For example, 1 2 3 4 5, so the number in the sub-band [three-way] is three. Then for the sub-interval 2 3 4, this number is only two, because 4 has a factor of 2 is also in the sub-range.
Analysis: The natural idea is to traverse each sub-range, and then count how many, and then add up. But this is not feasible, so that the problem of the trap, was misled by the formula, so we have to jump out of the inertial thinking, the unit of concern from the sub-range to each number.

Considering a number, how many times it can be counted depends on what? Depends on how many sub-ranges it can do without the factor. So it's natural for us to focus on the two factors that are closest to him, because the sub-ranges outside these two factors are not ova ... For example, 5 5 2 3 3 4 3 2 5 5, then for 4, we find the left and right two factor 2, we can find that starting from 5 and the end of the sub-range is not counted to 4, because there are 2 in there pestle.

At this point, the problem turns to finding the nearest factor to each number. Then it is easy to know how many times this number can be counted. So how to find the right and left side of the factor? There are two ways to start by introducing my approach. Note that there are only 1e4 possible numbers,first, the left-to-right scan updates two data, one is the last occurrence of this number, denoted by the LOC array, and the other is the position of the nearest factor to the left of the number, using each factor (traversal) of the number to find the last largest value in all the factors.
and then the right-to-left scan, the same principle. When you are donethen traverse the sequence, and for each number it is counted how many times.
#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]; Number of ints in sequence LB[MAXN], RB[MAXN];  The number of numbers in the sequence is around the position of his nearest factor int latest[maxm];//where the last occurrence of a number is int main () {int n;while (scanf ("%d", &n) = = 1) {memset (lb, 0, sizeof LB); memset (RB, INF, sizeof RB),//resetfor (int i = 1; I <= n; i++) {scanf ("%d", &nums[i]);} inputfor (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 + +) {//traverse each factor 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; Update the location, note to iterate after the update, because itself is also its own factor}//tackle 1for (int i = 0; i < MAXM; i++) Latest[i] = n + 1;for (int i = n; I >= 1; i--) {fo R (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 The same as ll ans = 0;for (int i = 1; I <= n; i++) {ans = (ans + (i-lb[i]) * (Rb[i]-i))% MOD;//statistical sequence in which each number is counted, can be understood Select a number on the left of the range to choose a number to the right of the selection method. }printf ("%lld\n", ans); return 0;}

another way is to record where each number appears, and use two points each time it is updated to find the location of the nearest factor. But it's more cumbersome and slower.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Problem Solving report HDU5288 OO ' s Sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.