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
题目大意:
给你一个数,求大于这个数字并满足以下条件的最小值:
    条件:数字的各个位置加起来与用质数拆分该数字后得到的数字的各个位置之和相等 4937775= 3*5*5*65837
暴力模拟就可以啦 首先要知道质数拆分,然后将得到的每个数字的各个位置相加相等。如果与原数字相等的话说明找到啦!
#include<iostream>
#include<cstdio>
using namespace std; int check(int x){//由于数字范围太大,不能打表,只能这样一步一步来
for(int i=;i*i<=x;i++){
if(x%i==) return ;
}
return ;
} int f2(int x){
int sum=;
while(x){
sum+=x%;
x=x/;
}
return sum;
}
int f(int x){
int sum=;
for(int i=;i*i<=x;i++){//拆分
if(x%i==){
int ans=;
if(i<)
{
while(x%i==){
ans++;
x=x/i;
}
sum+=i*ans;
}
else {
int s=f2(i);
while(x%i==){
ans++;
x=x/i;
}
sum+=s*ans;
}
}
}
if(x>) sum+=f2(x);
return sum;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF&&n){
for(int i=n+;;i++){
if(check(i)==){
if(f2(i)==f(i)){
printf("%d\n",i);
break;
} }
}
}
return ;
}

A - Smith Numbers POJ的更多相关文章

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

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

  2. Smith Numbers POJ - 1142 暴力递归枚举

    题意: 给你一个数x,把这个分解成素数之积(假设是x1*x2*x3),如果   x的每一数位的和   等于  x1每一数位的和加上x2每一数位的和加上x3每一数位的和,那么他就是题目要找的数 示例: ...

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

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

  4. poj 1142 Smith Numbers

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

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

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

  6. Smith Numbers - PC110706

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

  7. poj1142 Smith Numbers

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

  8. UVA 10042 Smith Numbers(数论)

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

  9. Smith Numbers(分解质因数)

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

随机推荐

  1. [模拟,英语阅读] Codeforces 549D Haar Features

    题目:https://codeforces.com/contest/549/problem/D D. Haar Features time limit per test 1 second memory ...

  2. Hive视图如何创建、特点及应用场景

    Hive视图特点 View是逻辑存在,Hive暂不支持物化视图(1.0.3) View只读,不支持LOAD/INSERT/ALTER.需要改变View定义,可以是用Alter View View内可能 ...

  3. mysql打开general log的办法

    mysql打开general log的办法   mysql打开general log之后,所有的查询语句都可以在general log 文件中以可读的方式得到,但是这样general log文件会非常 ...

  4. React入门(1)

    今天继续来学习react 首先,先写几个小demo来感受一下什么是react,以及react的语法规则,来建立对react的一个总体认识 上demo: demo01: demo01涉及的知识点有: 1 ...

  5. Java中如何调用静态方法

    Java中如何调用静态方法: 1.如果想要调用的静态方法在本类中,可直接使用方法名调用 2.调用其他类的静态方法,可使用类名.方法名调用 关于静态方法能被什么调用 1.实例方法 2.静态发放

  6. 剑指offer刷题总结

    ★ 二维数组的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否 ...

  7. SpringBoot中常见的错误

    数据源配置问题 原因 spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类, ...

  8. java web综合案例

    1.采用的技术: bootstrap+jsp+servlet+三层架构(servlet,service,dao)+mysql 注意:mysql使用的是5.5版本,使用高版本会有很多问题.可以将5.5版 ...

  9. RecyclerView 的 Item 的单击事件

    RecyclerView 的每个Item的点击事件并没有像 ListView 一样封装在组件中,需要 Item 的单击事件时就需要自己去实现,在 Adapter 中为RecyclerView 添加单击 ...

  10. Node教程——Node+MongoDB案例实现用户信息的增删改查

    想要获取源代码的同学可以留言,我不做git上传了,案例太简单 没必要 综合演练 用户信息的增删改查 需求:你需要实现这样的结果 点击添加可以添加用户,点击删除可以删除点击修改可以修改 代码分析: 1. ...