(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 + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
题目大意:
以下迭代序列定义在整数集合上:
n n/2 (当n是偶数时) n 3n + 1 (当n是奇数时)
应用以上规则,并且以数字13开始,我们得到以下序列:
可以看出这个以13开始以1结束的序列包含10个项。虽然还没有被证明(Collatz问题),但是人们认为在这个规则下,以任何数字开始都会以1结束。
以哪个不超过100万的数字开始,能给得到最长的序列? 注意: 一旦序列开始之后,也就是从第二项开始,项是可以超过100万的。
方法1:
#include<stdio.h>
#include<math.h>
#include<stdbool.h> int powcount(long long n) //计算2的幂数
{
int count=;
while(n>>=) count++;
return count;
} bool ispower(long long v) //判断n是否为2的幂
{
if(((v & (v - )) == )) return true;
else return false;
} int length(long long n)
{
int sum=;
while()
{
if(n==) break;
if((n & )==)
{
if(ispower(n)) return sum+powcount(n);
else n=n/;
}
else n=*n+;
sum++;
}
return sum;
} int main()
{
int i,t,k,max=;
for(i=; i<; i++)
{
t=length(i);
if(t>max)
{
max=t;
k=i;
}
}
printf("%lld\n",k);
return ;
}
方法2:
#include<stdio.h>
#include<math.h>
#include<stdbool.h> int a[]; void find()
{
long long i,j,k,f,sum,max=;
a[]=,a[]=;
for(j=; j<; j++)
{
sum=,k=i=j;
while()
{
if((i & )==)
{
i=i/;
if(i<k)
{
a[k]=sum+a[i];
break;
}
}
else
{
i=*i+;
}
sum++;
}
if(a[k]>max)
{
max=a[k];
f=k;
}
}
printf("%d\n",f);
} int main()
{
find();
return ;
}
Answer:
|
837799 |
(Problem 14)Longest Collatz sequence的更多相关文章
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- (Problem 42)Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- (Problem 74)Digit factorial chains
The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- (Problem 47)Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 7 15 = 3 5 The fi ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
- (Problem 28)Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
随机推荐
- codeforces 451E. Devu and Flowers 容斥原理+lucas
题目链接 给n个盒子, 每个盒子里面有f[i]个小球, 然后一共可以取sum个小球.问有多少种取法, 同一个盒子里的小球相同, 不同盒子的不同. 首先我们知道, n个盒子放sum个小球的方式一共有C( ...
- cocos2dx ResolutionPolicy
FrameSize 参数,在游戏运行时,我们可以通过 CCEGLView::sharedOpenGLView()->getFrameSize();如果在手机上运行,那么不同分辨率将会得到不同的值 ...
- poj 3128 Leonardo's Notebook(置换的幂)
http://poj.org/problem?id=3128 大致题意:输入一串含26个大写字母的字符串,能够把它看做一个置换.推断这个置换是否是某个置换的平方. 思路:具体解释可參考url=ihxG ...
- BZOJ3231(矩阵连乘,稍有点复杂)
题目:3231: [Sdoi2008]递归数列 题意: 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 ...
- asp.net MVC 使用JQuery.Ajax
使用到:Jquery.js 以及 Newtonsoft.Json.dll 客户端调用方式: $("#ButAjax").click(function() {$.ajax({type ...
- C++基础梳理--Class、Struct、Union
C++学习一段时间后,反过头来看我发现我忘了一下最基础的东西:strcut(结构体),union(联合体)我学会了类的一堆东西却忘了这两个最基础的: 现在就好好的重新学习一下这里的东西: 一.Clas ...
- SPOJ8222 Substrings( 后缀自动机 + dp )
题目大意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.F(1)..F(Length(S)) 建出SAM, 然后求出Right, 求Right可以按拓扑序dp..Right ...
- Eclipse环境问题集合
1. XDoclet错误:Missing library: xdoclet-1.2.1.jar 从http://sourceforge.net/projects/xdoclet/files/xdocl ...
- Linux常用解压文件
tar.gz tar -zxvf filename.tar.gz tar.bz2 tar -vxjf filename.tar.bz2
- ubuntu ~/.bash_history
sudo apt-get update sudo apt-get install python-pip sudo pip install Django==1.7.1 sudo apt-get inst ...