QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#756563#9551. The Emperorucup-team3670#WA 1ms3772kbC++203.1kb2024-11-16 20:57:442024-11-16 20:57:44

Judging History

你现在查看的是最新测评结果

  • [2024-11-16 20:57:44]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3772kb
  • [2024-11-16 20:57:44]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define sz(a) ((int)((a).size()))
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
#define endl "\n"

typedef pair<int, int> pt;
typedef long long li;
typedef long double ld;

const li INF64 = (li)(1e18);
const int INF = (int)(1e9);
const int MOD = 998244353;

int add(int a, int b){
	a += b;
	if (a >= MOD)
		a -= MOD;
	if (a < 0)
		a += MOD;
	return a;
}

int mul(int a, int b){
	return a * 1ll * b % MOD;
}

int binpow(int a, int b){
	int res = 1;
	while (b){
		if (b & 1)
			res = mul(res, a);
		a = mul(a, a);
		b >>= 1;
	}
	return res;
}


struct command
{
	int cond, go1, add0, go0;
	command(int cond = -1, int go1 = -1, int add0 = -1, int go0 = -1) : cond(cond), go1(go1), add0(add0), go0(go0) {};
};

int n;
vector<command> commands;

bool read()
{
	cin >> n;
	commands.resize(n);
	forn(i, n)
	{
		string s1;
		cin >> s1;
		if(s1 == "HALT;")
		{
			cin >> s1;
			int x;
			cin >> x;
			cin >> s1;
			int y;
			cin >> y;
			--y;
			commands[i] = command(-1, -1, x, y);
		}
		else
		{
			int x;
			cin >> x;
			cin >> s1;
			cin >> s1;
			int y = stoi(s1.substr(0, sz(s1) - 1));
			--y;
			cin >> s1;
			int z;
			cin >> z;
			cin >> s1;
			int a;
			cin >> a;
			--a;
			commands[i] = command(x, y, z, a);
		}
	}
	return true;
}

mt19937 rnd(time(NULL));

int solve_one(vector<int>& p)
{
	vector<bool> done(n);
	vector<pair<int, int>> cur(n);
	forn(i, n)
	{
		done[i] = false;
		cur[i] = mp(1, commands[i].go0);
	}
	//forn(i, n) cout << i << " " << commands[i].cond << " " << commands[i].go1 << " " << commands[i].add0 << " " << commands[i].go0 << endl;
	while(true)
	{
		//forn(i, n) cout << i << " " << cur[i].x << " " << cur[i].y << endl;
		int j = -1;
		forn(i, n)
		{
			int x = p[i];
			if(done[x]) continue;
			if(commands[cur[x].y].cond == commands[x].add0)
			{
				j = x;
				break;
			}
		}
		//cout << j << endl;
		if(j == -1) break;
		done[j] = true;
		cur[j].x = add(cur[j].x, 1);
		cur[j].y = commands[cur[j].y].go1;
		forn(i, n)
			if(!done[i] && commands[cur[i].y].cond != commands[i].add0 && cur[i].y == j)
			{
				cur[i].x = add(cur[i].x, cur[j].x);
				cur[i].y = cur[j].y;
			}
	}
	int ans = 0;
	int step = 0;
	forn(i, 1024)
	{
		if(commands[step].cond == -1)
			break;
		ans = add(ans, cur[step].x);
		step = cur[step].y;
	}
	if(commands[step].cond == -1)
		return add(ans, 1);
	else
		return -1;
}

void solve()
{
	int ans = -1;
	forn(i, 100)
	{
		vector<int> p(n);
		iota(p.begin(), p.end(), 0);
		shuffle(p.begin(), p.end(), rnd);
		int cur = solve_one(p);
		if(cur != -1) ans = cur;
	}
	cout << ans << endl;
}

int main(){
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
#endif
	cin.tie(0);
	ios::sync_with_stdio(false);
	int tc = 1;
	// cin >> tc;
	while(tc > 0)
	{
		--tc;
		read();
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3772kb

input:

1
HALT; PUSH 1 GOTO 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

5
POP 1 GOTO 2; PUSH 1 GOTO 2
HALT; PUSH 1 GOTO 3
POP 1 GOTO 4; PUSH 2 GOTO 4
POP 1 GOTO 2; PUSH 2 GOTO 4
HALT; PUSH 99 GOTO 4

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

1
POP 1 GOTO 1; PUSH 1 GOTO 1

output:

-1

result:

ok 1 number(s): "-1"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3708kb

input:

61
POP 62 GOTO 61; PUSH 30 GOTO 60
POP 1 GOTO 3; PUSH 62 GOTO 61
POP 2 GOTO 61; PUSH 62 GOTO 61
POP 4 GOTO 7; PUSH 2 GOTO 61
POP 62 GOTO 61; PUSH 3 GOTO 4
POP 62 GOTO 61; PUSH 3 GOTO 5
POP 5 GOTO 10; PUSH 3 GOTO 6
POP 62 GOTO 61; PUSH 4 GOTO 7
POP 62 GOTO 61; PUSH 4 GOTO 8
POP 6 GOTO 12; PUSH 4 GOTO...

output:

2

result:

wrong answer 1st numbers differ - expected: '150994941', found: '2'