HDU 2441

来源:互联网 发布:渲染软件哪个好 编辑:程序博客网 时间:2024/05/22 01:25

ACM(Array Complicated Manipulation)

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


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
230900
 

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

Source
2008 Asia Harbin Regional Contest Online 

数论题
初始为正,从2开始,将2的倍数状态变换,再选择大于2的下一个为正的数,再将其状态变换
最后问某个数是否为正

现将前50个为正的数打表
2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 

那么为负的数就是
4 8 9 12 16 18 20 24 25 27 28 32 36 40 44 45 48 49 50 

可以发现,为负的数其因子都存在平方数
而正的数都没有

因为任意一个正整数都能分解成若干个素数乘积的形式
所以将数分解为各个素因子之和,若某个因子出现平方,则是负,否则为正

另外,看了他人写的题解,才知道该题素因子最大不超过2的16次方
输入的数很大,所以要用高精度除法

代码:

#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<cstdlib>#include<algorithm>#include<map>#include<cmath>#include<stack>#include<queue>#include<set>#include<vector>#define PI acos(-1.0)#define E  exp(1.0)#define INF 0xFFFFFFF#define MAX 100#define len(a) (__int64)strlen(a)#define mem0(a) (memset(a,0,sizeof(a)))#define mem1(a) (memset(a,-1,sizeof(a)))using namespace std;__int64 gcd(__int64 a, __int64 b) {    return b ? gcd(b, a % b) : a;}__int64 lcm(__int64 a, __int64 b) {    return a / gcd(a, b) * b;}int max(int a, int b) {    return a > b ? a : b;}__int64 min(__int64 a, __int64 b) {    return a < b ? a : b;}bool a[66010];int p[66010];int div(char *str,int n){    int sum=0,len=0,i;    char temp[1000];    for(i=0;str[i];i++)    {        sum=sum*10+str[i]-'0';        temp[len++]=sum/n+'0';        sum%=n;    }    temp[len]=0;    if(sum==0)    {        for(i=0;temp[i]=='0';i++)            ;        strcpy(str,temp+i);        return 1;    }    return 0;}char s[1010];int main() {//    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);    int pq;    memset(a,0,sizeof(a));    a[0]=a[1]=1;    int len=0;    for(int i=2;i<66000;i++)    {        if(!a[i])        {            pq=2;            while(i*pq<66000)            {                a[i*pq]=1;                pq++;            }            p[len++]=i;        }    }    int sum,flag;    while(scanf("%s",s)!=EOF&&strcmp(s,"0"))    {        if(!strcmp(s,"1")){puts("no");continue;}        flag=1;        for(int i=0;i<len;i++)        {            sum=0;            while(div(s,p[i]))            {                sum++;                if(sum>1){flag=0;break;}            }            if(!flag)break;        }        if(flag)puts("yes");        else puts("no");    }    return 0;}

0 0
原创粉丝点击