P14663 题解
Solution of P14663
题意:
求出
做法一:暴力
我们直接枚举
#include <bits/stdc++.h>
using namespace std;
int main(){
int T;cin >> T;
while (T--){
int n;cin >> n;
int cnt = 0;
for (int i = 1;i <= sqrt(n);i++){
if (i * i % 3 == 0)cnt++;
}
cout << cnt << "\n";
}
}
暴力的优化:
不难想到,当一个数是
因此,我们只需要枚举
#include <bits/stdc++.h>
using namespace std;
int main(){
int T;cin >> T;
while (T--){
int n;cin >> n;
int cnt = 0;
for (int i = 3;i <= sqrt(n);i+=3){
cnt++;
}
cout << cnt << "\n";
}
}
正解:
由暴力的优化我们可以想到,我们只需要统计
我们把每三个数看成一组,那么每组中就会有且只有一个
因此
因此答案即为
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int T;cin >> T;
while (T--){
int x;scanf("%lld",&x);
int k = sqrt(x);
printf("%lld\n",k/3);
}
}
对于每次询问,获取答案的复杂度为