TOJ3772 Rupxup's Math Problem

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值