ACM HDU2442 ACM(Array Complicated Manipulation)

http://acm.hdu.edu.cn/showproblem.php?pid=2441

ACM(Array Complicated Manipulation)

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 258    Accepted Submission(s): 60


Problem Description
Given an infinite array of integers 2,3,.... Now do some operations on it.

The operation is to choose a minimum number from the array which is never been chosen, then change the status of its multiples excluding itself, i.e remove the multiples of the chosen number if they are in the array , otherwise add it to the array.keep the order after change.

For instance, the first step, choose number 2, change the status of 4, 6, 8, 10... They are all removed from the array. The second step, choose 3, change the status of 6, 9, 12, 15...

Pay attention: 9 and 15 are removed from the array while 6 and 12 are added to the array.
 

Input
Every line contains an integer n. The zero value for n indicates the end of input.
 

Output
Print "yes" or "no" according whether n is in the array.

 

Sample Input
2 30 90 0
 

Sample Output
yes yes no
Hint
The number n never has a prime factor greater than 13000000, but n may be extremely large.
 

Source
 

Recommend
gaojie
 
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 65536
char str[1000];
//打表产生MAX一下的全部素数
//用筛选法
int prime[MAX];
int vext()
{
    bool num[MAX+1];
    int i,j,len;
    double t=sqrt((double)MAX);
    memset(num,1,sizeof(num));//关于memset初始化当心,这里是bool类型的,非0就为真的
    for(i=2;i<t;i++)
    {
        if(num[i])
        {
            for(j=2;i*j<MAX;j++)
               num[i*j]=0;
        }    
    }
    len=0;
    for(i=2;i<MAX;i++)
      if(num[i])prime[len++]=i;
    //len记录着总的素数的长度
    return len;    
} 
//高精度除法,判断p是否被n整除
bool div(char *p,int n)
{
    char temp[1000];
    int i,sum=0,len=0;
    for(i=0;p[i]!=0;i++)
    {
        sum=sum*10+p[i]-'0';
        temp[len++]=sum/n+'0';
        sum%=n;
    }  
    temp[len]=0;
    if(sum==0)
    {
        for(i=0;temp[i]=='0';i++);
        strcpy(p,temp+i);
        return 1;
    }
    else return 0;      
} 
int main()
{
    int i,j,len;
    len=vext();
    while(scanf("%s",str),strcmp(str,"0")!=0)
    {
        if(strcmp(str,"1")==0)
        {
            printf("no\n");
            continue;
        } 
        int count;
        for(i=0;i<len;i++)
        {
            count=0;
            while(div(str,prime[i]))
            {
                count++;
                if(count>=2)break;
            }  
            if(count>=2)break;  
        }
        if(count>=2)printf("no\n");
        else printf("yes\n");       
    }
    return 0;    
    
}           

posted on 2011-07-23 23:40  kuangbin  阅读(567)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: