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 ...
随机推荐
- 【手记】解决涉及office的程序报“Unable to cast COM object of type System._ComObject...”的问题
报错内容大概像这种: 如果该电脑曾经装过WPS,那解决方法就是把WPS装回来,可以不用,但别卸,把它供着,惹不起.
- jquery只能输入数字
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Maven之setting.xml配置文件详解
setting.xml配置文件 maven的配置文件settings.xml存在于两个地方: 1.安装的地方:${M2_HOME}/conf/settings.xml 2.用户的目录:${user.h ...
- Mybatis 与hibernate
共同点 (1)Hibernate与MyBatis都是通过SessionFactoryBuider由XML配置文件生成SessionFactory,由SessionFactory 生成Session,由 ...
- blfs(systemd版本)学习笔记-配置远程访问和管理lfs系统
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 要实现远程管理和配置lfs系统需要配置以下软件包: 前几页章节脚本的配置:https://www.cnblogs.com/ren ...
- vue-cli脚手架之webpack.prod.conf.js
webpack.prod.conf.js 生产环境配置文件: 'use strict'//js严格模式执行 const path = require('path')//这个模块是发布到NPM注册中心的 ...
- CloudSim源代码学习——虚拟机(VM)
package org.cloudbus.cloudsim; import java.util.ArrayList;//This class provides methods to manipulat ...
- Android Stuido代码混淆
一.Android Studio 代码混淆基本配置首先我们要在build.gradle里设置 miifyEnabled 里改为true,表示可以混淆 proguardFiles getDefaultP ...
- Android中使用progurad混淆代码
第一步,取消project.properties中关于progurad的注释,开启progurad,默认的配置文件会被加载进来. proguard.config=${sdk.dir}/tools/pr ...
- redis介绍 (8) window 下redis的集群(cluster命令)
前言: 前段时间我在centos上搭建过一次redis集群,那是借助ruby搭建,这次我介绍一种纯redis集群命令的方式去搭建[最后我会简单介绍ruby搭建]. redis集群搭建(三主三备): 准 ...