UVALIve 5987 素数
题目链接:Distinct Primes
如果一个数。至少有三个因子是素数、。那么这个数就是prime num.30和42是前两个prime num.问你第n个这种数是谁。(1<=n<=1000)。
用质因子分解。判断每个数有多少个因子是质数。如果超过3个旧记录下来、记录前1000个。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std; typedef long long ll;
#define N 5000
int isprime[N];
ll prime[N], nprime, factor[N], numfactor[N], ct; void makeprime() // 打出1到N的素数表。
{
int i, j, temp;
nprime = ;
memset(isprime, , sizeof(isprime));
isprime[] = ;
temp = sqrt(N+0.0);
for (i=; i<=temp; ++i)
{
if (isprime[i])
{
for (j=i+i; j<N; j+=i)
{
isprime[j] = ;
}
}
}
for (int i=; i<N; ++i)
{
if (isprime[i]) prime[nprime++] = i;
}
} int divide(int n) // 对n进行质因子分解。ct表示质因子的个数、
{
int i;
int temp = sqrt(n+0.0);
ct = ;
memset(numfactor, , sizeof(numfactor));
for (i=; i<=nprime; ++i)
{
if (prime[i] > temp) break;
if (n % prime[i] == )
{
factor[++ct] = prime[i];
while(n % prime[i] == )
{
n /= prime[i];
}
}
}
if (n != )
{
factor[++ct] = n;
}
return ct;
} int main()
{
int num[], cnt = ;
makeprime();
for (int i=; ; ++i)
{
if (divide(i) >= ) num[cnt++] = i;
if (cnt > ) break;
}
int t, n;
cin >> t;
while(t--)
{
cin >> n;
cout << num[n-] << endl;
}
return ;
}
UVALIve 5987 素数的更多相关文章
- UVALive 5987
求第n个数,该数满足至少由3个不同的素数的乘机组成 #include #include #include #include #include using namespace std; int prim ...
- 【筛法求素数】【推导】【组合数】UVALive - 7642 - Prime Distance
题意:n个格子,m个球,让你把球放入某些格子里,使得所有有球的格子之间的距离(abs(i-j))均为素数 ,让你输出方案数. 只占一个格子或者两个格子显然可行. 占有三个格子的情况下,则必须保证其中两 ...
- HDOJ 1164 Eddy's research I(拆分成素数因子)
Problem Description Eddy's interest is very extensive, recently he is interested in prime number. Ed ...
- 素数测试算法(基于Miller-Rabin的MC算法) // Fermat素数测试法
在以往判断一个数n是不是素数时,我们都是采用i从2到sqrt(n)能否整除n.如果能整除,则n是合数;否则是素数.但是该算法的时间复杂度为O(sqrt(n)),当n较大时,时间性能很差,特别是在网络安 ...
- ACM -- 算法小结(十)素数的两种打表法
素数的两种打表法 下面介绍两种素数打表法,由于是两年前留下的笔记,所以没有原创链接~~ @_@!! 第一种疯狂打表法: #include<stdio.h> #include<math ...
- Help Hanzo (素数筛+区间枚举)
Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000). (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- 求解第N个素数
任务 求解第 10,0000.100,0000.1000,0000 ... 个素数(要求精确解). 想法 Sieve of Eratosthenes 学习初等数论的时候曾经学过埃拉托斯特尼筛法(Sie ...
- 使用BitArray判断素数
首先显示1024范围内的所有素数,然后显示输入的数是否是素数.1024 是代码中计算的素数的范围,可以修改.计算平方根,是为了确定一个基数的范围.1024的平方根是32,两个超过32 的数相乘,肯定大 ...
随机推荐
- Python3基础 while 斐波那契数列
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- python3+pyqt5 +eric5安装配置
一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...
- MySQL中查询所有数据库占用磁盘空间大小和单个库中所有表的大小的sql语句
查询所有数据库占用磁盘空间大小的SQL语句: ,),' MB') as data_size, concat(,),'MB') as index_size from information_schema ...
- 使用CCleaner卸载chrome
Google Chrome Update Patches Zero-Day Actively Exploited in the Wild 如果有同事使用google Chrome浏览器的话,请检查版本 ...
- github上不去了
这几天发现github.com上不去了 可能是由于要杜绝国外的人使用最新的技术??从而屏蔽了吗?
- 【TCP/IP详解 卷一:协议】第二章:链路层
2.1 引言 链路层的三个目的: (1)为IP模块发送和接收IP数据报. (2)为ARP模块发送ARP请求和接收ARP应答.地址解析协议:ARP. (3)为RARP模块发送RARP请求和接收RARP应 ...
- poj 1144 Network 无向图求割点
Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...
- shell while 语句
普通循环格式: while condition do command done 例子一 #!/bin/bash )) do echo $int let "int++" done 结 ...
- CSS3 媒体查询@media 查询(响应式布局)
例:如果文档宽度小于 300 像素则修改背景颜色(background-color): @media screen and (max-width: 300px) { body { background ...
- 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node
2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...