题解: P14304 【MX-J27-T1】分块
__DayLight__ · · 题解
前言
签到题。
思路
找规律即可。
对于 1 2 3 易知这 1,因数中有 1 的数有
同理 4 5 6 7 8 平方根下取整都为 2,因数中有 2 的数有
同理 9 10 11 12 13 14 15 平方根下取整都为 3,因数中有 3 的数有
可以知道每一组数中满足题意的数的个数始终是
注意到每组数都是由完全平方数开头,记该数为
再仔细看看,可以发现若将每组平方根下取整所得数记作
深入分析可知对于任意一组数
那就简单了,可以对 ans+=(r-1)*3,即可。
对于
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int q,n;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);//q 也挺大的,建议加上。
cin>>q;
while(q--)
{
cin>>n;
int ans=0;
int r=sqrt(n);
ans+=(r-1)*3;//在 L 之前
int L=r*r,R=(r+1)*(r+1)-1,mid=(L+R)>>1;//R 一定要减 1,否则就变成了下一组数的开头。
ans+=(n>=L)+(n>=mid)+(n>=R);//这样写方便
cout<<ans<<'\n';
}
return 0;
}