描述

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm:

1.      input n

2.      print n

3.      if n = 1 then STOP

4.           if n is odd then n ← 3n + 1

5.           else n ← n / 2

6.      GOTO 2

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

输入

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.

输出

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

样例输入

1 10

100 200

201 210

900 1000

样例输出

1 10 20

100 200 125

201 210 89

900 1000 174

#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
int np(int x)
{
int count=1;
while(x!=1)
{
count++;
if(x%2==0) x/=2;
else x=3*x+1;
}
return count;
}
int solve(int s, int e)
{
int res=0,i,tmp;
if(s>e)
swap(s,e);
for(i=s;i<=e;++i)
{
tmp=np(i);
if(res<tmp)
res=tmp;
}
return res;
}
int main()
{
int x,y,i;
while(cin>>x>>y)
{
cout<<x<<" "<<y<<" "<<solve(x,y)<<endl;
}
return 0;
}

  

1964-NP的更多相关文章

  1. 转载 什么是P问题、NP问题和NPC问题

    原文地址http://www.matrix67.com/blog/archives/105 这或许是众多OIer最大的误区之一.    你会经常看到网上出现“这怎么做,这不是NP问题吗”.“这个只有搜 ...

  2. HDU1760 A New Tetris Game NP态

    A New Tetris Game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. P与NP问题

    Polynomial Nondeterministic Polynomial P问题: 一个问题可以在多项式时间复杂度内解决 NP问题: 一个问题可以在多项式时间内证实或者证伪 NP-Hard问题: ...

  4. 浅谈P NP NPC

    P问题:多项式时间内可以找到解的问题,这个解可以在多项式时间内验证. NP问题:有多项式时间内可以验证的解的问题,而并不能保证可以在多项式时间内找到这个解. 比如汉密尔顿回路,如果找到,在多项式时间内 ...

  5. (数学)P、NP、NPC、NP hard问题

    概念定义: P问题:能在多项式时间内解决的问题: NP问题:(Nondeterministic Polynomial time Problem)不能在多项式时间内解决或不确定能不能在多项式时间内解决, ...

  6. P,NP,NP_hard,NP_complete问题定义

    背景:在看李航的<统计学习方法时>提到了NP完全问题,于是摆之. 问题解答:以下是让我豁然开朗的解答的摘抄: 最简单的解释:P:算起来很快的问题NP:算起来不一定快,但对于任何答案我们都可 ...

  7. P和NP问题

    1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html   NP----非定常多项式(英语:non-determin ...

  8. P,NP,NPC,NPC-HARD

    P: 能在多项式时间内解决的问题 NP: 不能在多项式时间内解决或不确定能不能在多项式时间内解决,但能在多项式时间验证的问题 NPC: NP完全问题,所有NP问题在多项式时间内都能约化(Reducib ...

  9. NP完全问题 NP-Completeness

    原创翻译加学习笔记,方便国人学习算法知识! 原文链接http://www.geeksforgeeks.org/np-completeness-set-1/ 我们已经找到很多很高效的算法来解决很难得问题 ...

  10. np问题

    NP(np) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 喜欢研究一些比较困难的问题,比如 np 问题.这次它又遇到一个棘手的 np 问题.问题是这个样子 ...

随机推荐

  1. 创建型模式——Builder

    1.意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 2.结构 3.参与者 Builder为创建一个Product对象的各个部件指定抽象接口 ConcreteBuild ...

  2. Mysql 作业(Scheduler)

    200 ? "200px" : this.width)!important;} --> 介绍 作业也叫做事件调度,其实它也就是一个时间触发器:它可以定义某个时间点执行指定的数 ...

  3. httpd 配置用户访问认证

    需求:单用户访问网站的某个目录,需要使用帐号密码来登录才能访问. 一.编辑虚拟主机的配置文件,添加目录级访问限制 <Directory "/var/www/html/demo" ...

  4. Nodejs 处理gb2312内容乱码问题

    在使用cheerio处理request模块返回的gb2312网页出现了乱码,从开始一直排查问题,一直排查到request.cheerio都有问题. 首先request会进行一次转码,这里需要设置req ...

  5. JAVA对象是如何占用内存的

      本文使用的是32位的JVM ,jdk1.6.本文基本是翻译的,加上了一些自己的理解,原文见文章底下链接.     在本文中,我们讨论如何计算或者估计一个JAVA对象占多少内存空间.(注意,使用 C ...

  6. 由于Linux操作平台屡次受到黑客的“青睐”,LINUX公司也越来越注重产品的安全问题。

    Guardian Digital公司和安全管理服务提供商(MSSP)Guardent公司推出的新产品将提高开放式源代码和Linux产品的安全性能. Guardian Digital公司将于本月底在纽约 ...

  7. Linux进程间通信IPC学习笔记之同步一(线程、互斥锁和条件变量)

    基础知识: 测试代码: 参考资料: Posix 多线程程序设计

  8. 关于javascript的window.onscroll方法

    ---恢复内容开始--- 今天在学习javascript的过程中被onscroll这个东西堵了一下午.心情极度郁闷. 在高度较大的网页中,我们通常会加一个返回顶部的按钮,方便用户操作. 代码如下: & ...

  9. Oracle中NULL值与索引

    NULL值是关系数据库系统布尔型(true,false,unknown)中比较特殊类型的一种值,通常称为UNKNOWN或空值,即是未知的,不确定的.由于NULL存在着无数的可能,因此NULL值也不等于 ...

  10. 如何应用CLR线程池来管理多线程

        class Program     {         static void Main(string[] args)         {             int intWorkerT ...