hdu 3826 数论 n能否被含有因子是一个平方数的数整出 很不错的题目

Squarefree number

Time Limit: 10000/3000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1404Accepted Submission(s): 373


Problem Description
In mathematics, a squarefree number is one which is divisible by no perfect squares, except 1. For example, 10 is square-free but 18 is not, as it is divisible by 9 = 3^2. Now you need to determine whether an integer is squarefree or not.

Input
The first line contains an integer T indicating the number of test cases.
For each test case, there is a single line contains an integer N.

Technical Specification

1. 1 <= T <= 20
2. 2 <= N <= 10^18

Output
For each test case, output the case number first. Then output "Yes" if N is squarefree, "No" otherwise.

Sample Input
 
 
2 30 75

Sample Output
 
 
Case 1: Yes Case 2: No

Author
hanshuai
题意:
n是否能被一个含有因子是一个平方数的数整出

能输出no 不能输出yes

/*一个数n能被一个数的平方整除  那么这个数一定能被一个质数的平方整出 
如果能被一个偶数的平方整出 由于偶数=2*x 一定含有质数2 故能被质素2的平方整出
 假如 一个数能被 一个奇数的平方整除 奇数=质数*x 故能被质素的平方整出
对于本题数据较大 可以做如下处理
如果n>10^6
1 求出10^6内的所有质数 看n是否可以分解成这些质数某个的平方 如果可以输出No
2 数据最大为10^18 当那么最多存在一个数x使得  x*x整出n 因为n*n*n大于10^18
故对其开方 如果能开放 则输出No  

思维扩展:
对于一个平方数 一定可以分解成某个质数的平方*某些质数
*/ 
#include<stdio.h>
#include<math.h>
#include<string.h>
int vis[1000000+5];
int prime[79000];
int cnt[79000+5];
int c;
void get_prime()
{
	int i,j,n,m;
	c=0;
	n=1000000;
	m=(int)sqrt(n+0.5);
	memset(vis,0,sizeof(vis));
	for(i=2;i<=m;i++) 
		if(!vis[i])
		{
			for(j=i*i;j<=n;j+=i)
				vis[j]=1;
		}
	for(i=2;i<=n;i++) if(!vis[i])
		prime[c++]=i;
}
#include<stdio.h>
int main()
{
     int i,j,cas,flag,k;
	 __int64 n,m;
	 double num;
	 get_prime();
	 scanf("%d",&cas);
	 for(k=1;k<=cas;k++)
	 {
		 scanf("%I64d",&n);
          memset(cnt,0,sizeof(cnt)); flag=0;      
			   for(j=0;j<c;j++)
			   {
				   m=n;
				   while(m%prime[j]==0)
				   {
					   m/=prime[j];
					   cnt[j]++;
					   if(cnt[j]>=2) {flag=1;break;}
				   }
				   if(flag) break;
			   }	  
		  if(n>1000000)
		  {
			  num=sqrt((double)n);//这里要注意 必须要加double  否则编译错误
		     if(floor(num+0.5)==num) flag=1;
		  }
		  if(flag) printf("Case %d: No\n",k);
		  else printf("Case %d: Yes\n",k);
	 }
	 return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值