hdu 2441-ACM(Array Complicated Manipulation)(数学)

ACM(Array Complicated Manipulation)

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


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.


题意:原始数组有2.3.4...∝,现在开始,每一步先找出数组中未找过的最小元素。一开始是2,然后把所有2的倍数都状态反置(本来存在数组的删去,不存在数组的就加进来)。求给出的一个数在不在数组中。

解法:这道题说起来有点坑爹,题目的数据范围没具体说明,害我开到__int64都是错误的,居然必须用大数。其次,Hint中说这个数的最大素因子不会超过1300W,但其实这个数只有2^16这么大,我尝试用1300W去做,结果超时了。

这道题打表之后不难发现,只要这个数的因子中存在平方数,那么这个数就不在数组里(我是打表发现这个规律的)。那么,我们只要把这个大数分解成素因子幂次方乘积(

N=p1^e1*p2^e2*...*pn^en)的方式,只要存在一个幂次方大于等于2的,那么这个数就有一个因子是平方数(就不存在)。否则就存在。


此题注意高精度方面的处理即可。


#include<iostream>
#include <CMATH>
#include <cstring>
#include <cstdio>
using namespace std;
#define N 66000
bool is[N]; __int64 prm[66000];
int getprm(int n){
	int i, j, k = 0;
	int s, e = (int)(sqrt(0.0 + n) + 1);
	memset(is, 1, sizeof(is));
	prm[k++] = 2; is[0] = is[1] = 0;
	for (i = 4; i < n; i += 2) is[i] = 0;
	for (i = 3; i < e; i += 2) if (is[i]) {
		prm[k++] = i;
		for (s = i * 2, j = i * i; j < n; j += s)
			is[j] = 0;
		// 因为j是奇数,所以+奇数i后是偶数,不必处理!
	}
	for ( ; i < n; i += 2) if (is[i]) prm[k++] = i;
	return k; // 返回素数的个数
}
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()
{
//	freopen("test2.out","r",stdin);
	int cnt=getprm(66000);
	int i,n;
	char str[1010];
	while (scanf("%s",&str)&&str[0]!='0')	
	{
		bool judge=true;
		if(strcmp(str,"1")==0)
		{
			printf("no\n");
			continue;
		}
		for(i=0;i<cnt;i++)
		{
			int sum=0;
			while(div(str,prm[i]))
			{
				sum++;
				if(sum>1)
				{
					judge=false;
					break;
				}
			}
		}
		if(judge)
			printf("yes\n");
		else
			printf("no\n");
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值