Squarefree number

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

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
 
翻译:输入一个n,如果可以被一个平方数整除,则不是平方自由数,输出no,否则输出yes
分析:显然要分解质因数,根据唯一分解定理分解。
n<=10^18,不能用暴力求到10^9的素数,欧拉筛一般只是找10^6内的素数。显然需要优化。
如果n>10^6,巨大,筛完了10^6内的质因子后,n还是大于10^6,则
1.如果n不是平方自由数,则因子中包含质因数的平方,则n=p*p,p是素数,p>10^6,除此之外没有别的大于10^6的因子了,否则n>10^18
2.如果n是平方自由数,则因子中不包含质因数的平方
(1)n是素数
(2)n不是素数,n=p1*p2,是两个大素数的乘积。p1,p2>10^6
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; ll prime[];
ll cnt,t,n;
int maxx=1e6+;
bool vis[]; void init()
{
memset(vis,true,sizeof(vis));
vis[]=vis[]=false;
cnt=;
for(ll i=;i<=maxx;i++)
{
if(vis[i])
prime[cnt++]=i;
for(ll j=;j<cnt && i*prime[j]<=maxx;j++)
{
vis[ i*prime[j] ]=false;
if( i%prime[j]== ) break;
}
}
} int main()
{
init();
scanf("%lld",&t);
for(ll k=;k<=t;k++)
{
bool flag=false;
scanf("%lld",&n);
for(ll i=;i<cnt && !flag;i++)
{
int num=;
while( n%prime[i]== )///分解质因子
{
n=n/prime[i];
num++;
}
if(num>=)
{
flag=true;
break;
}
}
/*分解完所有小于10^6的质因子,出来后的这个n还大于1的有三种情况
第一种情况,是一个 大于10^6 的素数
第二种情况,是两个 大于10^6 的素数 的乘积,不可能还有第三个大于10^6的质因子
第三种情况,是一个 大于10^6 的素数 的平方,不可能还有第三个大于10^6的质因子
*/
if(!flag && n>)
{
ll temp=(ll)sqrt(n);
if(temp*temp==n)
flag=true;
}
if(flag)
printf("Case %lld: No\n",k);
else
printf("Case %lld: Yes\n",k);
}
return ;
}

hdu3826-Squarefree number-(欧拉筛+唯一分解定理)的更多相关文章

  1. hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)

    Deciphering Password Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 2018南京icpc-J-Prime Game (欧拉筛+唯一分解定理)

    题意:给定n个数ai(n<=1e6,ai<=1e6),定义,并且fac(l,r)为mul(l,r)的不同质因数的个数,求 思路:可以先用欧拉筛求出1e6以内的所有质数,然后对所有ai判断, ...

  3. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  4. noip复习——线性筛(欧拉筛)

    整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...

  5. 欧拉筛,线性筛,洛谷P2158仪仗队

    题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...

  6. hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)

    YAPTCHA Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. Bi-shoe and Phi-shoe(欧拉筛)

    Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...

  8. POJ2909_Goldbach's Conjecture(线性欧拉筛)

    Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one p ...

  9. 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛

    欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...

随机推荐

  1. 入坑机器学习?听听MIT在读博士的AI心得

    随着人工智能技术的火热,越来越多的年轻学者正准备投身其中,开启自己的研究之路.和所有其他学科一样,人工智能领域的新人总会遇到各种各样的难题,其中不仅有研究上的,也有生活方面的.MIT EECS 在读博 ...

  2. implementation compile的区别

    implementation不可以依赖传递:依赖对app Module 是不可见的 compile可以依赖传递:依赖对app Module 是可见的 AndroidStudio升级到3.0以上后,第一 ...

  3. IIS7.5和IIS8如何设置FTP的pasv端口范围

    如果不设置端口范围,在防火墙开启的情况下,连接FTP时可能出现列表错误的现象,下面介绍下如何设置FTP的pasv端口范围.. 一.首先打开IIS选择服务器会进入全局设置,再双击FTP防火墙支持 二.设 ...

  4. luoguP1368 工艺(最小表示法 后缀自动机)

    最小表示法就是直接扫过去 后缀自动机就是每次找最字典序最小儿子输出 最小表示法 /* 最小表示法裸题, 我好像学过来着?? 怎么忘得这么干净 */ #include<cstdio> #in ...

  5. virtualenv与virtualenvwrapper讲解

    #现在有个需求,我现在有两个项目,一个得运行于django1.1.11 ,还有一个版本得跑在django2.0上 python3 manage.py runserver  0.0.0.0:8000   ...

  6. MySQL 中的三中循环 while loop repeat 的基本用法

    -- MySQL中的三中循环 while . loop .repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和 /* while循环语法: while 条件 DO 循 ...

  7. 前端笔记二:CSS盒模型

    1.标准模型和IE模型 2.标准模型和IE模型的区别 标准模型的height和width只是content的: IE模型的height和width是包含padding和border的 3.CSS如何设 ...

  8. 微信小程序笔记<七>视图层 —— wxml

    微信小程序的视图层由 *.wxml 组成,wxml与html一样属于标签语言,但wxml与html的标签截然不一样. xwml特性 一.数据绑定 <!--wxml--> <view& ...

  9. 转:JSON 获取属性值的方法

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript(Standard ECMA-262 ...

  10. 使用git时报错出现vim.exe.stackdump

    使用git时报错出现vim.exe.stackdump 关闭命令行重新打开试试   一般由于异常报错引起的