hdu5288 OO ' s Sequence (mass factor decomposition + dichotomy)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5288

Test instructions: interval [l,r], if l<=i<=r, for all I, satisfies a[i]%a[j]!=0 (i!=j) I contribute +1 to the answer. Find out the answers to all the intervals and, among them, 1<=l<=r<=n.

Analysis: Find out the contribution of each number to the answer. For each a[i], the position of the left A[i] nearest and can be rounded out a[i] l[i] and the right away from a[i] nearest and can be complete a[i] r[i], then a[i] The contribution to the answer is (r[i]-i) * (I-l[i]). How to find L[i], first the position of each number in the input order exists in the array, the enumeration of a[i] factor, each enumeration a[i] a factor, two points to the left and right of A[i] The nearest position, update position.

Code:

#include <iostream> #include <cstdio> #include <vector> #include <algorithm>using namespace std ; const int MAXN = 1e5+6;const int mod = 1e9+7;vector <int > Fac[10006];vector <int > Pos[10006];int a[maxn],l[ Maxn],r[maxn];void Init () {int i,j;for (i=1;i<10005;i++) {for (j=1;j*j<=i;j++) {if (i%j==0) {fac[i].push_back (j); if (i/j!=j) fac[i].push_back (i/j);}}} int find_l (int f,int p) {int down=0,mid,up=pos[f].size () -1,ret=-1;while (down<=up) {mid= (Up+down) >>1;if (pos[ f][mid]>=p) up=mid-1;else{down=mid+1;if (Ret<pos[f][mid]) ret=pos[f][mid];}} return ret;} int find_r (int f,int p) {int down=0,mid,up=pos[f].size () -1,ret=1e9;while (down<=up) {mid= (Up+down) >>1;if (pos [F] [mid]<=p) Down=mid+1;else{up=mid-1;if (Ret>pos[f][mid]) ret=pos[f][mid];}} return ret;} int main () {Init (); int N,i,j;while (scanf ("%d", &n)!=eof) {for (i=0;i<=10000;i++) pos[i].clear (); for (i=0;i<n ; i++) {scanf ("%d", &a[i]);p os[a[i]].push_back (i); L[i]=-1; R[i]=n;} for (i=0;i<n;i++) {for (J=0;j<fac[a[i]].size (); j + +) {int f=fac[a[i]][j];int l=find_l (f,i); int R=find_r (f,i); L[i]=max (L,l[i]); R[i]=min (R,r[i]);}} Long Long ans=0;for (i=0;i<n;i++) ans= (ans+ (r[i]-i) * (I-l[i])%mod)%mod;printf ("%i64d\n", ans);} return 0;}


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

hdu5288 OO ' s Sequence (mass factor decomposition + dichotomy)

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.