Description
Rupxup has learned a new function called function "F(n)", where F(n) equals to the number of factors of n (including 1 and n). For example, F(1) = 1, F(4) = 3, ... Now if you ask Rupxup to calculate F(n), he may soon tell you the answer. If you are evil enough and ask him about the sum of the first k F(i): (F(1) + F(2) + .. + F(k) = G(k)), I believe out superbrother Rupxup will soon tell you the answer too!
Input
In the first line there is an integer T, indicates the number of test cases. (T ≤ 100) In each case, the first line contains only one integer k. (1 ≤ k ≤ 109).
Output
Just output G(k).
Sample Input
3 1 2 3
Sample Output
1 3 5
一开始想模拟打表,后来发现数字太大。
后来注意到可以sum +=n/i //i从1到n n/i表示小于等于n的所有数中i的倍数的数目
#include <cstdio>
#include <iostream>
using namespace std;
long long f(long long n){
long long sum = 0;
for (long long k = 1;k <= n;k++) sum += n/k;
return sum;
}
int main(){
int n;
scanf("%d",&n);
while (n--){
long long m;
scanf("%lld",&m);
printf("%lld\n",f(m));
}
return 0;
}
光荣TLE......
在网上看到题解数形结合压缩到O(sqrt(n))
先思考x*y = n这条双曲线,,显然在第一象限分支中下方的整点的横纵坐标相乘必定小于n,那这一对数字必定代表一个小于n的数的一对约数。
作直线x=y,于是可以先计算含x=y这条直线的双曲线下方整点的个数。
x=1的时候有n个,x=2的时候有n/2-1个。。。
于是乘以2。
然后x=y这条直线上的i-1个点多计算了一次,于是要减去(i-1)个。
ORZ..
#include <cstdio>
#include <iostream>
using namespace std;
long long f(long long n){
long long sum = 0;
long long k;
for ( k = 1;k*k <= n;k++) sum += n/k - k + 1;
return sum*2 - k + 1;
}
int main(){
int n;
scanf("%d",&n);
while (n--){
long long m;
scanf("%lld",&m);
printf("%lld\n",f(m));
}
return 0;
}