C Halting Problem

本文探讨了停机问题,一种在计算理论中确定任意程序是否能停止的问题。文章介绍了一种特定编程语言DreamLanguage的五种指令,并提出了解决停机问题的方法。通过分析程序指令,判断程序是否会在特定条件下停止运行。

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

题目链接

C Halting Problem

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program, whether the program will finish running (i.e., halt) or continue to run forever.

Alan Turing proved in 1936 that a general algorithm to solve the halting problem cannot exist, but DreamGrid, our beloved algorithm scientist, declares that he has just found a solution to the halting problem in a specific programming language -- the Dream Language!

Dream Language is a programming language consists of only 5 types of instructions. All these instructions will read from or write to a 8-bit register rrr, whose value is initially set to 0. We now present the 5 types of instructions in the following table. Note that we denote the current instruction as the iii-th instruction.

InstructionDescription
add vvvAdd vvv to the register rrr. As rrr is a 8-bit register, this instruction actually calculates (r+v)mod256(r + v) \mod 256(r+v)mod256 and stores the result into rrr, i.e. r←(r+v)mod256r \leftarrow (r + v) \mod 256r←(r+v)mod256. After that, go on to the (i+1)(i + 1)(i+1)-th instruction.
beq vvv kkkIf the value of rrr is equal to vvv, jump to the kkk-th instruction, otherwise go on to the (i+1)(i + 1)(i+1)-th instruction.
bne vvv kkkIf the value of rrr isn't equal to vvv, jump to the kkk-th instruction, otherwise go on to the (i+1)(i + 1)(i+1)-th instruction.
blt vvv kkkIf the value of rrr is strictly smaller than vvv, jump to the kkk-th instruction, otherwise go on to the (i+1)(i + 1)(i+1)-th instruction.
bgt vvv kkkIf the value of rrr is strictly larger than vvv, jump to the kkk-th instruction, otherwise go on to the (i+1)(i + 1)(i+1)-th instruction.

A Dream Language program consisting of nnn instructions will always start executing from the 1st instruction, and will only halt (that is to say, stop executing) when the program tries to go on to the (n+1)(n + 1)(n+1)-th instruction.

As DreamGrid's assistant, in order to help him win the Turing Award, you are asked to write a program to determine whether a given Dream Language program will eventually halt or not.

Input

There are multiple test cases. The first line of the input is an integer TTT, indicating the number of test cases. For each test case:

The first line contains an integer nnn (1≤n≤1041 \le n \le 10^41≤n≤10​4​​), indicating the number of instructions in the following Dream Language program.

For the following nnn lines, the iii-th line first contains a string sss (s∈{“add”,“beq”,“bne”,“blt”,“bgt”}s \in \{\text{``add''}, \text{``beq''}, \text{``bne''}, \text{``blt''}, \text{``bgt''}\}s∈{“add”,“beq”,“bne”,“blt”,“bgt”}), indicating the type of the iii-th instruction of the program.

  • If sss equals to "add", an integer vvv follows (0≤v≤2550 \le v \le 2550≤v≤255), indicating the value added to the register;
  • Otherwise, two integers vvv and kkk follow (0≤v≤2550 \le v \le 2550≤v≤255, 1≤k≤n1 \le k \le n1≤k≤n), indicating the condition value and the destination of the jump.

It's guaranteed that the sum of nnn of all test cases will not exceed 10510^510​5​​.

Output

For each test case output one line. If the program will eventually halt, output "Yes" (without quotes); If the program will continue to run forever, output "No" (without quotes).

Sample Input

4
2
add 1
blt 5 1
3
add 252
add 1
bgt 252 2
2
add 2
bne 7 1
3
add 1
bne 252 1
beq 252 1

Sample Output

Yes
Yes
No
No

Hint

For the second sample test case, note that rrr is a 8-bit register, so after four "add 1" instructions the value of rrr will change from 252 to 0, and the program will halt.

For the third sample test case, it's easy to discover that the value of rrr will always be even, so it's impossible for the value of rrr to be equal to 7, and the program will run forever.

思路:

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#define in(x) scanf("%d",&x)
#define out(x) printf("%d",x)
#define rep(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=10000+5;
int k[maxn],lucky[maxn];
int i,j,sum,r,flag;
const int mod=256;
struct node{
	string str;
	int v,k;
};
int main()
{
	string str;
	int t,a,v,k;
	in(t);
	while(t--){
		node pp[maxn];
		memset(lucky,0,sizeof(lucky));
		in(a);
		sum=r=0;
		rep(i,1,a){//模拟大法 
			cin>>pp[i].str>>pp[i].v;
			if(pp[i].str!="add"){
				cin>>pp[i].k;
			}
		}
		flag=0;
		i=1;
		while(1){//“add”,“beq”,“bne”,“blt”,“bgt”
		    lucky[i]++;	
			if(pp[i].str=="add"){
				r=(r+pp[i].v%mod)%mod;
				i++;
			}
			else if(pp[i].str=="beq"){
				if(pp[i].v==r) i=pp[i].k;
				else i++;
			}
			else if(pp[i].str=="blt"){
				 if(pp[i].v>r) i=pp[i].k;
				 else i++;
			}
			else if(pp[i].str=="bne"){
				if(pp[i].v!=r) i=pp[i].k;
				else i++;
			}
			else if(pp[i].str=="bgt"){
				if(pp[i].v<r) i=pp[i].k;
				else i++;
			}
			if(i>a) 
			{
				flag=1; 
			    break; 
			}   
			if(lucky[i]>=256) break;
		}
		if(flag==1) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值