版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37891604/article/details/82083880
链接: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,所以这道题就变成了求
失了智,竟然暴力了一发,过了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;
}