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 ...
随机推荐
- [Codeforces 1016F]Road Projects
Description 题库链接 给你一棵 \(n\) 个节点的树,定义 \(1\) 到 \(n\) 的代价是 \(1\) 到 \(n\) 节点间的最短路径的长度.现在给你 \(m\) 组询问,让你添 ...
- sql多条件查询语句
如上图:三个文本可选项,那sql语句怎么写呢? 1.首先获取三个文本的值分别为Name,Age,Sex. 2.string sql="select * from 表 where 1=1&qu ...
- c# API接受图片文件以文件格式上传图片
/// 文件图片上传 /// </summary> /// <returns>成功上传返回上传后的文件名</returns> [HttpPost] public a ...
- LeetCode 任务调度器-Python3<八>
题目:https://leetcode-cn.com/problems/task-scheduler/description/ 给定一个用字符数组表示的 CPU 需要执行的任务列表.其中包含使用大写的 ...
- vb.net 日期時間
Dim dMyDate As DateTime = DateTime.Now‘當前時間日期 DateDiff("h", C13, C3)’ 二時間差 ‘日期格式 C2 = Form ...
- Java学习笔记之——循环语句
一.for循环 语法: for(变量初始化:条件判断:更新循环变量){ 循环体: } 案例: 二.while循环 语法: while(条件){ 循环体: } 如果条件为true,执行循环体,false ...
- Spring基于注解和XML混合方式的使用
首先要明白,基于注解和XML两种方式的实现功能是一样的,只是两种不同的配置方式. 一.IoC配置 1.配置xml 在使用注解与xml结合的方式配置IoC之前,首先要引入context标签: xmlns ...
- Fundebug前端JavaScript插件更新至1.2.0
摘要: Fundebug的前端JavaScript错误监控插件更新至1.2.0:支持监控WebSocket连接错误:修复了监控unhandledrejection错误的BUG,即未用catch处理的P ...
- mysql之数据备份与还原
mysql数据备份 #1. 物理备份: 直接复制数据库文件,适用于大型数据库环境.但不能恢复到异构系统中如Windows. #2. 逻辑备份: 备份的是建表.建库.插入等操作所执行SQL语句,适用于中 ...
- 洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)
题意 题目链接 Sol 首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血 发现状态数只有\(s = 1 ...