文章目录
230B - T-primes
问题描述 难度1300
判断T-prime(被1和自身和自己的平方根整除)
代码
/*
Dreams never shine!
It's you that shine while chasing your dreams :)
JAYO!!
*/
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
bool isPrime(LL x) {
if (x == 1) {
return 0;
}
for (LL i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
int main()
{
ios_base::sync_with_stdio(0);
int Kase;
cin >> Kase;
while (Kase--) {
LL num;
cin >> num;
if (sqrtl(num) != floor(sqrtl(num))) {
// T-prime能被1,自身,还有自己的平方根整除
cout << "NO\n";
continue;
}
if (isPrime(sqrtl(num))) { // 这个平方根要唯一(不能被分解),不然就不是Triple-prime 了
cout << "YES\n";
}
else cout << "NO\n";
}
return 0;
}
知识点
std::ios, std::basic_ios, std::ios_base
sync_with_stdio(false) 应该是定义在父类ios_base中的
std::ios 子类
std::basic_ios<cT, Traits>) 模板
std::ios_base 为父类
ios_base::sync_with_stdio(false);
sqrtl ()
floor函数
1.The constants are actually defined in std::ios_base but std::ios (well, actually std::basic_ios<cT, Traits>)
is derived from std::ios_base. Thus, all members defined in std::ios_base can be accessed using std::ios.
2.The class std::ios_base contains all members which entirely independent of the stream's template parameter.
std::basic_ios<cT, Traits> derives from std::ios_base and output streams.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
在考虑效率且没有必要刷新输出流时使用cout << . . . << "\n";
在一些大程序需要刷新输出流时使用cout << . . . << endl;
std::ios::sync_with_stdio(false);
iostream默认是与stdio关联在一起的,以使两者同步,因此消耗了
iostream不少性能。
通过tie(nullptr)来解除std :: cin和std :: cout之间的绑定,进一步加快执行效率
Math 类的 long double sqrtl () 方法以精度返回 long double 变量的平方根。
floor函数,其功能是“向下取整”,或者说“向下舍入”、“向零取舍”,即取不大于x的最大整数
小结论
专注刷题,每一天完成任务了吗?