凌艺樽 • 4个月前
算法1:筛法思想,构成数,再判断是否在范围内;
#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e7+10;
const int INF=0x3f3f3f3f;
int t1,t2;
long long ans;
int main()
{
IOS;
cin>>t1>>t2;
for(int i=1;i<=t2;++i)
{
int j=1;//i,j两个因子
while(i*j<=t2)
{
if(i*j>=t1 && i*j<=t2)//i*j构成的数是否越界
{
ans++;
}
j++;
}
}
cout<<ans;
return 0;
}
算法2:先算出1~t2所有因数,再减去(t1-1)(注意)所有因数;
时间复杂度O(t2);
#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e7+10;
const int INF=0x3f3f3f3f;
int t1,t2;
long long ans;
int main()
{
IOS;
cin>>t1>>t2;
for(int i=1;i<=t2;++i)
{
ans+=t2/i; //1~t2所有因子
/*
1:1
2:1 2
3:1 3
4:1 2 4
5:1 5
6:1 2 3 6
(1六个,2三个,3两个,4,5,6各一个)
*/
}
for(int i=1;i<t1;++i)
{
ans-=(t1-1)/i;//推导同上
}
cout<<ans;
return 0;
}
Comments: