POJ 1142 Smith Numbers(分治法+质因数分解)
http://poj.org/problem?id=1142
题意:
给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加。
思路:
直接暴力。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; int n; int cacl(int x)
{
int sum = ;
while (x)
{
sum += x % ;
x /= ;
}
return sum;
} bool isprime(int x)
{
int m = sqrt(x + 0.5);
for (int i = ; i <= m; i++)
{
if (x%i == ) return false;
}
return true;
} int solve(int x)
{
if (isprime(x))
return cacl(x);
else
{
int m = sqrt(x + 0.5);
for (int i = m; i >;i--)
if (x%i == )
return solve(i) + solve(x / i);
}
} int main()
{
//freopen("D:\\input.txt", "r", stdin);
while (~scanf("%d", &n) && n)
{
for (int i = n + ;; i++)
{
if (!isprime(i) && cacl(i) == solve(i))
{
printf("%d\n", i);
break;
}
}
}
return ;
}
POJ 1142 Smith Numbers(分治法+质因数分解)的更多相关文章
- POJ 1142 Smith Numbers(史密斯数)
Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...
- poj 1142 Smith Numbers
Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh U ...
- POJ 1845 Sumdiv#质因数分解+二分
题目链接:http://poj.org/problem?id=1845 关于质因数分解,模板见:http://www.cnblogs.com/atmacmer/p/5285810.html 二分法思想 ...
- POJ 1142:Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- Smith Numbers POJ - 1142 (暴力+分治)
题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...
- Smith Numbers(分解质因数)
Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14173 Accepted: 4838 De ...
- POJ 2429 long long 质因数分解
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16206 Accepted: ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- Poj 1401 Factorial(计算N!尾数0的个数——质因数分解)
一.Description The most important part of a GSM network is so called Base Transceiver Station (BTS). ...
随机推荐
- 二、微信小游戏开发 多线程Worker
微信多线程Worker教程 微信多线程Worker API 一.创建Worker,并和当前线程通讯 多线程worker只能创建1个.能和当前线程互传数据. 创建worker 在微信开发者工具中,在当前 ...
- 聊聊对APM的理解
本文主要从以下几个列举对APM的认识: -什么是APM工具 -为什么要用APM工具,APM工具的价值在哪里: -什么样的APM工具适合于传统金融业: -如何用好APM工具: -精准告警 - ...
- 安装 sql server 2008出现重启电脑,另在server 2012 r2安装sql server 2008 安装不上
时即使是进行电脑重启,也会报这个错误,那么就不是电脑的问题了,其实是系统注册表在作怪,解决方法如下: 1.开始-->运行,输入regedit,打开注册表管理器: 2. 找到 HKEY_LOCAL ...
- Composer 添加 Laravel-china 的国内源
不知道由于什么原因,原来的 Composer 的国内镜像 https://pkg.phpcomposer.com/ 不能正常使用,经常连不上. 找了半天,发现还有一个 laravel-china 的国 ...
- tomcat启动时常见错误问题集锦
1:环境变量 问题:The JAVA_HOME environment variable is not defined This environment variable is needed to r ...
- js判断浏览器是否安装Flash插件,并提示安装或开启
var flashChecker = function() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(do ...
- pta 习题集 5-17九宫格输入法
假设有九宫格输入法键盘布局如下: [ 1,.?! ] [ 2ABC ] [ 3DEF ] [ 4GHI ] [ 5JKL ] [ 6MNO ] [ 7PQRS ] [ 8TUV ] [ 9WXYZ ] ...
- linux 分卷压缩和合并
压缩: 可以用任何方式压缩,如tar -czf 分卷: split [OPTION]... [INPUT [PREFIX]] -b 代表分卷大小, 后面可以加单位,如G,M,K. 如果不 ...
- 知乎live 我的读书经验 总结
https://www.zhihu.com/lives/757587093366009856/messages 碎片化阅读没有意义, 捡硬币捡成富翁 kindle不能全文检索 短篇文章的阅读是否有 ...
- 如何修改git分支名名称
1. 修改本地git分支名称指令 git branch -m oldBranchName newBranchName 2. 修改远程仓库(github)上的分支名称 git本地分支名已修改,只需推送到 ...