uva10392 Factoring Large Numbers

本文涉及的知识点是,使用线性筛选法得到素数表。

Table of Contents

1 题目

====================

Problem F: Factoring Large Numbers

One of the central ideas behind much cryptography is that factoring large numbers is computationally intensive. In this context one might use a 100 digit number that was a product of two 50 digit prime numbers. Even with the fastest projected computers this factorization will take hundreds of years.

You don't have those computers available, but if you are clever you can still factor fairly large numbers.

Input

The input will be a sequence of integer values, one per line, terminated by a negative number. The numbers will fit in gcc's long long int datatype. You may assume that there will be at most one factor more than 1000000.

Output

Each positive number from the input must be factored and all factors (other than 1) printed out. The factors must be printed in ascending order with 4 leading spaces preceding a left justified number, and followed by a single blank line.

Sample Input

90
1234567891
18991325453139
12745267386521023
-1

Sample Output

    2
3
3
5 1234567891 3
3
13
179
271
1381
2423 30971
411522630413

====================
 

2 思路

这是一个分解质因数问题,因为题目说明了数字不超过long long int表示的数,所以 这不是一个大数问题。基本的思路是首先得到素数表,再用输入的大数对素数表中的 素数依次作除法运算。

关键的地方有两个,一个是素数表的获得。这里使用了线性筛选法,把所有的合数筛去, 关键的思想在于每个合数都是被它的最小素因子筛去的,且只会被筛一次。

关键代码在于:

 if (!i%prime[k]) break;

之所以可以break在于,i可以整除prime[k], 在于下一次循环要筛的数字i*prime[k+1]一定已经被prime[k]筛过了,因为素数表是按大小排列的,prime[k]比prime[k+1]小,而每个合数都是被它最小的素因子筛出去的。

第二个点在于题目中已经说明,最多有一个素因子大于1000000,所以素数表只开到1000000就可以了, 如果遍历完素数表输入的数字还没有变成1,那么,将其最终结果输出就可以了,这个结果 就是那个大于1000000的因子,否则它一定可以被1000000内的素数整除。

3 代码

#include <stdio.h>
#include <string.h> #define N 1000000
long long prime[N];
short is_prime[N]; long long get_prime (long long prime[], long long n) {
long long i, j, k;
memset (is_prime, 0, sizeof(is_prime[0])*n); j = 0;
for (i=2; i<n; i++) {
if (!is_prime[i]) prime[j++] = i;
for (k=0; k<j && i*prime[k]<n; k++) {
is_prime[ i*prime[k] ] = 1;
if (!i%prime[k]) break;
}
} return j;
} int main() {
long long n;
long long i;
long long prime_num; prime_num = get_prime (prime, N); while (scanf ("%lld", &n) != EOF) {
if (n == -1) break; for (i=0; i<prime_num && n!=1; i++) {
while (n % prime[i] == 0) {
printf (" %lld\n", prime[i]);
n /= prime[i];
}
}
if (n != 1) printf (" %lld\n", n);
printf ("\n");
} return 0;
}

uva10392 Factoring Large Numbers的更多相关文章

  1. [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers

    When looking at large numbers in code (such as 1800000) it’s oftentimes difficult for the human eye ...

  2. 【概率论】6-2:大数定理(The Law of Large Numbers)

    title: [概率论]6-2:大数定理(The Law of Large Numbers) categories: - Mathematic - Probability keywords: - Ma ...

  3. Law of large numbers and Central limit theorem

    大数定律 Law of large numbers (LLN) 虽然名字是 Law,但其实是严格证明过的 Theorem weak law of large number (Khinchin's la ...

  4. 中心极限定理 | central limit theorem | 大数定律 | law of large numbers

    每个大学教材上都会提到这个定理,枯燥地给出了定义和公式,并没有解释来龙去脉,导致大多数人望而生畏,并没有理解它的美. <女士品茶>有感 待续~ 参考:怎样理解和区分中心极限定理与大数定律?

  5. Markov and Chebyshev Inequalities and the Weak Law of Large Numbers

    https://www.math.wustl.edu/~russw/f10.math493/chebyshev.pdf http://www.tkiryl.com/Probability/Chapte ...

  6. 大数定律(Law of Large Numbers)

    大数定律:每次从总体中随机抽取1个样本,这样抽取很多次后,样本的均值会趋近于总体的期望.也可以理解为:从总体中抽取容量为n的样本,样本容量n越大,样本的均值越趋近于总体的期望.当样本容量极大时,样本均 ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  9. UVA 10392 (13.07.28)

    Problem F: Factoring Large Numbers One of the central ideas behind much cryptography is that factori ...

随机推荐

  1. Yii 1.1.17 五、分页类、关联模型、权限验证与默认页面跳转

    一.分页类使用 1.在控制器中 // 实例化 $criteria = new CDbCriteria(); $articleModel = Article::model(); // 分页 $total ...

  2. 工具安装===Sublime Text-安装

    Sublime Text 是一款通用型轻量级编辑器,支持多种编程语言.有许多功能强大的快捷键(如 Ctrl+d),支持丰富的插件扩展.如果平时需要在不同编程语言间切换,那么它将会是一个,不错的选择. ...

  3. [Deep dig] ViewController初始化过程调查

    代码:https://github.com/xufeng79x/ViewControllerLife 1.简介: 介绍xib方式.storyborad方式以及code方式下ViewController ...

  4. squid 代理服务

    squid代理服务分为两种方式: 一.正向代理(用在企业的办公环境中,员工上网需要通过Squid代理来上网) 客户端发送请求到代理服务器,代理服务器去向真正的服务器请求结果,并将结果返回给客户端 二. ...

  5. ArcGIS Server 基于Token安全验证

    写在前面:只使用token并不能起到安全验证的作用,ArcGIS Server文件夹的权限是开放的,我们不需要登录Server平台即可访问服务,所以我们应该将Token验证和文件夹的安全性结合起来使用 ...

  6. http跟https的区别

    http: Hypertext transform protocol 超文本传输协议 是一个为了传输超媒体文档(比如html)的应用层协议 是为了web的浏览器跟web的server端的交流而设计的, ...

  7. Python+Selenium 自动化实现实例-Link 捕捉元素的几种方法

    from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.baidu.com&quo ...

  8. mysql5.7 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    mysql5.7初次登录使用提示 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before ...

  9. JS将日期转换为yyyy-MM-dd HH:mm:ss

    //格式化后日期为:yyyy-MM-dd HH:mm:ss function FormatAllDate(sDate) { var date = new Date(sDate); var sepera ...

  10. [你必须知道的.NET]第二十七回:interface到底继承于object吗?

    发布日期:2009.03.05 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 说在,开篇之前 在.NET世界里,我们常常听到的一句话莫过于“S ...