Algorithm friend number pair for large number affinity pair, large number affinity pair

Source: Internet
Author: User

Algorithm friend number pair for large number affinity pair, large number affinity pair

After a couple of days of affinity, I finally got an ac connection this evening. It's not easy. Let's talk about my practices. I hope I can help you.

The questions are as follows:

Description

Numbers 220 and 284 are a pair of youyuan numbers, because the sum of all factors of 220 is 284,284 and the sum of all factors of 220 is. Can you find out the number of friends in the number smaller than 10000? That is required. Your task is to find the number of pairs of friends in the given interval [a, B.

Input

There are multiple groups of test data. Each group of data contains two integers, a and B (1 <= a <= B <= 5000000 ). Input to the end of the file.

Output

For each group of inputs, the number of pairs of friends in the interval is output. Each group of output occupies one row.

Sample input 1 1001 1000 sample output 01 prompt

Although 6 is 6, 6 is not a friend number. A number like this is called a perfect number.

The question is to output the logarithm of the affinity number, and the range of the number is very large, so we can consider making a table first;

The code for creating a table is as follows:

Because there are many array elements, we first output the array elements to the hard disk, and then paste them into the array for calculation.

# Include <iostream>
Using namespace std;
Int main ()
{
Int I, k = 0;
For (I = 200; I <5000000; I ++) // loop 500 0000, the first number is 220, so from 200 Loops
{
Int sum1 = 1;
For (int j = 2; j <I/j; j ++)
{
If (I % j = 0)
Sum1 = sum1 + j + I/j; // calculate the true factor of a number and store it in sum1,
}
Int sum2 = 1;
For (int m = 2; m <sum1/m; m ++)
{
If (sum1% m = 0)
{
Sum2 = sum2 + m + sum1/m; // calculate the real factor of sum1 and store it in sum2,
}
}
FILE * fp;
If (I = sum2 & I <sum1) // determine whether the true factor of I is equal to the true factor of sum1, And I <sum1, in order to avoid non-repetition, such as 220,284, if I <sum1 is not handed in, a 284,220 error is returned;

{
Cout <k <"" <I <"" <sum1 <endl;
K ++;
Fp = fopen ("D: \ data.txt", "AB"); // output to the D disk. You can modify this step and save it to your own files;
Fprintf (fp, "% d, % d, \ r \ n", I, sum1 );
Fclose (fp );
}
}
}

Next, we need to calculate it;

# Include <iostream>
Using namespace std;
Int main ()
{// Both arrays are computed above. a [] indicates a small pair of affinity numbers, and B [] indicates a large number.
Int a [71] = {
171856,
176272,185368, 196724,280540, 308620,319550, 356408,437456, 469028,503056, 522405,600392, 609928,624184, 635624,643336,
667964,425104, 802725,879712, 898216,947835, 998104,1077890, 1154213,1156870, 1175265, 1185376, 1280565 1328470,
1358595, 1392368, 1466150,1468324, 1511930,1669910, 1798875,2010864, 2236570,2652728, 272379,2728726, 272134,2802416,
2803580,3276856, 3606850,3786904, 4155264,4238984, 4156130,1099750 };
Int B [71] = {
176336,
180848,203432, 202444,365084, 389924,2017402, 399592,455344, 486178,514736, 525915,669688, 686072,691256, 712216,652664,
783556,796696, 863835,901424, 980984,1125765, 1037996,1099390, 1189150,1292570, 1438983,1286744, 1340235,1483850,
1486845,1464592, 1747930,1738512, 1598470,2062570, 1870245,2090656, 2429030,2941672, 2874064,3077354, 2928136,2947216,
3716164,3721544, 3892670,4300136, 4006736,4314616, 4488910,4421350 };



Int x, y, sum, I;
While (cin> x> y)
{
Sum = 0;
For (I = 0; I <71; I ++)
{
If (x <= a [I] & B [I] <= y)
Sum ++;
}
Cout <sum <endl;
}
}

















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.