BNU 12846 LCM extreme least common multiple sum (linear Euler screening + recursion)

Source: Internet
Author: User
LCM extreme Time limit:3000 Ms Memory limit:131072kb this problem will be judged on uvalive. Original ID: 5964
64-bit integer Io format: % LLDJava class name: Main


Find the result of the following code:
Unsigned long allpairlcm (int n ){
Unsigned long res = 0;
For (INT I = 1; I <= N; I ++)
For (Int J = I + 1; j <= N; j ++)
Res + = lcm (I, j); // LCM means least common multiple
Return res;
}
A straight forward implementation of the Code may time out.
Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.
Each case starts with a line containing an integer N (1 ≤ n ≤ 5*106
).
Output
For each case, print the case number and the value returned by the function 'allpairlcm (n) '. As
Result can be large, we want the result modulo 2
64
.
Sample input output for sample input
4
2
10
13
100000
Case 1: 2
Case 2: 1036
Case 3: 3111
Case 4: 9134672774499923824

1/* 2 Question: lcm (1, 2) + lcm (1, 3) + lcm (2, 3) + .... + lcm (1, N) + .... + lcm (n-2, n) + lcm (n-1, n) 3 set sum (n) To sum (LCM (I, j )) (1 <= I <j <= N) and F (n) are sum (I * n/gcd (I, n )) (1 <= I <n) 4 then sum (n) = sum (n-1) + f (n ). Linear Euler filtering + recursion can be used. 5 */6 # include <iostream> 7 # include <cstdio> 8 # include <cstring> 9 10 typedef unsigned long ll; 11 const int maxn = 5000005; 12 ll Phi [maxn], sum [maxn], F [maxn]; 13 14 void Euler () 15 {16 memset (PHI, 0, sizeof (PHI )); 17 int I, j; Phi [1] = 1; 18 for (I = 2; I <maxn; I ++) 19 {20 if (PHI [I]) continue; 21 For (j = I; j <maxn; j + = I) 22 {23 if (! Phi [J]) Phi [J] = J; 24 Phi [J] = Phi [J]/I * (I-1); 25} 26} 27 for (I = 1; I <maxn; I ++) Phi [I] = Phi [I] * I/2; // The sum of the numbers of mutual classes with I 28} 29 30 void Init () 31 {32 Euler (); 33 memset (sum, 0, sizeof (SUM); 34 memset (F, 0, sizeof (f); 35 int I, J; sum [1] = f [1] = 0; 36 for (I = 2; I <maxn; I ++) 37 {38 f [I] + = Phi [I] * I; // The sum of the LCM between the numbers of I and I is 39 for (j = 2 * I; j <maxn; J + = I) 40 f [J] + = Phi [I] * j; // gcd (x, j) = sum (LCM (x, j) of I )) 41 sum [I] = sum [I-1] + F [I]; 42} 43} 44 45 int main () 46 {47 // freopen ("in.txt ", "r", stdin); 48 // freopen ("out.txt", "W", stdout); 49 Init (); 50 int T, icase = 0, N; 51 scanf ("% d", & T); 52 while (t --) 53 {54 scanf ("% d", & N); 55 printf ("case % d: % LlU \ n ", ++ icase, sum [N]); 56} 57 Return 0; 58}

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.