`
Simone_chou
  • 浏览: 184575 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Singles' Day(素数判断 + 进制转化)

    博客分类:
  • ZOJ
 
阅读更多
Singles' Day
Time Limit: 2 Seconds      Memory Limit: 65536 KB

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. 

 

     题意:

     给出 B,N(1 ~ 16),代表用 N 个 1 构成的 B 进制数。判断这个数是不是素数,是则输出 Yes,不是则输出 No。

 

     思路:

     N 进制化成十进制后判断是否素数即可。主要要开unsign long long,且循环到开方数,而不是数的一半,不然会超时。

     一开始循环到num / 2 来判断,导致 TLE,后想了各种办法推了各种公式,也知道可以打表,但是一直以为是什么需要技巧的题目。

 

     AC:

#include<stdio.h>

int test(unsigned long long num)
{
    if(num == 1)    return 0;
    for(unsigned long long i = 2;i * i <= num;i++)
        if(!(num % i)) return 0;
    return 1;
}

int main()
{
    int b,n;
    while(~scanf("%d%d",&b,&n))
    {
        unsigned long long sum = 0,k = 1;
        for(int i = 0;i <= n - 1;i++)
        {
            sum += k;
            k *= b;
        }

        if(test(sum))   printf("YES\n");
        else            printf("NO\n");
    }
    return 0;
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics