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)
OutputIf the customer can get a gift, output "YES", otherwise "NO".
Sample Input3 3 2 4 2 1 10 2Sample Output
YES NO NO YESHint
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.
题意:给出进制和位数,每个位数上的数字是1,求出该数的十进制数,如果是素数就输出YES,否则就输出NO。
题解:就按着题目的意思做就好,先转换进制数,再判断是否为素数。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int prime(long long x)
{
long long i;
for(i = 2;i * i <= x;i++)
{
if(x % i == 0)
break;
}
if(i * i > x)
return 1;
else
return 0;
}
int main()
{
long long b;
long long N;
long long num;
int i;
while(scanf("%I64d%I64d",&b,&N) != EOF)
{
num = 0;
for(i = 1;i <= N;i++)
{
num = num * b + 1;
}
if(num == 1)
{
printf("NO\n");
continue;
}
if(prime(num))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}