Background
MaoLaoDa likes studying numbers, especially the prime numbers, very much. One day, MaoLaoDa found that a number could be expressed as the product of two prime numbers. For instance, 10 = 2 x 5, and he called this kind of numbers MaoLaoDa Numbers. Now, give you a number N and you should tell me whether it is a MaoLaoDa number or not.
Input
Multiple test cases, each contains a positive integer, N (N <= 231 - 1).
Output
For each case, print "Yes" when N is a MaoLaoDa number, or you should print "No".
Sample Input
10 11
Sample Output
Yes No
解题报告:
此题就是问一个数是否能化为两个素数相乘。范围在int型内,开根号可知只需要找到前50000个数中的素数即可。代码如下:
- int prime[6000],num;
- bool visited[50000];
- void isprime(){
- num=0;
- memset(visited,0,sizeof(visited));
- memset(prime,0,sizeof(prime));
- for(int i=2; i<=50000; i++){
- if(visited[i] == 0)
- prime[num++] = i;
- for(int j=0; j<num && prime[j]*i<=50000; j++){
- visited[prime[j]*i] = 1;
- if(i%prime[j] == 0)
- break;
- }
- }
- }
- bool primes(int n){
- for(int i=0;prime[i]*prime[i]<=n;i++)
- if(n%prime[i]==0)
- return false;
- return true;
- }
- int main(){
- isprime();
- int n;
- while(scanf("%d",&n)==1){
- bool flag=false;
- for(int i=0;(long long)prime[i]*prime[i]<=n;i++)
- if(n%prime[i]==0)
- if(primes(n/prime[i])){
- flag=true;
- break;
- }
- if(flag)
- printf("Yes\n");
- else
- printf("No\n");
- }
- return 0;
- }