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. IE6、7下块级元素设置display:inline-block不换行的解决办法

    使用背景 在实际的工作中,我们有的时候会把块元素设置为inline-block,这样做的目的有2个,一是块元素能够排列到一行,二是块元素就形成包裹性,能够自适应content area,而不必设置宽和 ...

  2. hibernate课程 初探单表映射3-1 单一主键

    本节简介: 1 单一主键的两种赋值方式:手动赋值(assigned)和自动赋值(native) 2 mysql和oracle赋值的不同形式 3 demo 2 native由底层数据库生成标识符,如果是 ...

  3. vue实现pdf导出,解决生成canvas模糊等问题

    最近公司项目需要,利用vue实现pdf导出,从而保存到本地打印出来,说起来好像也很容易,具体要怎么实现呢? 1 .我们要添加两个模块 第一个.将页面html转换成图片 npm install --sa ...

  4. 记秋招第一个offer:去哪儿

    9月17日 网申去哪儿,没有内推,因为网申了就不能内推了.难受,内推可以免简历筛选的,这下好了,可能简历直接挂了.我怎么犯了这么低级的错误?还没去搞清楚能不能内推就先傻乎乎地网申了. 9月28日 晚上 ...

  5. base、self标签

    以新的窗口打开页面 self在自己的窗口打开

  6. Eucalyptus4.0 管理页面介绍

    桉树配置好之后,我们可以通过web方式登陆桉树管理页面,实现对实例的启动,新建.删除,以及相关的配置操作.这里我们使用的是内网地址:http://192.168.20.60:8888/, 用户名密码默 ...

  7. IOS plist的数据 存 取(沙河目录)

    应用沙盒目录的常见获取方式 沙盒根目录:NSString *home = NSHomeDirectory(); Documents:(2种方式) 1.利用沙盒根目录拼接”Documents”字符串 N ...

  8. 【洛谷3232】[HNOI2013] 游走(贪心+高斯消元)

    点此看题面 大致题意: 一个无向连通图,小\(Z\)从\(1\)号顶点出发,每次随机选择某条边走到下一个顶点,并将\(ans\)加上这条边的编号,走到\(N\)号顶点时结束.请你对边进行编号,使总分期 ...

  9. 六、react添加多个className报错解决方法

    例如<div className={style.calss1,style.class2}></div> 该方法会报错 想得到最终渲染的结果:<div class='cla ...

  10. kernel

    http://sebastianraschka.com/Articles/2014_kernel_pca.html