(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:
40
20
10
5
16
8
4
2
1It 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开始,我们得到以下序列:
40
20
10
5
16
8
4
2
1可以看出这个以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 ...
随机推荐
- IOS 学习笔记(6) 控件 文本域(UITextField)的使用方法
UITextField控件的诸多特性都和UITextView相似,比如成为输入文本焦点时键盘自动显示,支持长按弹出动作选项,能够接收输入事件(开始输入,修改内容,结束输入和点击回车等). 1.特有的特 ...
- IOS 学习笔记(2) 视图UINavigationController
1.栈 导航控制器自身有一个针对显示内容的栈,也有一个对于导航栏的栈,当有新的内容欲显示时,进的导航栏和显示内容会被压入此栈,这样原本显示中的导航栏和显示内容则会进入到栈的更深一层中,根据栈的先进后出 ...
- 转:js不同类型对象的比较规则
Type(x) Type(y) Result type(x)==type(y) x===y otherwise... false null undefined true undefined null ...
- MVC 优缺点
MVC是一个架构,或者说是一个设计模式,它就是强制性使应用程序的输入,处理和输出分开.将一个应用程序分为三个部分:Model,View,Controller. 1. MVC的优点 (1) 可以为一个模 ...
- rpc的学习
rpc(Remote process call 即远程过程调用)是一种请求-相应的协议, 主要使用于C/S架构中,使得分布式系统成为可能.由客户端发起请求,服务端调用各种参数处理请求,当服务器在处理请 ...
- ZYB's Game(博弈)
ZYB's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- B - 楼下水题(扩展欧几里德)
B - 楼下水题 Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- Android Blur效果之FastBlur
Blur 自从iOS系统引入了Blur效果,也就是所谓的毛玻璃.模糊化效果,磨砂效果,各大系统就開始竞相模仿,这是一个怎样的效果呢,我们现来看一些图: 这些就是典型的Blur效果,在iOS和MIUI中 ...
- mysql基本总结
创建数据库 creat table test( #整数通常使用int test_id int, #小数通常使用decimal test_price decimal, #普通文本通常使用,并使用Defa ...
- Android JNI入门第一篇——HelloJni
android支持使用NDK开发C程序,关于配置NDK环境问题应该不用再赘述了,这个网上有很多,这里通过一篇实例来讲述简单的JNI开发,大家可以参考这篇文章(Get Your Eclipse-Inte ...