230B - T-primes 难度1300

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的最大整数

小结论

专注刷题,每一天完成任务了吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值