HDU 4961 boring sum (Thinking hash scan)

Source: Internet
Author: User

Question Link

Question: Here is an array for you to generate two new arrays. A requires that each number be located before it, and the nearest one is its multiple, then it becomes the number. Otherwise, it is your own. C is to find it later, and the sum of output cross-multiplication is

Analysis:

This is the complexity of O (N * SQRT (N). The limit data definitely times out, but the data in this question is a bit watery, so you can pass it.

Use the VIS [I] array to represent the position of the number nearest to the number I in the [], because all numbers are in the range of 1-, so it is feasible to scan it again, find the divisor of the current number, overwrite the divisor, and overwrite the/divisor.

The same is true if you look for a multiple number closest to this number and then multiply it.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # define LL _ int64 8 const int maxn = 100000 + 10; 9 Using namespace STD; 10 ll a [maxn], pre [maxn], AF [maxn], vis [maxn], sum; 11 12 INT main () 13 {14 int N, I, j, TMP; 15 while (~ Scanf ("% d", & N) 16 {17 memset (AF, 0, sizeof (AF); 18 memset (PRE, 0, sizeof (pre); 19 memset (VIS, 0, sizeof (VIS); 20 for (I = 1; I <= N; I ++) 21 scanf ("% i64d", & A [I]); 22 for (I = 1; I <= N; I ++) 23 {24 if (vis [A [I]) 25 pre [I] = A [vis [A [I]; 26 else27 pre [I] = A [I]; 28 TMP = (INT) SQRT (A [I]); 29 for (j = 1; j <= TMP; j ++) 30 if (a [I] % J = 0) 31 {32 vis [J] = I; 33 vis [A [I]/J] = I; // do not forget that SQRT is followed by the divisor 34} 35} 36 37 memset (VIS, 0, sizeof (VIS); 38 for (I = N; i> = 1; I --) 39 {40 if (vis [A [I]) 41 AF [I] = A [vis [A [I]; 42 else43 AF [I] = A [I]; 44 TMP = (INT) SQRT (A [I]); 45 for (j = 1; j <= TMP; j ++) 46 if (a [I] % J = 0) 47 {48 vis [J] = I; 49 vis [A [I]/J] = I; 50} 51} 52 sum = 0; 53 for (I = 1; I <= N; I ++) 54 sum + = pre [I] * AF [I]; 55 printf ("% i64d \ n", sum); 56} 57 Return 0; 58}

 

HDU 4961 boring sum (Thinking hash scan)

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.