Project Euler Problem 14-Longest Collatz sequence
记忆化搜索来一发。没想到中间会爆int
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
int dp[MAXN];
int dfs(long long num)
{
if(num <= MAXN && dp[num]) return dp[num];
if(num == 1) return (dp[num] = 1);
if(num&1)
{
if(num <= MAXN) return (dp[num] = dfs(num*3+1)+1);
else return dfs(num*3+1)+1;
}
else
{
if(num <= MAXN) return (dp[num] = dfs(num/2)+1);
else return dfs(num/2)+1;
}
}
int main()
{
int len = 0,res,maxn = 0;
for(int i = 1; i <= MAXN; ++i)
{
len = dfs(i);
if(len > maxn)
{
maxn = len;
res = i;
}
}
printf("%d\n",res);
return 0;
}
Project Euler Problem 14-Longest Collatz sequence的更多相关文章
- Project Euler 14 Longest Collatz sequence
题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...
- (Problem 14)Longest Collatz sequence
The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n ...
- Project Euler Problem 10
Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of ...
- Project Euler Problem (1~10)
1.If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. Th ...
- Project Euler problem 62
题目的大意很简单 做法的话. 我们就枚举1~10000的数的立方, 然后把这个立方中的有序重新排列,生成一个字符串s, 然后对于那些符合题目要求的肯定是生成同一个字符串的. 然后就可以用map来搞了 ...
- Project Euler problem 63
这题略水啊 首先观察一下. 10 ^ x次方肯定是x + 1位的 所以底数肯定小于10的 那么我们就枚举1~9为底数 然后枚举幂级数就行了,直至不满足题目中的条件即可break cnt = 0 for ...
- Project Euler problem 61
题意很明了. 然后我大概的做法就是暴搜了 先把每个几边形数中四位数的处理出来. 然后我就DFS回溯着找就行了. 比较简单吧. #include <cstdio> #include < ...
- Project Euler problem 68
题意须要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.而且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下即可. #include <iostr ...
- Project Euler Problem 675
ORZ foreverlasting聚聚,QQ上问了他好久才会了这题(所以我们又聊了好久的Gal) 我们先来尝试推导一下\(S\)的性质,我们利用狄利克雷卷积来推: \[2^\omega=I\ast| ...
随机推荐
- JQMObile 优势
1.跨平台 目前大部分的移动设备浏览器都支持HTML5标准,jQuery Mobile以HTML5标记配置网页,所以可以跨不同的移动设备,如Apple iOS,Android,BlackBerry, ...
- 计蒜客 Zoning Houses(线段树区间最大次大)
Given a registry of all houses in your state or province, you would like to know the minimum size of ...
- iOS项目转移到自动引用计数
这里主要参考了Apple官方文档:Transitioning to ARC Release Notes 在支持iOS5的Xcode4中,创建项目会看到这样的选项: 这是iOS5的新特性,自动对象引用计 ...
- idea启动报错:Access denied for user 'root '@'192.168.100.XXX' (using password: YES)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean wit ...
- python ndarray相关操作:索引
- Linux ar命令介绍 和常用示例
制作静态库要用到ar命令,命令格式: ar [-]{dmpqrtx}[abcfilNoPsSuvV] [membername] [count] archive files... {dmpqrtx}中的 ...
- Adapter小练习
Aapter的继承关系图: Android中Adapter的是数据和视图之间的桥梁,数据在adapter中做处理,然后显示到视图上面. 一.ArrayAdapter适配器 java代码: import ...
- 二.python数据结构的性能分析
目录: 1.引言 2.列表 3.字典 一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来 ...
- material-ui里面的withStyles是什么?
export default withStyles(styles, { name: 'MuiAppBar' })(AppBar); //这里的作用是什么? withStyles 是一个 HOC 组件, ...
- python系列之(4)豆瓣图书《平凡的世界》书评及情感分析
本篇主要是通过对豆瓣图书<平凡的世界>短评进行抓取并进行分析,并用snowNLP对其进行情感分析. 用到的模块有snowNLP,是一个python库,用来进行情感分析. 1.抓取数据 我们 ...