Smith Numbers POJ - 1142 (暴力+分治)
题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意)
思路:主要是分治:分解成质因子使用递归即可。
#include<cstdio>
#include<cmath>
// 检测素数
bool is_prime(int n)
{
for (int i = ; i*i <= n;++i)
if (n%i == ){ return ; }
return ;
}
//数位
int sumfun(int n)
{
int ans = ;
while (n){ ans += n % ; ; n /= ; }
return ans;
}
//分治
int che(int n)
{
if (is_prime(n))return sumfun(n);
else
{
int m = (int)sqrt(n + 0.5);
for (int i = m; i >; --i)
{
if (n%i == )
return che(i) + che(n / i);
}
}
}
int main()
{
int n;
while (scanf("%d", &n)!=EOF && n)
{
while (n++)
{
if (!is_prime(n)&&sumfun(n) == che(n)){
printf("%d\n", n);
break;
}
}
}
}
Smith Numbers POJ - 1142 (暴力+分治)的更多相关文章
- Smith Numbers POJ - 1142 暴力递归枚举
题意: 给你一个数x,把这个分解成素数之积(假设是x1*x2*x3),如果 x的每一数位的和 等于 x1每一数位的和加上x2每一数位的和加上x3每一数位的和,那么他就是题目要找的数 示例: ...
- A - Smith Numbers POJ
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- POJ 1142:Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- poj 1142 Smith Numbers
Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...
- poj1142 Smith Numbers
Poj1142 Smith Numbers Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13854 ...
- Smith Numbers - PC110706
欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10042.html 原创:Smit ...
- POJ 2182/暴力/BIT/线段树
POJ 2182 暴力 /* 题意: 一个带有权值[1,n]的序列,给出每个数的前面比该数小的数的个数,当然比一个数前面比第一个数小的个数是0,省略不写,求真正的序列.(拗口) 首先想到的是从前到后暴 ...
- UVA 10042 Smith Numbers(数论)
Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...
随机推荐
- Mybatis缓存(1)--------系统缓存及简单配置介绍
前言 Mybatis的缓存主要有两种: 系统缓存,也就是我们一级缓存与二级缓存: 自定义的缓存,比如Redis.Enhance等,需要额外的单独配置与实现,具体日后主要学习介绍. 在这里主要记录系统缓 ...
- 牛刀小试MySQL--GTID
GTID的概念 何为GITD GTID(global transaction identifier)是全局事务标识符,在MySQL5.6版本中作为一个超级特性被推出.事务标识不仅对于Master(起源 ...
- SPI Flash(W25Q16DV) 驱动
大体上可分为以下几个部分: 1.注册设备驱动 spi_register_driver 2.分配 mtd_info 结构体 3.配置 mtd_info 结构体 4.注册 mtd_info 结构体 构建 ...
- Apollo 5 教你怎么把自己的配置放到 Spring 环境中
目录: 前言 处理方案 简单例子 前言 有的时候,你可能需要在 Spring 环境中放入一些配置,但这些配置无法写死在配置文件中,只能运行时放入.那么,这个时候该怎么办呢? Apollo 就是搞配置的 ...
- c# Newtonsoft.Json封装
public static T Deserialize<T>(string content) where T : class, new() { return JsonConvert.Des ...
- Install/Remove of the Service Denied!
在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld --install 报错: 信息如下一: Install/Remove of the Service Den ...
- 漫画 | Spring AOP的底层原理是什么?
1.Spring中配置的bean是在什么时候实例化的? 2.描述一下Spring中的IOC.AOP和DI IOC和AOP是Spring的两大核心思想 3.谈谈IOC.AOP和DI在项目开发中的应用场景 ...
- Git的概念及常用命令
一.概念 Git是一个分布式的版本控制工具,区别于集中式管理的SVN. 二.优势 每个开发者都拥有自己的本地版本库,可以在本地任意修改代码.创建分支,不会影响到其他开发者的使用: 所有版本信息均保存在 ...
- DotNetCore 结合 Nginx 将网站部署到阿里云
基础环境配置 域名和服务器请先自行购买 基于 云服务器ECS 创建一个应用实例,选择系统镜像为 Ubuntu 16.04,在本机通过 SSH 进行远程连接,并进行相关配置 ssh root@http: ...
- 初学HTML-5
表格标签:用来给一堆数据添加表格语义. 格式:<table> <tr> <td></td> </tr> </table> tab ...