https://ac.nowcoder.com/acm/problem/53079
题意:
给定一数n,求1~n每个数的最小质因数的和。
分析:
可以用欧拉线性筛素数法,每个数仅使用其最小素因数筛去,开始先打个表,后面直接统计答案就行了。
代码:
#include<stdio.h> #include<algorithm> #include<vector> using namespace std; typedef unsigned long long ll; int n,m,v,w,s,i,j; const int LIM = 3e7 + 10; int prime[LIM / 3]; int miniFactor[LIM]; int primepos; void euler() { int tmp; for (int i = 2; i < LIM; i++) { if (!miniFactor[i]) prime[primepos++] = i, miniFactor[i] = i; for (int j = 0; (tmp = i * prime[j]) < LIM; j++) { miniFactor[tmp] = prime[j]; if (!(i % prime[j])) break; } } } int main() { ll sum=0; euler(); scanf("%d",&n); for(i=1;i<=n;i++){ sum+=miniFactor[i]; } printf("%lld",sum); }