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. python+bs4+urllib

    # -*- coding: utf-8 -*- # # # from bs4 import BeautifulSoup import urllib2 import sys reload(sys) sy ...

  2. ORACLE 监听配置

    安装后最开始如下 # listener.ora Network Configuration File: D:\oracle\app\Administrator\product\11.2.0\dbhom ...

  3. gentoo rtthread scons error: unknown type name 'fd_set

    手动在rtconfig.h中加人 #define HAVE_SYS_SELECT_H 就能编译过了

  4. ubuntu14.04 配置g++工具,并运行一个简单的c++文件

    首先,对Ubuntu 14.04 LTS进行源更新,摘自下述链接: http://chenrongya.blog.163.com/blog/static/8747419620143185103297/ ...

  5. js提高篇

    1定义一个方法 function aa(){alert(this)} 对于浏览器 这只是一个方法,那么this是什么的,this理所当然是document对象了..也就是说 打页面定义的方法 都是do ...

  6. 1. ip正则表达式验证

    java: private boolean IsIpAddressValidOrEmpty(String ip) { if (ip == null) return false; String temp ...

  7. python第三方库自动安装脚本

    #python第三方库自动安装脚本,需要在cmd中运行此脚本#BatchInstall.pyimport oslibs = {"numpy","matplotlib&qu ...

  8. java reflect反射调用方法invoke

    类定义 package Reflect; public class MyTest { public int a; public static int b; public static final in ...

  9. ASPxCallback组件(珍藏版)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs ...

  10. linux 任务调度

    crontab 定时任务 -e  编辑 -l  查看 -r  删除 参数 * * * * * 分钟  小时  天数   月   星期几