poj1142 Smith Numbers
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的更多相关文章
- POJ1142 Smith Numbers 暴力+分解质因子
题意:题目定义了一个史密斯数,这个数的定义是:一个合数的各个位置上加起来的和等于它的素因数所有位置上的数字加起来的和.比如: 4937775=3∗5∗5∗658374+9+3+7+7+7+5=3+5+ ...
- poj1142.Smith Number(数学推导)
Smith Number Time Limit: 1 Sec Memory Limit: 64 MB Submit: 825 Solved: 366 Description While skimm ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- Smith Numbers - PC110706
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10042.html 原创:Smit ...
- poj 1142 Smith Numbers
Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...
- Smith Numbers POJ - 1142 (暴力+分治)
题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...
- POJ 1142:Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- UVA 10042 Smith Numbers(数论)
Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...
- A - Smith Numbers POJ
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...
随机推荐
- SpringBoot | 第十章:Swagger2的集成和使用
前言 前一章节介绍了mybatisPlus的集成和简单使用,本章节开始接着上一章节的用户表,进行Swagger2的集成.现在都奉行前后端分离开发和微服务大行其道,分微服务及前后端分离后,前后端开发的沟 ...
- personalWebsite_1:历史记录汇总
最开始,根据 https://blog.csdn.net/zbl1146556298/article/details/79714239 进行网站构思设计,根据源码, 1.把gradle项目转为mav ...
- spring batch 以游标的方式 数据库读取数据 然后写入目标数据库
前面关于Spring Batch的文章,讲述了SpringBatch对Flat.XML等文件的读写操作,本文将和大家一起讨论Spring Batch对DB的读写操作.Spring Batch对DB数据 ...
- 会话跟踪之Session
Session是服务端使用记录客户端状态的一种机制,Session使用简单,但是和Cookie相比,增加了服务器的存储压力[因为为了追求速度,服务器将Session放置在了内存中].Cookie是保存 ...
- vue+element-ui实现cookie登录
//效果 //login.vue <template> <div> <el-form :model="ruleForm" :rules="r ...
- oracle 查询之前的表数据
SELECT * FROM Student AS OF TIMESTAMP SYSDATE - 3/1440 对SQL的解释说明: SYSDATE :当前时间 1440 :24h*60m=1440m ...
- JAVA和数据库工具的下载地址(备用)
sqlite数据库驱动下载: http://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/
- intelij idea相关笔记--持续更新
一.快捷键: Ctrl+F 文件内查找 Ctrl+Shift+F 全局查找 Ctrl+Shift+N 查找文件 Ctrl+Alt+← 返回上一步 Ctrl+Alt+→ 返回下一步 二.编译相关: 如果 ...
- CentOs7 修复 引导启动
一.修复MBR: MBR(Master Boot Record主引导记录): 硬盘的0柱面.0磁头.1扇区称为主引导扇区.其中446Byte是bootloader,64Byte为Partition t ...
- 在IIS 7.5上安装WebDAV(http文件下载上传)
WebDAV 简介 WebDAV (Web-based Distributed Authoring and Versioning) 一种基于 HTTP 1.1协议的通信协议.它扩展了HTTP 1.1, ...