Poj1142 Smith Numbers

Smith Numbers

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 13854 Accepted: 4716

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!

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.

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.

Sample Input

4937774

0

Sample Output

4937775

Source

Mid-Central European Regional Contest 2000

题意 大于n满足你的各个位数之和等于质因子各位数之和。

题解 暴力过。

#include<cstdio>
int fun(long long a)
{
int sum=0;
while(a>0)
{
sum+=a%10;
a/=10;
}
return sum;
} bool prime(long long a)
{
int flag=1;
if (a==1) return false;
if(a==2) return true;
for(int i=2;i*i<a+1;i++)
if(a%i==0)
{
flag=0;
break;
}
if(flag)
return true;
else
return false;
} int cnt(long long a)
{
if(prime(a))
return fun(a);
else
{
for(int i=2;i*i<a+1;i++)
{
if(a%i==0)
return cnt(i)+cnt(a/i);
}
}
} int main()
{
long long a;
while(scanf("%lld",&a)!=EOF&&a)
{
while(a++)
{
int sum=fun(a); if(!prime(a)&&fun(a)==cnt(a))
break; }
printf("%lld\n",a); }
return 0;
}

这时间倒也不是很多,79ms

poj1142 Smith Numbers的更多相关文章

  1. POJ1142 Smith Numbers 暴力+分解质因子

    题意:题目定义了一个史密斯数,这个数的定义是:一个合数的各个位置上加起来的和等于它的素因数所有位置上的数字加起来的和.比如: 4937775=3∗5∗5∗658374+9+3+7+7+7+5=3+5+ ...

  2. poj1142.Smith Number(数学推导)

    Smith Number Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 825  Solved: 366 Description While skimm ...

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

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

  4. Smith Numbers - PC110706

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

  5. poj 1142 Smith Numbers

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

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

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

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

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

  8. UVA 10042 Smith Numbers(数论)

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

  9. A - Smith Numbers POJ

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

随机推荐

  1. eclipse配置android开发环境并搭建第一个helloWord工程

    一.搭建Android在eclipse下环境    一.JDK(不用安装  下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...

  2. spring的工厂方法

    http://blog.csdn.net/nvd11/article/details/51542360

  3. centos6安装lnmp

    CentOS 6 默认仓库不包含nginx,我们可以手动添加nginx的仓库. 访问nginx官网获取repo文件 我们需要先访问nginx的官方网站,获取官方的仓库地址.点击这里访问nginx官方文 ...

  4. jQuery懒加载插件

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  5. EasyUI Combobox 的 onChange,onSelect,onClick 事件

    EasyUI 中 Combobox 选项发生改变时会触发 onChange,onSelect,onClick,3 个事件.最近要做一个级联的 Combo 菜单,类似于选择地址时让用户填写省,市,区的菜 ...

  6. (2017.9.27) 自定义列表项 list-style 使用心得

    今天给某公司做招聘专页.早上完成设计图,下午开始排版.页面套用了我之前做的某人才局的招聘页面,导航栏.banner 很快就出来了.这次内容里我有些地方用了列表,当然要用 <ul> < ...

  7. 零基础逆向工程24_C++_01_类_this指针_继承本质_多层继承

    1 类内的成员函数和普通函数的对比 1.1 主要是从参数传递.压栈顺序.堆栈平衡来总结. 1.参数传递:成员函数多传一个this指针 2.压栈顺序:成员函数会将this指针压栈,在函数调用取出 3.堆 ...

  8. Eucalyptus简介

    1.Eucalyptus是什么? Eucalyptus  n.桉树 Eucalyptus is a Linux-based software architecture that implements ...

  9. C#Udp组播

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  10. linux 命令——51 lsof(转)

    lsof(list open files) 是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 如传输控制 ...