题目链接

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.

Instruction Description
add vvv Add 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 kkk If 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 kkk If 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 kkk If 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 kkk If 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;
} 

C Halting Problem相关推荐

  1. halting problem 详解

    在讲解halting problem 问题之前,我们先来看一张图和一个小故事 图片: 图片来自网络,如有侵权请联系删除 故事: 理发师悖论:   在一个村子里只有有一个理发师,他说他只给不给自己理发的 ...

  2. Halting Problem的讨论

    有关于Halting Problem的一点点讨论 Halting Problem with no input: Halting Problem with no input: Show that giv ...

  3. ZOJ - 4049 Halting Problem 题解【c++】

    题目如下: In computability theory, the halting problem is the problem of determining, from a description ...

  4. 图灵停机问题(The Halting Problem)

    不存在这样一个程序(算法),它能够计算任何程序(算法)在给定输入上是否会结束(停机). 那么,如何来证明这个停机问题呢? 反证!假设我们某一天真做出了这么一个极度聪明的万能算法(就叫God_algo吧 ...

  5. The halting problem

    转自: http://www.matrix67.com/blog/article.asp?id=62 不可解问题(Undecidable Decision Problem)指的是这样一种问题:他无论如 ...

  6. 计算机无法解决停机问题,不可解问题之停机问题(Undecidable Problem Halting Problem)...

    计算机技术已运用到人类生活的方方面面,帮助人类解决各种问题.可你是否有想过,计算机是否能为人类解决所有问题呢? 假如你是一个程序猿,你已编写过很多程序.有些程序一下子就能出结果,有些程序则好久都没有显 ...

  7. 2018 青岛网络赛C题Halting Problem

    判断一个指定的程序是一直运行下去还是会停止.停止好判断,就是如何判断是否会一直运行下去.当第二次到达第n步的时候,如果此时的r仍然是第一次到达第n步时候的r,那么这个程序会一直运行下去. 通过这道题还 ...

  8. Halting Problem图灵机问题

    不存在这样一个程序(算法),它能够计算任何程序(算法)在给定输入上是否会结束(停机). 那么,如何来证明这个停机问题呢? 反证!假设我们某一天真做出了这么一个极度聪明的万能算法(就叫God_algo吧 ...

  9. Halting problem

    scheme伪代码 ; (test-halt program) -> true/false (define (forever)(forever)) (define (f)(if (test-ha ...

最新文章

  1. SpringSession(redis)
  2. python访问memcached
  3. 每日一皮:这题作的没毛病...
  4. Spring源码:AOP转文
  5. 【Best Time to Buy and Sell Stock III 】cpp
  6. 如何更好地优化大数据分析
  7. service数据保存_精通IPFS:IPFS 保存内容之下篇
  8. CTF中的RSA套路
  9. html5扫雷代码,使用js开发网页版 扫雷(附代码详解)
  10. 足球机器人比赛3V3
  11. ONOS 南向抽象层分析
  12. ffmpeg 视频转码
  13. 如何确定今天是星期几
  14. 查看windows系统许可证过期日期方法
  15. Echarts使用二:全国地图与各省市地图联动
  16. 手机端H5页面边框缺失问题解决
  17. R语言ggplot2可视化散点图(scatter plot)、aes函数中的fill参数为连续变量、使用scale_fill_distiller函数自定义指定连续变量的颜色填充方案
  18. Latex中用Bibtex来引用文献
  19. 498 对角线遍历(找规律)
  20. 算法第二节:逆推法解决“银行存款问题

热门文章

  1. python读取word文档并做简单的批量文档筛选
  2. 包含C/C++开发文档的Docset文档库
  3. 脉冲星 6 月脉动 | Pulsar Summit 北美峰会圆满结束,ApacheCon 议程官宣
  4. 博弈论 Nim游戏与SG函数
  5. Hypermesh2D网格划分实例1
  6. python对seo有什么用_pythonseo-python对于做SEO主要有什么作用
  7. IT项目管理总结:第七章 项目成本管理
  8. bilibili_Linux4_Linux常用命令
  9. 网站常用的favicon.ico文件
  10. Creating a Font for Apps and Games with Glyphs 如何使用Glyphs为应用和游戏创建字体 Lynda课程中文字幕