题目链接
反函数y=n/x面积求和来求解。
t=
n
\sqrt{n}
n
这个函数按照(t,t)对称,所以结果为t前的所有∑n/i乘以2,再减去t*t。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
ll f(ll d)
{
ll ans=1;
ll t=sqrt(d);
for(ll i=1;i<=t;i++)
ans+=d/i;
ans=ans*2-t*t;
return ans;
}
int main()
{
ll n,m;
cin>>n>>m;
cout<<f(m)-f(n-1)<<endl;
}
特殊字符复制
求n的所有因子个数
可以解决埃式筛mle(在线代码评测中的内存过大而导致无法AC的现象)的问题。通过从小到大枚举素数对n进行唯一分解定理的分解过程,可以比较显著的降低时间消耗。
#include <iostream>
using namespace std;
typedef long long ll;
ll f(ll n)
{
ll ans=1;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
ll sum=0;
while(n%i==0)
{
sum++;
n/=i;
}
ans=ans*(sum+1);
}
}
if(n>1)
ans*=2;
return ans;
}
int main()
{
ll n;
cin>>n;
cout<<f(n)<<endl;
return 0;
}