欢迎访问我的新博客:http://www.milkcu.com/blog/

原文地址:http://www.milkcu.com/blog/archives/uva10042.html

原创:Smith Numbers - PC110706

作者:MilkCu

题目描述

Smith Numbers

 

While skimming his phone directory in 1982, mathematician Albert Wilansky 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 named this type of number after his brother-in-law: the Smith numbers.

As this property is true for every prime number, Wilansky excluded them from the definition. Other Smith numbers include 6,036 and 9,985.

Wilansky was not able to find a Smith number which was larger than the telephone number of his brother-in-law. Can you help him out?

Input

The input consists of several test cases, the number of which you are given in the first line of the input. Each test case consists of one line containing a single positive integer smaller than 109.

Output

For every input value n, compute the smallest Smith number
which is larger than n and print it on a single line. You can assume that such a number exists.

Sample Input

1
4937774

Sample Output

4937775

解题思路

如何找出素因子呢?枚举法。



那每个整数的素因子是否唯一呢?

由算术基本定理可得,每个整数表示成素数乘积的方式只有一种。



Smith数肯定是合数,且满足各个数字之和等于所有素因子的每个数字之和。

注意,素因子中可能出现两个相同的数字。



那样就可以按部就班的做,从给定的数开始遍历,找到满足的数就退出循环。



为什么会超时呢?构建一个装有素数的容器。



为什么答案错误呢?

注意:若临时变量tc不为1,则说明它超出了sqrt(1e9)的范围,但它是质数,仍是该整数的质因子。

代码实现

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
int isPrime(int x) {
if(x == 2) {
return 1;
}
int s = ceil(sqrt(x));
for(int i = 2; i <= s; i++) {
if(x % i == 0) {
return 0;
}
}
return 1;
}
void getPrime(void) {
int s = ceil(sqrt(1e9));
for(int i = 2; i <= s; i++) {
if(isPrime(i)) {
v.push_back(i);
}
}
}
int calc(int x) {
int sum = 0;
while(x) {
sum += x % 10;
x /= 10;
}
return sum;
}
int smith(int n) {
int current = n + 1;
while(1) {
//if(find(v.begin(), v.end(), current) != v.end()) {
if(isPrime(current)) {
//zhishu
current++;
continue;
}
int csum = calc(current);
int tc = current;
int tsum = 0;
for(int i = 0; i < v.size(); i++) {
while(tc % v[i] == 0) {
//cout << tc << " " << v[i] << endl;
tc /= v[i];
tsum += calc(v[i]);
}
}
if(tc != 1) {
tsum += calc(tc); //注意!!
}
//cout << current << " " << csum << " " << tsum << endl;
if(tsum == csum) {
return current;
}
//break;
current++;
}
}
void print(int x) {
cout << x << " ";
}
int main(void) {
//cout << isPrime(4937775) << endl;
//cout << calc(4937775) << endl;
int m;
getPrime();
//for_each(v.begin(), v.end(), print);
//cout << endl;
cin >> m;
while(m--) {
int n;
cin >> n;
cout << smith(n) << endl;
}
return 0;
}

(全文完)

本文地址:http://blog.csdn.net/milkcu/article/details/23607205

Smith Numbers - PC110706的更多相关文章

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

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

  2. poj 1142 Smith Numbers

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

  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. poj1142 Smith Numbers

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

  6. UVA 10042 Smith Numbers(数论)

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

  7. A - Smith Numbers POJ

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

  8. Smith Numbers(分解质因数)

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

  9. hdu 1333 Smith Numbers

    刚开始没看清题意,要找的数一定要是素数 ;}

随机推荐

  1. android浏览器开发小技巧集锦(转)

    本人和朋友们做了一段时间浏览器,将一些小技巧分享出来,先写一部分,慢慢写,同时也为我们的浏览器打打广告 我们的浏览器将要上线,名叫沙发浏览 1.网页内的右键菜单 public boolean onLo ...

  2. 我的MYSQL学习心得(二)

    原文:我的MYSQL学习心得(二) 我的MYSQL学习心得(二) 我的MYSQL学习心得(一) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL学习心得(五) 我的MYSQL ...

  3. 交易应用-运行多个SQL声明

    事务具有原子性.要么不运行.要么全运行.一旦成功运行永久保存.而这些正是因为事务的原子性和对数据库的持久性形成的.下面是一个关于统一给数据库中的数据改动的批量操作,利用到事务. TODO:批量改动数据 ...

  4. sql server 更新表,每天的数据分固定批次设置批次号sql

    按表中的字段 UpdateTime 按每天进行编号,每天的编号都从1开始编号,并附带表的主键 cid,把数据存入临时表中 WITH temp AS (SELECT cid,updatetime, RO ...

  5. 经典算法题每日演练——第十四题 Prim算法

    原文:经典算法题每日演练--第十四题 Prim算法 图论在数据结构中是非常有趣而复杂的,作为web码农的我,在实际开发中一直没有找到它的使用场景,不像树那样的频繁使用,不过还是准备 仔细的把图论全部过 ...

  6. crawler_httpclient代理访问

    public String getDocumentByProxy(String url) throws ClientProtocolException, IOException { DefaultHt ...

  7. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  8. 用css2属性clip实现网页进度条

    前言 看了网上一些关于网页进度条样式的资料,有很多方式实现,针对其展现形式,有用图片的,有用css2属性clip,有用flash的,本人就学会了一种,下面就简单来介绍一下. css2的属性clip 如 ...

  9. hdu 3874

    求一个序列中全部数字的和,当中数值同样的仅仅能计算一次. 先储存全部的请求,然后依照它们的右边界排序,在查询的同一时候更新区间.这里事实上有一点点DP的味道,在它进行某个查询之前,保证全部的反复数字( ...

  10. zsh的安装与配置

    参考: http://cnbin.github.io/blog/2015/06/01/mac-zsh-an-zhuang-he-shi-yong/ http://www.cnblogs.com/ios ...