Lord's first CSDN blog!

Lord's first CSDN blog!

ZCMU-1409
1409: Factor and
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 329 Solved: 90
[Submit][Status][Web Board]
Description
It is easy to find the factor sum of a number, so Xiao Ming is lazy and wants to let it You can help calculate which number in the interval a to b has the largest sum of factors, and output this number and each of its factors.

Input
input a and b, (0<a<=b<10^6).

Output
outputs this number and each of its factors (the smallest output in the same situation), and the factors are output from small to large.

Sample Input
1 10
Sample Output
10 = 1 + 2 + 5

Preface

This is the first blog of Caigou's entrance to school. Today, let's simply record the simple application of prime number sieve!
(As an ACMer, you have to develop the habit of blogging!)

1. What is a prime number sieve?

1. The basic theorem of arithmetic: any natural number N greater than 1, if N is not a prime number, then N can be uniquely decomposed into the product of a finite number of prime numbers.
2. If a number can be factorized, the two numbers obtained must be There is one >=sqrt(x) and the other <=sqrt(x).

According to my understanding, it is to traverse the numbers from 2 to maxn, and then record the factors of the following numbers, like a sieve, so as to reduce the time complexity and greatly improve the efficiency.

2. How to use it?

This is the function executed by the prime number sieve. You can understand it after reading it. It is written here to prevent forgetting. It is not as good as a bad pen to remember!

int prime[maxn];
void isprime()
{
    int i,j;
    memset(prime,1,sizeof(prime));
    for(i=2;i<=maxn/2;i++)
        for(j=i+i;j<maxn;j+=i)
            prime[j]=0;
}

to sum up

The following is the AC code, in fact, if you know the prime number sieve, it is a water problem

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int arr[maxn],sum[maxn];
int t = 0;
void getsum()//素数筛法,预处理每一个数的因子和,后面只要查找就行了,不用重复计算
{
    int i,j;
    memset(sum,1,sizeof(sum));
    for(i = 2;i <= maxn / 2;i++)
    {
        for(j = i + i;j < maxn;j += i)
            sum[j] += i;
    }
}
int main(){
    
        getsum();
        int a,b;
        while(~scanf("%d%d",&a,&b))
        {
            int maxsum = 0,dis = 0;
            for(int i = a;i <= b;i++)
            {
                if(sum[i] > maxsum)
                {
                    maxsum = sum[i];
                    dis = i;//记录因子和最大的那个数
                }
                
            }
            printf("%d = 1",dis);
            for(int i=2;i<=dis/2;i++)
                if(dis % i==0)printf(" + %d",i);
            cout << endl;
        }
    return 0;
    }

So be it.
I feel that blogging is quite interesting, and I can sort out my knowledge and strive for one a day!

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/114005266