题目描述
Bablu is very fond of Series and Sequences…
After studying Fibonacci Series in Class IX, he was impressed and he designed his own sequence as follows…
a[0] = a[1] = 0
For n > 1, a[n] = a[n - 1] + f(n), where f(n) is smallest prime factor of n.
He is also very fond of programming and thus made a small program to find a[n], but since he is in Class IX, he is not very good at programming. So, he asks you for help. Your task is to find a[n] for the above sequence…
输入格式
Your code will be checked for multiple Test Cases.
First Line of Input contains T (<= 100), the number of Test Cases.
Next T lines contain a single number N. (1 < N < 10^7).
输出格式
Single line containing a[n] i.e. nth number of the sequence for each test case.
题意翻译
题目描述
Bablu 非常喜欢序列,他按照以下方式设计了自己的序列:a_0=a_1=0,对于 n(n>1),a_n=a_n-1+f_n ,其中 f_n 是 n 的最小素因子。 他也非常喜欢编程,因此制作了一个小程序来找到 a_n ,但由于他不擅长编程。所以,他请你帮忙找到 a_n。
输入格式
本题有多组数据。
第一行输入包含 T(T≤100),即测试用例的数量。
接下来的 T 行包含单个数字 n(1<n<10^7)。
输出格式
共 T 行,每行包含对应的 a_n 。
思路
这里我们可以用素筛求出每个数的最小素因数(第一个把它筛出来的就是),然后直接a[i]=a[i-1]+f[i]完事(2<=i<=n)
code:
吖#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
using namespace std;
int a[10000007];
long long b[10000007];
int n[101],t;
int main()
{
cin>>t;
for (int i=2;i<=10000007;i++)
{
if (a[i]) continue;
for (int j=i;j<=10000007;j+=i)
{
if (!a[j]) a[j]=i;
}
}
int mx=1;
for (int i=0;i<t;i++)
{
cin>>n[i];
mx=max(n[i],mx);
}
for (int j=2;j<=mx;j++) b[j]=b[j-1]+a[j];
for (int i=0;i<t;i++) cout<<b[n[i]]<<endl;
return 0;
}