Affinity number within 5 million

Source: Internet
Author: User

Time Complexity of O (N*log (N))
Time Complexity Accurate procedure:
n * (1 + 1/3 + + +) + 1/n) ~ ~ N*inn
So the time complexity of the second for loop is about O (N*log (N))

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <map> #include <set> #include <list> #include <cassert> #include <string># Include <climits> #include <algorithm>using namespace std; #define MAXN 5000000#define Left (i) (2*i+1) # Define Right (i) (2*i+2) #define _CP (A, B) ((a) < (b)) #define MIN (A, B) ((a) < (b)? ( A):(B)) #define MAX (A, B) ((a) > (b)? (   A):(B)) #define RST (n) memset (n, 0, sizeof (n)) int sum[maxn+10];    Prevent overflow int main () {freopen ("Data.out", "w", stdout);  for (int i=0; i<=maxn; i++) sum[i]=1; 1 is the true factor of all numbers, so all 1//preprocessing, Time complexity O (N*log (N)) for (int i=2; i+i<=maxn; i++) {//5 million The true factor less than half in   T J = i + i;  Because it is a true factor, it cannot be added itself while (J <= Maxn) {Sum[j] + = i;        All the multiples of I plus i j + = i; }} for (int i=220; i<=maxn; i++)//scan {//Go to weight, determine if cross-border, judge Affinity if (Sum[i]>i && SUM[I]&LT;=MAXN & Amp;& sum[sum[i]]==i) {printf ("%d%d\n", I, sum[i]); }} return 0;}



Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with

Affinity number within 5 million

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.