SPOJ 74. Divisor Summation 分解数字的因子
本题有两个难点:
1 大量的数据输入。没处理好就超时 - 这里使用buffer解决
2 因子分解的算法 a)暴力法超时 b)使用sieve(筛子),只是当中的算法逻辑也挺不easy搞对的。
数值N因子分解逻辑:
1 保存全部能够sqrt(N)范围内的质素
2 找到能够被N除尽的质素d, 然后用d去除N。使用deg变量,保存度。即有多少个d能够被N除尽
3 用d去乘全部已经找到的因子(包含1),假设度deg大于1。那么循环i从1到deg, 用d*i去乘全部找到的因子
找到全部因子相加,减去N,就是答案。
原题:http://www.spoj.com/problems/DIVSUM/
本题是tutorial题,能够说不是非常难的题目,只是看了下提交的记录,超时的2.5万左右,AC的1万左右。
当中包括数学思想的:
-- by Rosen
THE FUNDAMENTAL THEOREM OF ARITHMETIC:
Every integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size.
The prime factorizations of 100, 641, 999, and 1024 are given by
100 = 2 · 2 · 5 · 5 = 2 2 5 2 ,
641 = 641,
999 = 3 · 3 · 3 · 37 = 3 3 · 37,
1024 = 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 · 2 = 2 10
-- 本题因式分解的基本数学思想
只是本题不单须要质数,而是须要全部能除尽的数,那么就是这些质数组合起来了。
If n is a composite integer, then n has a prime divisor less than or equal to√ n.
it follows that an integer is prime if it is not divisible by any prime less than or equal to its square root. This leads to the brute-force algorithm known as trial division.
-- 这个是半暴力法用来找素数(也叫质数)的数学思想
- class DivisorSummation47
- {
- const static int MAX_BUF = 5120;
- int st, len;
- char inBuf[MAX_BUF];
- const static int FLASH_P = MAX_BUF - 12;
- int oSt;
- char outBuf[MAX_BUF];
- const static int MAX_NUM = 500000;
- bool *sieve;
- char getFromBuf()
- {
- if (st >= len)
- {
- len = fread(inBuf, sizeof(char), MAX_BUF, stdin);
- st = 0;
- }
- return inBuf[st++];
- }
- int intFromBuf()
- {
- char c = getFromBuf();
- while (c < '0' || '9' < c && len)
- {
- c = getFromBuf();
- }
- int num = 0;
- while ('0' <= c && c <= '9' && len)
- {
- num = (num<<3) + (num<<1) + (c - '0');
- c = getFromBuf();
- }
- return num;
- }
- void wrToBuf(int num, char sep)
- {
- if (oSt > FLASH_P)
- {
- fwrite(outBuf, sizeof(char), oSt, stdout);
- oSt = 0;
- }
- if (0 == num)
- {
- outBuf[oSt++] = '0';
- outBuf[oSt++] = sep;//漏了这句错误
- return;
- }
- char chs[12];
- int i = 0;
- while (num)
- {
- chs[i++] = num % 10 + '0';//这里竟然忘记步进i错误
- num /= 10;
- }
- for (i--; i >= 0; i--)
- {
- outBuf[oSt++] = chs[i];
- }
- outBuf[oSt++] = sep;
- }
- inline void flashLeft()
- {
- if (oSt) fwrite(outBuf, sizeof(char), oSt, stdout);
- }
- public:
- DivisorSummation47() : st(0), len(0), oSt(0)
- {
- int sq = (int)sqrt(double(MAX_NUM));
- sieve = (bool *) calloc(sizeof(bool), sq+1);
- //fill(sieve, sieve+sq+1, false);
- vector<int> primes;
- for (int i = 2; i <= sq; i++)
- {
- if (!sieve[i])
- {
- for (int j = (i<<1); j <= sq; j += i)
- {
- sieve[j] = true;
- }
- primes.push_back(i);
- }
- }
- int T = 0;
- T = intFromBuf();
- while (T--)
- {
- int num = intFromBuf();
- int N = num;
- vector<int> divs(1, 1);
- for (int i = 0; i < (int)primes.size() && num > 1; i++)
- {
- int d = primes[i];
- if (d*d > num) d = num;
- if (num % d == 0)
- {
- int deg = 0;
- for ( ; num % d == 0; num /= d) deg++;
- for (int j = (int)divs.size() - 1; j >= 0 ; j--)
- {
- int t = divs[j];
- for (int k = 0; k < deg; k++)
- {
- t *= d;
- divs.push_back(t);
- }
- }
- }
- }
- int ans = -N;
- for (int i = 0; i < (int)divs.size(); i++)
- {
- ans += divs[i];
- }
- wrToBuf(ans, '\n');
- }//while(T--)
- flashLeft();
- }
- };
SPOJ 74. Divisor Summation 分解数字的因子的更多相关文章
- SPOJ DIVSUM - Divisor Summation
DIVSUM - Divisor Summation #number-theory Given a natural number n (1 <= n <= 500000), please ...
- HDU6623 思维题(n分解成质因子的形式,问最小的幂是多少)
题目大意:给你一个数n,把它分解为素数的幂次的乘积的形式:n=p1^e1 * p2^e2 * .......pk^ek 求最小的幂次是多少 n=le18 分析: 首先我们肯定是不可以枚举1e18的因 ...
- spoj 1029 Matrix Summation
题意: 对一个矩阵有2种操作: 1.把某个元素设为x. 2.查询以(x1,y1)为左上角 以(x2,y2)为右上角的矩阵中的数字的和. 思路: 二维树状数组入门题,同时对横坐标和纵坐标做前缀和就行了. ...
- zoj 2095 Divisor Summation
和 hdu 1215 一个意思// 只是我 1坑了 1 时应该为0 #include <iostream> #include <math.h> #include <map ...
- 【C/C++】任意大于1的整数分解成素数因子乘积的形式
// #include<stdio.h> #include<math.h> #include<malloc.h> int isprime(long n); void ...
- SPOJ 1029 Matrix Summation【 二维树状数组 】
题意:二维树状数组,更改值的时候有一点不一样, 是将a[x][y]设置为一个值,所以add的时候要将它和以前的值作差一下 #include<iostream> #include<cs ...
- codeforces 1025B Weakened Common Divisor(质因数分解)
题意: 给你n对数,求一个数,可以让他整除每一对数的其中一个 思路: 枚举第一对数的质因数,然后暴力 代码: #include<iostream> #include<cstdio&g ...
- uva 993 Product of digits (贪心 + 分解因子)
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
- R语言基础:数组&列表&向量&矩阵&因子&数据框
R语言基础:数组和列表 数组(array) 一维数据是向量,二维数据是矩阵,数组是向量和矩阵的直接推广,是由三维或三维以上的数据构成的. 数组函数是array(),语法是:array(dadta, d ...
随机推荐
- XPROG-m编程器
XPROG-m编程器是为取代较早版本的XPROG编程器而设计的. XPROG-m编程器硬件完全与XPROG编程器向上兼容,还具有其它许多功能. 该XPROG - M支持摩托罗拉68HC05,68HC0 ...
- Short Circuit Protection Circuit
http://www.daycounter.com/Circuits/Short-Circuit-Protection/Short-Circuit-Protection.phtml Short cir ...
- shell中的括号(小括号,中括号,大括号)及单引号、 双引号,反引号(``)
一.小括号,园括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号, ...
- 【BZOJ】【1251】序列终结者
Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...
- 【BZOJ】【3668】【NOI2014】起床困难综合症
贪心 位运算的题……基本都是按位来做的?... 从高位到低位,贪心来搞就可以了…… 这都算不上是数位DP吧= = /****************************************** ...
- html嵌套iframe怎样实现等iframe页面载入完进行下一步调用
</pre>假设想在你的html里面显示一张图片.或者显示一个报表,常常会在里面嵌套iframe,当我们点查询报表时.在报表显示过程中,我们想做个遮罩层,提示等待...可是报表显示出来后. ...
- ASP.NET—015:ASP.NET中无刷新页面实现
原文作者:杨友山 原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/39679823 前面也说过在asp.net中前后前交互的 ...
- MySQL命令行查询结果中文显示乱码
数据库编码格式为utf8,表和字段也都是utf8,存进去的格式是utf-8 但是用命令行工具查询命令select * from 表名; 查询出来的中文是乱码 原因:MySQL客户端根本就不能以utf8 ...
- Java JDBC数据库链接
好久没有编写有关数据库应用程序啦,这里回顾一下java JDBC. 1.使用Java JDBC操作数据库一般需要6步: (1)建立JDBC桥接器,加载数据库驱动: (2)连接数据库,获得Connect ...
- Decorator Wrapper 装饰模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...