Nyoj 411 Friends Number (number theory-factor and)

Source: Internet
Author: User

Links: Click to open link

Test instructions

Friends number time limit: ms | Memory limit: 65535 KB Difficulty: 2
Describe
Paula and Tai are couple. There is many stories between them. The day Paula-airplane, Tai send one message to telephone 2200284, then, everything is changing ... (The story in "The Snow Queen"). After a long time, Tai tells Paula, the number and 284 was a couple of friends number, as they is special, all divisor S of 284, and all divisors of 284 ' s sum is 220. Can you find out there is how many couples of friends number less than 10,000. Then, what about 100,000, 200,000 and so on. The task for-is-find out there-many couples of friends number in given closed interval [a, b].
Input
there is several cases.
Each test case contains positive integers a, B (1<= a <= b <=5,000,000).
Proceed to the end of file.
Output
for each test case, output the number of couples in the given range. The output of one test case occupied exactly one line.
Sample input
1 1001 1000
Sample output
01
Tips
6 is a number whose sum of all divisors are 6.6 is not a friend number, these number is called Perfect number.
Source
Liaoning Province 10-year provincial race
Idea: Actually understand test instructions is relatively simple, to find the interval to meet "friend Num" number of pairs, friend Num: Suppose, a, B, if all the factors of a and ==b, and all of the factors and ==a, and meet!

OJ data a bit large, conventional method, kept time-out, looked at the first few people in the code, the basic is to play the table, (embarrassing ~ ~), later a hint, with the array simulation, in fact, can also use the container

, it's more comfortable to write.

Code: (red area emphasis)

 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>using namespace STD; #define N 5000001#define CLR (arr, what) memset (arr, what, sizeof (arr)) int a[n], pre[n],last[n],ss=0;void getsum ();        int main () {//getsum ();/for (int i = 2, I <= N; i++) A[i] = 1;//for (int i = 2; I * I <= N; i++)//   for (int j = i; J * I <= N; j + +)//A[i * j] + = i + j; <span style= "color: #ff0000;"    > for (int i = 2; I <= N; i++) a[i] = 1;    for (int i = 2, I * I <= N; i++) for (int j = i + 1; I * j <= N; j + +) A[i * j] + = i + j;    for (int i = 2; I * I <= N; i++) a[i * I] + = i;        for (int i=1; i<=n; i++) {int t=a[i];            if (T > I && t <=N&& a[t] = = i) {pre[ss]=i;            last[ss]=t;        ss++;    }}</span> int m,i,j,n;        while (~SCANF ("%d%d", &m,&n)) {int count = 0; for (i=0; i<ss; i++) if (pre[i]>=m&&last[i]<=n) count++;    printf ("%d\n", count);         } return 0;}

When you want to give up, think of what you persist until now!


Nyoj 411 Friends Number (number theory-factor and)

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.