牛客练习赛25 A 签到题 因数个数和

链接:https://www.nowcoder.com/acm/contest/158/A
来源:牛客网
 

题目描述

q次询问,每次给一个x,问1到x的因数个数的和。

输入描述:

第一行一个正整数q ;
接下来q行,每行一个正整数 x

输出描述:

共q行,每行一个正整数表示答案

示例1

输入

复制

4
1
2
3
10

输出

复制

1
3
5
27

说明

1的因数有1

2的因数有1,2

3的因数有1,3

以此类推

备注:

1<=q<=10 ,1<= x<=109

一开始想打表仔细一看是1加到x,显然不行

启用oeis公式

代码如下

//---JQM---//
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
 
ll yin(ll n)
{
    ll l, temp;
    ll ans = 0;
    for( l = 1; l <= n; l = temp+1)
    {
        temp = n/(n/l);
        ans += n/l * (temp - l + 1);
    }
    return ans;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll x;
        cin>> x;
        cout<<yin(x)<<endl;
    }
}

赛后出了官方题解,大概是这个意思:

把因数个数和转化为倍数个数和,即\sum ^n_{i=1} \left \lfloor \frac{n}{i} \right \rfloor

猜你喜欢

转载自blog.csdn.net/Jqmjyj/article/details/82026073