题意
- q次询问,每次给一个x,问1到x的因数个数的和
思路
- 考虑每一个因子的贡献,对于因子p,有x/p对:
p(1,2,3,……,n/p)
。其中前p-1个是已经被枚举过的,p*p这对被计算了两次,所以每个因子的贡献是(x/p-p)*2+1
枚举加和即可
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int q;
cin >> q;
for(int i=1;i<=q;i++){
int x;
cin >> x ;
long long ans=0;
for(int j=1;j*j<=x;j++){
ans+=(x/j-j)*2+1;
}
cout << ans << endl;
}
return 0;
}