Hdoj 5288 OO ' s Sequence water

Source: Internet
Author: User


Preprocessing the position of the left and right sides of each number to divide its nearest number.


OO ' s SequenceTime limit:4000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1880 Accepted Submission (s): 672


Problem Descriptionoo has got a array a of size n, defined a function f (l,r) represent the number of I (L&LT;=I&LT;=R), t Hat there ' s no J (l<=j<=r,j<>i) satisfy AI mod aj=0,now OO want to know ∑ i = 1 n ∑j=in F (I,J) mod  ( ten 9 +7).

Inputthere 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 ai (0<ai<=10000)

Outputfor each tests:ouput a line contain a number ans.
Sample Input
51 2 3) 4 5

Sample Output
23

Authorfzuacm
Source2015 multi-university Training Contest 1
/* ***********************************************author:ckbosscreated time:2015 July 24 Friday 08:12 15 seconds file Name : hdoj5288.cpp************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;typedef Long long int ll;const int Maxn=100100;const ll mod= (LL) (1e9+7); int N;int A[maxn];int left[maxn],right[maxn];vector< int> pos[10010];void Init () {for (int i=0;i<=10010;i++) {pos[i].clear ();} for (int i=0;i<n;i++) {left[i]=0; right[i]=n-1;}} void Pre () {for (int i=0;i<n;i++) {int x=a[i];for (int. j=x;j<=10000;j+=x) {for (int k=0,sz=pos[j].size (); k<sz;k+ +) {int z=pos[j][k];if (z==i) Continue;else if (z<i) {right[z]=min (right[z],i-1);} else if (z>i) {Left[z]=max (left[z],i+1);}}}} int main () {//freopen ("In.txt", "R", stdin);//freopen ("OUT.txt", "w", stdout), while (scanf ("%d", &n)!=eof) {init (); for (int i=0;i<n;i++) {scanf ("%d", a+i); Pos[a[i]].push_back (i);} Pre (); ll ans=0;for (int i=0;i<n;i++) {ll l=i-left[i]+1ll; LL r=right[i]-i+1ll;ans= (ans+ (l*r)%mod)%mod;}        Cout<<ans<<endl;} return 0;}




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

Hdoj 5288 OO ' s Sequence water

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.