欢迎访问我的新博客:http://www.milkcu.com/blog/

原文地址:http://www.milkcu.com/blog/archives/uva10042.html

原创:Smith Numbers - PC110706

作者:MilkCu

题目描述

Smith Numbers

 

While skimming his phone directory in 1982, mathematician Albert Wilansky noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the
digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:

4937775 = 3 . 5 . 5 . 65837

The sum of all digits of the telephone number is 4 + 9 + 3 + 7 + 7 + 7 + 5 = 42, and the sum of the digits of its prime factors is equally 3
+ 5 + 5 + 6 + 5 + 8 + 3 + 7 = 42. Wilansky named this type of number after his brother-in-law: the Smith numbers.

As this property is true for every prime number, Wilansky excluded them from the definition. Other Smith numbers include 6,036 and 9,985.

Wilansky was not able to find a Smith number which was larger than the telephone number of his brother-in-law. Can you help him out?

Input

The input consists of several test cases, the number of which you are given in the first line of the input. Each test case consists of one line containing a single positive integer smaller than 109.

Output

For every input value n, compute the smallest Smith number
which is larger than n and print it on a single line. You can assume that such a number exists.

Sample Input

1
4937774

Sample Output

4937775

解题思路

如何找出素因子呢?枚举法。



那每个整数的素因子是否唯一呢?

由算术基本定理可得,每个整数表示成素数乘积的方式只有一种。



Smith数肯定是合数,且满足各个数字之和等于所有素因子的每个数字之和。

注意,素因子中可能出现两个相同的数字。



那样就可以按部就班的做,从给定的数开始遍历,找到满足的数就退出循环。



为什么会超时呢?构建一个装有素数的容器。



为什么答案错误呢?

注意:若临时变量tc不为1,则说明它超出了sqrt(1e9)的范围,但它是质数,仍是该整数的质因子。

代码实现

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
int isPrime(int x) {
if(x == 2) {
return 1;
}
int s = ceil(sqrt(x));
for(int i = 2; i <= s; i++) {
if(x % i == 0) {
return 0;
}
}
return 1;
}
void getPrime(void) {
int s = ceil(sqrt(1e9));
for(int i = 2; i <= s; i++) {
if(isPrime(i)) {
v.push_back(i);
}
}
}
int calc(int x) {
int sum = 0;
while(x) {
sum += x % 10;
x /= 10;
}
return sum;
}
int smith(int n) {
int current = n + 1;
while(1) {
//if(find(v.begin(), v.end(), current) != v.end()) {
if(isPrime(current)) {
//zhishu
current++;
continue;
}
int csum = calc(current);
int tc = current;
int tsum = 0;
for(int i = 0; i < v.size(); i++) {
while(tc % v[i] == 0) {
//cout << tc << " " << v[i] << endl;
tc /= v[i];
tsum += calc(v[i]);
}
}
if(tc != 1) {
tsum += calc(tc); //注意!!
}
//cout << current << " " << csum << " " << tsum << endl;
if(tsum == csum) {
return current;
}
//break;
current++;
}
}
void print(int x) {
cout << x << " ";
}
int main(void) {
//cout << isPrime(4937775) << endl;
//cout << calc(4937775) << endl;
int m;
getPrime();
//for_each(v.begin(), v.end(), print);
//cout << endl;
cin >> m;
while(m--) {
int n;
cin >> n;
cout << smith(n) << endl;
}
return 0;
}

(全文完)

本文地址:http://blog.csdn.net/milkcu/article/details/23607205

Smith Numbers - PC110706的更多相关文章

  1. POJ 1142 Smith Numbers(史密斯数)

    Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...

  2. poj 1142 Smith Numbers

    Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...

  3. Smith Numbers POJ - 1142 (暴力+分治)

    题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...

  4. POJ 1142:Smith Numbers(分解质因数)

                                   Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  5. poj1142 Smith Numbers

    Poj1142 Smith Numbers Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13854 ...

  6. UVA 10042 Smith Numbers(数论)

    Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...

  7. A - Smith Numbers POJ

    While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...

  8. Smith Numbers(分解质因数)

    Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14173   Accepted: 4838 De ...

  9. hdu 1333 Smith Numbers

    刚开始没看清题意,要找的数一定要是素数 ;}

随机推荐

  1. Android开发模板------自己定义SimpleCursorAdapter的使用

    使用SimpleCursorAdapter所设计的table(数据表)一定要有_id字段名称,否则会出现"找不到_id"的错误 SimpleCursorAdapter直接使用的方法 ...

  2. mysql 在创建批处理脚本日志表信息

    mysql在批处理脚本通过存储过程如下所示创建日志信息表: drop PROCEDURE if EXISTS reqSp; DELIMITER // create procedure reqSp(sT ...

  3. Unix / 类 Unix shell 中有哪些很酷很冷门很少用很有用的命令?(转)

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:孙立伟 链接:http://www.zhihu.com/question/20140085/answer/14107336 ...

  4. C#关于HttpClient的统一配置(一)

    public class BaseHttpClient { protected string contentType; public BaseHttpClient() { this.contentTy ...

  5. crawler_爬虫开发的准备工作【工具】

    俗话说工欲善其事必先利其器,做java网络爬虫开发分析网页的分析工具,抓包工具比不可少,一下是个人常用的几个工具. 1.firefox低版本是为了支持httpwather , ie各个版本都支持htt ...

  6. C#将Excel数据导入数据库(MySQL或Sql Server)

    最近一直很忙,很久没写博客了.今天给大家讲解一下如何用C#将Excel数据导入Excel,同时在文章最后附上如何用sqlserver和mysql工具导入数据. 导入过程大致分为两步: 1.将excel ...

  7. js 正则练习之语法高亮

    原文:js 正则练习之语法高亮 学了几天正则,差不多该总结整理写成果了,之前就想写语法高亮匹配来着,不过水平不够,看着例子都不理解.今天就分析下 次碳酸钴 和 Barret Lee 语法高亮实现. 先 ...

  8. java编程接口(5) ------ button和button组

    这篇文章是由自己的学习笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 了解了布局管理器和Swing事件模型,那么剩下的就是Swing 的各个组件了 ...

  9. PHP的垃圾回收机制详解

    原文:PHP的垃圾回收机制详解 最近由于使用php编写了一个脚本,模拟实现了一个守护进程,因此需要深入理解php中的垃圾回收机制.本文参考了PHP手册. 在理解PHP垃圾回收机制(GC)之前,先了解一 ...

  10. 【转】height,posHeight和pixelHeight区别

    height 返回高度    带单位 posHeight    不带单位的数字 pixelHeight 转换成像素后的数字    不带单位 更多资料 http://www.1fan.com.cn