UVa 371 Ackermann Functions (water ver.)

本文介绍了一种特定的整数Ackermann函数,该函数生成的序列最终收敛于1,并提供了求解最长序列及其长度的方法。通过示例展示了如何计算从特定起始值到1的序列长度,并给出了一段实现该功能的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

371 - Ackermann Functions

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=307

An Ackermann function has the characteristic that the length of the sequence of numbers generated by the function cannot be computed directly from the input value. One particular integer Ackermann function is the following:

displaymath32

This Ackermann has the characteristic that it eventually converges on 1. A few examples follow in which the starting value is shown in square brackets followed by the sequence of values that are generated, followed by the length of the sequence in curly braces:

     [10] 5 16 8 4 2 1 {6}
     [13] 40 20 10 5 16 8 4 2 1 {9}
     [14] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 {17}
     [19] 58 29 88 44 22 ... 2 1 {20}
     [32] 16 8 4 2 1 {5}
     [1] 4 2 1 {3}

Input and Output

Your program is to read in a series of pairs of values that represent the first and last numbers in a closed sequence. For each closed sequence pair determine which value generates the longest series of values before it converges to 1. The largest value in the sequence will not be larger than can be accomodated in a 32-bit Pascal LongInt or C long. The last pair of values will be 0, 0. The output from your program should be as follows:

Between L and H, V generates the longest sequence of S values.

Where:

L = the lower boundary value in the sequence

H = the upper boundary value in the sequence

V = the first value that generates the longest sequence, (if two or more values generate the longest sequence then only show the lower value) S = the length of the generated sequence.

In the event that two numbers in the interval should both produce equally long sequences, report the first.

Sample Input

  1 20
 35 55
  0 0

Sample Output

Between 1 and 20, 18 generates the longest sequence of 20 values.
Between 35 and 55, 54 generates the longest sequence of 112 values.

这一题


完整代码:

/*0.126s*/

#include<bits/stdc++.h>
using namespace std;

int main()
{
	///坑1
	long long i, j, n, m, count, maxi, max;
	while (scanf("%lld%lld", &n, &m), n || m)
	{
		if (n > m) swap(n, m);///坑2
		max = 0;
		for (i = n; i <= m; ++i)
		{
			j = i;
			count = 0;
			if (j == 1) count = 3;
			while (j != 1)
			{
				if (j & 1) j += (j << 1) + 1;
				else j >>= 1;
				++count;
			}
			if (count > max)
			{
				max = count;
				maxi = i;
			}
		}
		printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", n, m, maxi, max);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值