Description

题目描述

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University, 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 was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.

As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.

Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036.However, Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!

阿尔伯特·威兰斯基是一位理海大学的数学家,在1982年浏览他自己的电话薄时,注意到他的表兄弟(Harold Smith)H. Smith的电话号码有有如下特点:各位上的数字相加等于分解质因数后各位上的数字相加。懂否?史密斯的电话号码是493-7775。这个数字可被分解质因数致如下形式:

4937775= 3*5*5*65837

这个电话号码各位数字的和是4+9+3+7+7+7+5= 42,并且与分解质因数后各位数字的和相等3+5+5+6+5+8+3+7=42。威兰斯基感觉很神奇就以他的表兄弟命名:史密斯数。

不过这个性质对每个质数都成立,因此威兰斯基后来把质数(分解不能)从史密斯数的定义中剔除了。

威兰斯基在the Two Year College Mathematics Journal发表了关于史密斯数的论文并且列出了一整套史密斯数:举个栗子,9985是史密斯数,6036也是。但是威兰斯基没能找到比他表兄弟电话号码4937775更大的史密斯数,你可以当条红领巾!

Input

输入

The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.

输入文件由一列正整数组成,每行一个整数。每个整数最多8位。数字0表示输入结束。

Output

输出

For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n, and print it on a line by itself. You can assume that such a number exists.

对于每个n>0的输入,你要算出大于n的最小史密斯数,输出一行。你可以认为结果是存在的。

Sample Input - 输入样例

Sample Output - 输出样例

4937774

0

4937775

【题解】

  首先,这道题是水题,不然就会和某个人一样觉得要用Pollard's rho算法……

  注意几点就可以了:

  ①可以暴力。②素数不是史密斯数。③从n+1开始找。

  ④题目描述和输入输出分开看,并不是要你找4937775后的史密斯数。

【代码 C++】

 #include<cstdio>
#include<cstring>
#include<cmath>
int prime[];
void rdy(){
bool temp[];
memset(temp, , sizeof(temp));
prime[] = ;
int i = , j, pi = ;
for (i = ; i < ; i += ){
if (temp[i]) continue;
else{
for (j = i << ; j < ; j += i) temp[j] = ;
prime[++pi] = i;
}
}
prime[] = ;
}
int digitSum(int now){
int sum = ;
while (now) sum += now % , now /= ;
return sum;
}
int find(int now){
int i = , ed = sqrtf(now) + 0.5;
if (ed > ) ed = ;
for (; prime[i] <= ed; ++i){
if (now%prime[i] == ) return prime[i];
}
return ;
}
int change(int now){
int sum = , temp, stp = ;
while (now > ){
temp = find(now);
if (temp) sum += digitSum(temp), now /= temp, ++stp;
else sum += digitSum(now), now = ;
}
if (stp) return sum;
return ;
}
int main(){
rdy();
int n;
while (scanf("%d", &n)){
if (n++){
while (digitSum(n) != change(n)) ++n;
printf("%d\n", n);
}
else break;
}
return ;
}

POJ 1142

POJ 1142 Smith Numbers(史密斯数)的更多相关文章

  1. poj 1142 Smith Numbers

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

  2. POJ 1142 Smith Numbers(分治法+质因数分解)

    http://poj.org/problem?id=1142 题意: 给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加. 思路:直接暴力. #include <ios ...

  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. Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)

    一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...

  6. A - Smith Numbers POJ

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

  7. Smith Numbers - PC110706

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10042.html 原创:Smit ...

  8. UVA 10042 Smith Numbers(数论)

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

  9. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

随机推荐

  1. Linux Centos 上一些常用的命令

    1.查看端口被哪个进程占用 netstat -lnp | grep <端口号> 2.查看某个进程号详细信息 ps <进程号> 3.检查指定服务是否开启(例如 telnet) c ...

  2. linux设备驱动归纳总结(七):1.时间管理与内核延时【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxx ...

  3. Java中删除指定文件夹文件夹下面有内容也删除使用递归方案

    import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...

  4. Class.forName("ClassName")与ClassName.class的区别

    引发问题的来源:最近在看比较深入的JVM相关的书,不得不感慨,JVM确实是比较深奥,很多地方难以理解不说,在网上还找不到什么资料,发现一个左思右想都想不明白的问题上网来搜索,结果基本上都是从书上cop ...

  5. 探秘腾讯Android手机游戏平台之不安装游戏APK直接启动法

    前言相信这样一个问题,大家都不会陌生,“有什么的方法可以使Android的程序APK不用安装,而能够直接启动”.发现最后的结局都是不能实现这个美好的愿望,而腾讯Android手机游戏平台却又能实现这个 ...

  6. ecshop后台增加模块菜单详细教程(图)

    我们有时候针对ecshop如此开发,想在后台加一些菜单,最模板以前提供过教程,但是并非很系统,今天最模板抛砖引玉图文教程告诉大家:如何在ecshop后台增加模块菜单! 首先需要修改四个文件:inc_p ...

  7. hdwiki中插件开发指南

    插件就是为了满足个性化需求按照HDWiki插件开发规范编写的可插拔程序,虽然可以直接对HDWiki进行二次开发实现插件同样的功能,但是这样做势必影响到系统的升级和稳定性. 采用插件的方式,可以随时进行 ...

  8. java 错误集锦

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: at com.niuniu. ...

  9. Radar Installation 分类: POJ 2015-06-15 19:54 8人阅读 评论(0) 收藏

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 60120   Accepted: 13 ...

  10. Unix下五种IO模型

    http://blog.chinaunix.net/uid-25324849-id-247813.html 1. I/O模型 Unix下共有五种I/O模型 a. 阻塞I/O b. 非阻塞I/O c. ...