Title Link: HDU 5288 OO ' s Sequence
Test instructions: Gives an n, a sequence of n numbers, a function f (l,r) definition, how many numbers exist in the L,r interval, and cannot be divisible by other numbers. Calculate the function value of all the sub-intervals
Idea: Start with a small range of AI
1.a cannot be divisible by B, that is, all the factors of a do not exist in B, so the table enumerates all the factors of the number.
2. Finding a number (position I) satisfies the condition when the leftmost L and the right-most R, (i-l) * (r-i) is the contribution to the answer.
AC Code:
#include <stdio.h> #include <algorithm> #include <vector> #include <string.h> #define LL __ Int64using namespace Std;int a[100010];vector<int> mp[10010];int l[100010],r[100010];//l: The nearest divisible a from the left of the number of I to a[i] Position of the number of [i] int vis[10010];const LL kmod=1e9+7;void init () {int i,j;for (i=1;i<=10000;i++) {mp[i].clear (); for (j=1;j< =i;j++) {if (i%j==0) Mp[i].push_back (j);}}} int main () {int i,j,n;init (), while (scanf ("%d", &n)!=eof) {for (i=1;i<=n;i++) {scanf ("%d", &a[i]);} for (i=1;i<=n;i++) L[i]=1;memset (vis,0,sizeof Vis), for (i=1;i<=n;i++) {int sz=mp[a[i]].size (), for (j=0;j<sz; J + +) {int tmp=mp[a[i]][j];if (vis[tmp]==0 | | a[i]%tmp!=0)//Factor Continue;l[i]=max (l[i],vis[tmp]+1) not occurring or not a[i]; Take a rightmost point}vis[a[i]]=i;//record a[i] subscript,}for (i=1;i<=n;i++) R[i]=n;memset (vis,0,sizeof Vis); for (i=n;i>=1;i--) {int Sz=mp[a[i]].size (); for (j=0;j<sz;j++) {int tmp=mp[a[i]][j];if (vis[tmp]==0 | | a[i]%tmp!=0)//First determine TMP (actually a[i] Whether there is equal to TMP), then whether the Continue;r[i]=min (r[i],vis[tmp]-1) is divisible.} Vis[a[i]]=i;} ll Ans=0;for (i=1;i<=n;i++) {ll tmp= (LL) (i-l[i]+1) * (r[i]-i+1);p rintf ("%d%d =%d\n", l[i],r[i],tmp); ans= (ans+tmp)% Kmod;} printf ("%i64d\n", ans);} return 0;} /*51 2 6 4 5*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5288 OO ' s Sequence (brute Force enumeration factor)