【Daily Code AcWing】PAT可逆质数

这个是我自己的,也不知道是哪里有问题,在AcWing上面过不了。因为测试集比较大我只能看到一部分。
+++new+++
知道哪里错了……Yes,我输出YES;No,我输出NO……
y总的代码真的是很整洁明了
emm照着y总的代码在平台上过了一遍

#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>

using namespace std;
typedef long long LL;
LL N, D;

// x -> 10进制
int check(LL x) {
    if(x == 1 || x == 0) return -1;
    if(x == 2 || x == 3) return 1;
    for (LL i = 2; i * i <= x; i ++ ) {
        if(x % i  == 0) return -1;
    }
    return 1;
}

string num_str(LL x, LL radix) {
    string ans = "";
	do{
		LL t = x % radix;
		if(t >= 0 && t <= 9) ans += (t + '0');
		else ans += (t - 10 + 'a');
		x /= radix;
	} while ( x != 0 );	//使用do{}while()以防止输入为0的情况
	return ans;	
}

LL chance(string c, LL radix) {
    LL tmp = 0;
    for (int i = 0; i < c.size(); i ++ ) {
        if(c[i] <= '9') {
            tmp = tmp * radix + 1L * (c[i] - '0');
        }
        else if(c[i] <= 'z' && c[i] >= 'a'){
            tmp = tmp * radix + 1L * (c[i] - 'a' + 10);
        }
        else if(c[i] <= 'Z' && c[i] >= 'A'){
            tmp = tmp * radix + 1L *(c[i] - 'A' + 10);
        }
    }
    return tmp;
}

int main()
{
    cin >> N;
    while(N >= 1) {
        cin >> D;
        // N不是质数
        if(check(N) == -1) cout << "NO" << endl;
        else {
            string s = num_str(N, D);
            // cout << s << endl;
            LL res = chance(s, D);
            // cout << "res: " << res << endl;
            if(check(res) == 1) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        cin >> N;
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值