Prime factor

Insert picture description here
Input description:
only one line with a positive integer n (2 <= n <=4*10^6)

Output description:
only one line, which means that the answer is the result of taking the remainder of 10^9+7

Input:
Example 1
3

Sample 2
10

Output:
Example 1
5

Sample 2
342

Idea: Core: Prime number sieve + dp
will decompose a number into the product of the smallest prime factor and then combine it into a large number. If it is violent, it will time out.
May wish to integrate the process of putting together, that is, 1+1=2, and the two parts are put together into one complete part.
It can be found that for the number n, when it is assembled into its large number f[n], the end must be the largest prime number of n. It is easy to get f[n]=f[n/maximum prime number] + the largest prime number of n, use dp Think about it, and suddenly become clear.
Here dp can be added to the prime number sieve, which is very comfortable (I use the Eschweiz sieve, of course, the linear sieve can also be used, and the code may be a little longer =......=

// 素筛dp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int p=1e9+7;
ll f[10000000];//每个数的处理后的大整数

ll cal(int x) //计算10的x次方
{
    
    
    int ans=10;
    while(x--)
    {
    
    
        ans*=10;
    }
    return ans;
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    int n;
    cin>>n;
    //埃氏筛
    for(int i=2; i<=n; i++)
    {
    
    
        if( !f[i] )//如果是素数
        {
    
    
            f[i]=i; //最大素数就是本身
            //dp,用对数求要对f[j/i]乘多少个10,因为末尾素数可能很大,前面的数字要前移的位数等于末尾素数的位数
            for(int j=i+i ;j<=n ;j+=i)   f[j]=( f[j/i] * cal(log10(i))%p + i)%p;
        }
    }
    ll ans=0;
    for(int i=2 ; i<=n ;i++) //最后求和
    {
    
    
        ans= ( ans+f[i] ) %p;
    }
    cout<<ans<<endl;
}

Guess you like

Origin blog.csdn.net/m0_53688600/article/details/113840627