又见n/i下取整+分块

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

因数个数和

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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

分析:枚举每个因子,i=1时,1~n都含有因子1,i=2时,n/2个含有因子2,所以这道题就变成了求\sum_{i}^{n}\left \lfloor \frac{n}{i} \right \rfloor

失了智,竟然暴力了一发,过了80%,然后TLE,n/i下取整,明显有一段区间值都一样,所以分块做了

比如:n=10;

/*#include<bits/stdc++.h>
using namespace std;
typedef long long ll;*/
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <bits/stdc++.h>
#include <algorithm>
#define N 100005
#define ll long long
using namespace std;
int main()
{
    int t,tmp,l,r;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        ll ans = 0;
        for (int i=n; i>=1;)
        {
            tmp=n/i;///值
            r=i;
            l=n/(tmp+1)+1;
            ans+=(r-l+1)*tmp;
            i=n/(tmp+1);
            //cout<<r<<" "<<l<<" "<<ans<<endl;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值