Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
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.
Source
ZOJ Monthly, March 2014
题意:
很好理解
b是指b进制
N是长度 有N个1
例如 b=4 N=6
(111 111)4
求出它的十进制M ,如果M是素数 输出YES 否则输出NO
注意:
1.不明白为什么第五行定义成int就会超时
2.另外避免错误
pow(x,y)的使用时,x强制为double型 x,y在pow函数中的范围是 x>=0&&y是小数 或者 x=0&&y<=0
#include<stdio.h>
#include<math.h>
int prime (long long n)
{
long long i;//定义成int会超时
int t;
if(n<=1) return 0;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
int main (void)
{
long long sum;
int a,b;
while(~scanf("%d%d",&a,&b))
{
sum=0;
for(int i=b-1;i>=0;i--)
{
sum=sum+pow((double)a,i);
}
//sum-=pow(a,bb);
//sum+=1;
//printf("sum %lld\n",sum);
if(prime(sum)==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}