ZOJ3758:Singles' Day(快速素数判定法)

本文介绍了一个在中国单身节期间超市举办的促销活动案例,顾客可通过获取特定形式的素数来赢得奖品。文中提供了一段C++代码示例,用于判断一个由1组成的特定进制数是否为素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Singles' Day(or One's Day), an unofficial holiday in China, is a pop culture entertaining holiday on November 11 for young Chinese to celebrate their bachelor life. With the meaning of single or bachelor of number '1' and the huge population of young single man. This festival is very popular among young Chinese people. And many Young bachelors organize parties and Karaoke to meet new friends or to try their fortunes that day.

On Singles' Day, a supermarket has a promotional activity. Each customer will get a ticket on which there are two integers b and N, representing an integer M that only contains N digits 1 using b as the radix. And if the number M is a prime number, you will get a gift from the supermarket.

Since there are so many customers, the supermarket manager needs your help.

Input

There are multiple test cases. Each line has two integers b and N indicating the integer M, which might be very large. (2 <= b <= 16, 1 <= N <= 16)

Output

If the customer can get a gift, output "YES", otherwise "NO".

Sample Input
3 3
2 4
2 1
10 2
Sample Output
YES
NO
NO
YES
Hint

For the first sample, b=3, N=3, so M=(111)3, which is 13 in decimal. And since 13 is a prime number, the customer can get a gift, you should output "YES" on a line.

 

题意很简单,就是求长度为n的b进制数在每一位都是1的情况下,是不是素数,这里我用了快速素数判定法

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

long long sum;
long long ppow(int n,int k)
{
    long long ans = 1;
    int i;
    if(n == 0)
        return 1;
    for(i = 1; i<=n; i++)
        ans = ans*k;
    return ans;
}

bool prime (long long num)
{
    if (num == 2 || num == 3 || num == 5)
        return true;
    if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0 || num == 1)
        return false;

    long long c = 7;
    int maxc = (int)(sqrt (num));
    while (c <= maxc)
    {
        if (num % c == 0)
            return false;
        c += 4;
        if (num % c == 0)
            return false;
        c += 2;
        if (num % c == 0)
            return false;
        c += 4;
        if (num % c == 0)
            return false;
        c += 2;
        if (num % c == 0)
            return false;
        c += 4;
        if (num % c == 0)
            return false;
        c += 6;
        if (num % c == 0)
            return false;
        c += 2;
        if (num % c == 0)
            return false;
        c += 6;
    }
    return true;
}


int main()
{
    int i,j,n,b,a;
    while(~scanf("%d%d",&b,&n))
    {
        sum = 0;
        for(i = 0; i<n; i++)
            sum+=ppow(i,b);
        if(sum%2==0 || sum == 1)
            printf("NO\n");
        else
        {
           bool flag = prime(sum);
            if(!flag)
                printf("NO\n");
            else
                printf("YES\n");
        }
    }

    return 0;
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值