牛牛的质因数

链接:https://ac.nowcoder.com/acm/contest/9982/I

如果有可能 我想离它远点

大意:从2-n 分解质因数

例如 8 分解得到222
从 2-8
2
3
22
5
23
7
222
然后加起来得到342
解释不清楚可以看题目

思路:素数筛
这里利用埃氏筛和欧拉筛分别整理了一下

欧拉筛 这个到头来都卡我t 我想不明白了 害

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062
typedef long long ll;
typedef pair<int,int> PII;
int qow_m(int n,int w){int k=1;while(w){if(w&1)k=k*n%1000;	n=n*n%1000;	w>>=1;}return k%1000;}
ll read(){ll res = 0, ch, flag = 0;if((ch = getchar()) == '-')flag = 1;else if(ch >= '0' && ch <= '9')res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}
const int maxn =4e6+7 ;
ll sum=0;
ll n,m,k,wz,cnt=0,h;//ans=0,
ll mod=1e9+7;
ll x,y=0;
ll nm[maxn];
ll pre[maxn];
ll ans[maxn];
string ok[maxn];
int c;
void cal(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(nm[i]==0)
            pre[c++]=i;

        for(int j=0;i*pre[j]<=n&&j<c;j++)
        {
            nm[pre[j]*i]=1;
            if(i%pre[j]==0)
                break;
        }
    }
}
string fenjie(int n)
{
    string s="";
    int k=0;
    for(int i=0;i<c;i++)
    {
       if(n%pre[i]==0)
       {
           n/=pre[i];
           s+=to_string(pre[i]);
       }

       if(n==1)
        break;

       if(nm[n]==0)
       {
           s+=to_string(n);
           break;
       }
       if(!ok[n].empty())
        {
            s+=ok[n];
            break;
        }

    }
    return s;
}

int main()
{
    ll p[100];
    p[0]=1;
    for(int i=1;i<=100;i++)
        p[i]=(p[i-1]*10)%mod;
    n=read();
    cal(n);

    for(int i=2;i<=n;i++)
    {

        if(!nm[i])
           {
               ans[i]=i;
           }
        else
            {
            ok[i]=fenjie(i);
           int len=ok[i].size();
           for(int j=0;j<len;j++) ans[i]=(ans[i]+(ok[i][j]-'0')*p[len-j-1])%mod;
            }

    }
    ll op=0;
   for(int i=2;i<=n;i++)    op=(op+ans[i])%mod;


  printf("%lld",op);
    return  0;
}


埃氏筛

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062
typedef long long ll;
typedef pair<int,int> PII;
int qow_m(int n,int w){int k=1;while(w){if(w&1)k=k*n%1000;	n=n*n%1000;	w>>=1;}return k%1000;}
ll read(){ll res = 0, ch, flag = 0;if((ch = getchar()) == '-')flag = 1;else if(ch >= '0' && ch <= '9')res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}
const int maxn =4e6+7 ;
ll sum=0;
ll n,m,k,wz,cnt=0,h,ans=0;
ll mod=1e9+7;
ll x,y=0;
ll nm[maxn];
ll pre[maxn];
ll f[maxn];
string ok[maxn];
int c;
int calc(int x)
{
    int res = 10;
    for (int i = 1;i <= x;i ++)  res *= 10;
    return res;
}

int main()
{
    cin >> n;

    for (int i = 2;i <= n;i ++)
    {
        if (!f[i])
        {
            f[i] = i;
            for (int j = 2 * i;j <= n;j += i)
                f[j] = (f[j / i] * calc(log10(i)) % mod + i) % mod;
        }
    }
    ll ans = 0;
    for (int i = 2;i <= n;i ++)  ans = (ans + f[i]) % mod;
    cout << ans << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值