链接: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;
}