HDU-4961 boring sum

Source: Internet
Author: User

Problem descriptionnumber theory is interesting, while this problem is boring.

Here is the problem. Given an integer sequence A1, A2 ,..., An, let s (I) = {J | 1 <= j <I, and AJ is a multiple of AI }. if S (I) is not empty, Let f (I) be the maximum integer in S (I); otherwise, f (I) = I. now we define B I as AF (I ). similarly, let T (I) = {J | I <j <= n, and AJ is a multiple of AI }. if T (I) is not empty, let G (I) be the minimum integer in T (I); otherwise, g (I) = I. now we define CI as AG (I ). the boring sum of this sequence is defined as B1 * C1 + B2 * C2 +... + BN * CN.

Given an integer sequence, your task is to calculate its boring sum.
Inputthe input contains multiple test cases.

Each case consists of two lines. The first line contains an integer N (1 <=n <= 100000). The second line contains N integers A1, A2 ,..., An (1 <= AI <= 100000 ).

The input is terminated by n = 0.
Outputoutput the answer in a line.
Sample Input
 
51 4 2 3 90

Sample output
136 question: Give you an array and let you generate two new arrays. A requires that each number be located before it. If you can find the nearest one, which is a multiple of it, then it will be the number. Otherwise it will be your own, C will be looking for it later, and the idea of output crossover and multiplication will be: scanning record factor Processing
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> using namespace STD; typedef _ int64 ll; const int maxn = 100005; int A [maxn], B [maxn], C [maxn], vis [maxn]; int N; int main () {While (scanf ("% d", & n) = 1) {If (n = 0) break; For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) {If (vis [A [I]) B [I] = A [vis [A [I]; elseb [I] = A [I]; for (Int J = 1; j <= (INT) SQRT (double) A [I] + 0.5); j ++) {if (a [I] % J = 0) {vis [J] = I; vis [A [I]/J] = I ;}} memset (VIS, 0, sizeof (VIS); For (INT I = N; I> = 1; I --) {If (vis [A [I]) c [I] = A [vis [A [I]; elsec [I] = A [I]; for (Int J = 1; j <= (INT) SQRT (double) A [I] + 0.5); j ++) {if (a [I] % J = 0) {vis [J] = I; vis [A [I]/J] = I ;}} ll sum = 0; For (INT I = 1; I <= N; I ++) {sum + = (LL) B [I] * C [I];} printf ("% i64d \ n", sum);} return 0 ;}


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.