Submission #374193


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define MOD @
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

i64 A, B, X;
int W;

struct matrix
{
	int val[36][36];
};

matrix calc_rev(matrix m)
{
	matrix ret;

	for (int i = 0; i < W; ++i) {
		for (int j = 0; j < W; ++j) {
			ret.val[i][j] = (i == j) ? 1 : 0;
		}
	}

	for (int i = 0; i < W; ++i) {
		if (m.val[i][i] != 1) {
			for (int j = i + 1; j < W; ++j) if (m.val[j][i] == 1) {
				for (int k = 0; k < W; ++k) {
					swap(m.val[j][k], m.val[i][k]);
					swap(ret.val[j][k], ret.val[i][k]);
				}
				goto nex;
			}
			fprintf(stderr, "><");
			exit(0);

		nex:
			i += 0;
		}

		for (int j = 0; j < W; ++j) if (i != j && m.val[j][i] == 1) {
			for (int k = 0; k < W; ++k) {
				m.val[j][k] ^= m.val[i][k];
				ret.val[j][k] ^= ret.val[i][k];
			}
		}
	}

	return ret;
}

matrix matmul(matrix a, matrix b)
{
	matrix ret;
	for (int i = 0; i < W; ++i) {
		for (int j = 0; j < W; ++j){
			ret.val[i][j] = 0;
			for (int k = 0; k < W; ++k) ret.val[i][j] ^= a.val[i][k] & b.val[k][j];
		}
	}
	return ret;
}

matrix matpow(matrix &m, int p)
{
	if (p == 1) return m;
	matrix tmp = matpow(m, p / 2);
	tmp = matmul(tmp, tmp);
	if (p % 2 == 1) tmp = matmul(tmp, m);
	return tmp;
}

i64 apply(matrix &m, i64 vec)
{
	i64 ret = 0;
	for (int i = 0; i < W; ++i) {
		int sig = 0;
		for (int j = 0; j < W; ++j) {
			sig ^= m.val[i][j] & (int)(vec >> (i64)j) & 1;
		}
		ret |= (i64)sig << (i64)i;
	}
	return ret;
}

void show(matrix &mat)
{
	for (int i = 0; i < W; ++i) {
		for (int j = 0; j < W; ++j) printf("%d ", mat.val[i][j]);
		puts("");
	}
	puts("");
}

int main()
{
	scanf("%lld%lld%lld", &A, &B, &X);
	int def = 50050;

	for (int i = 0; i < def; ++i) {
		if (A == X) {
			printf("%d\n", i);
			return 0;
		}
		A = (A / 2) ^ (A % 2 * B);
	}

	if (B == 0) {
		puts("-1");
		return 0;
	}

	W = 0;
	for (int i = 1; i <= 36; ++i) if ((1LL << (i64)i) > B) {
		W = i;
		break;
	}

	if ((1LL << (i64)W) <= X) {
		puts("-1");
		return 0;
	}

	matrix matr, rev, heavy;
	for (int i = 0; i < W; ++i) for (int j = 0; j < W; ++j) matr.val[i][j] = 0;
	for (int i = 0; i < W - 1; ++i) matr.val[i][i + 1] = 1;
	for (int i = 0; i < W; ++i) if ((1LL << (i64)i) & B) matr.val[i][0] = 1;

	heavy = matpow(matr, 1 << 18);
	rev = calc_rev(heavy);

	matrix mm = matmul(heavy, rev);
	//show(rev);
	//show(mm);

	map<i64, int> lval;
	i64 ret = 1LL << 60LL;

	for (int i = 0; i < (1 << 18); ++i) {
		if (lval.count(A) == 0) {
			lval.insert(make_pair(A, i));
		}
		A = apply(matr, A);
	}
	for (int i = 0; i < (1 << 18); ++i) {
		if (lval.count(X)) {
			ret = min(ret, (i64)i * (1 << 18) + lval[X]);
		}
		X = apply(rev, X);
	}

	if (ret > 1LL << 38LL) ret = -def - 1;
	printf("%lld\n", ret + def);

	return 0;
}

Submission Info

Submission Time
Task K - 乱数調整
User semiexp
Language C++11 (GCC 4.9.2)
Score 300
Code Size 3146 Byte
Status AC
Exec Time 1870 ms
Memory 17692 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:107:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld", &A, &B, &X);
                                   ^

Judge Result

Set Name Subtask All
Score / Max Score 30 / 30 270 / 270
Status
AC × 20
AC × 63
Set Name Test Cases
Subtask subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt
All scrambled_00.txt, scrambled_01.txt, scrambled_02.txt, scrambled_03.txt, scrambled_04.txt, scrambled_05.txt, scrambled_06.txt, scrambled_07.txt, scrambled_08.txt, scrambled_09.txt, scrambled_10.txt, scrambled_11.txt, scrambled_12.txt, scrambled_13.txt, scrambled_14.txt, scrambled_15.txt, scrambled_16.txt, scrambled_17.txt, scrambled_18.txt, scrambled_19.txt, scrambled_20.txt, scrambled_21.txt, scrambled_22.txt, scrambled_23.txt, scrambled_24.txt, scrambled_25.txt, scrambled_26.txt, scrambled_27.txt, scrambled_28.txt, scrambled_29.txt, scrambled_30.txt, scrambled_31.txt, scrambled_32.txt, scrambled_33.txt, scrambled_34.txt, scrambled_35.txt, scrambled_36.txt, scrambled_37.txt, scrambled_38.txt, scrambled_39.txt, scrambled_40.txt, scrambled_41.txt, scrambled_42.txt, subtask_00.txt, subtask_01.txt, subtask_02.txt, subtask_03.txt, subtask_04.txt, subtask_05.txt, subtask_06.txt, subtask_07.txt, subtask_08.txt, subtask_09.txt, subtask_10.txt, subtask_11.txt, subtask_12.txt, subtask_13.txt, subtask_14.txt, subtask_15.txt, subtask_16.txt, subtask_17.txt, subtask_18.txt, subtask_19.txt
Case Name Status Exec Time Memory
scrambled_00.txt AC 26 ms 920 KB
scrambled_01.txt AC 24 ms 796 KB
scrambled_02.txt AC 25 ms 920 KB
scrambled_03.txt AC 43 ms 1172 KB
scrambled_04.txt AC 628 ms 9376 KB
scrambled_05.txt AC 26 ms 800 KB
scrambled_06.txt AC 25 ms 800 KB
scrambled_07.txt AC 602 ms 7324 KB
scrambled_08.txt AC 788 ms 17048 KB
scrambled_09.txt AC 796 ms 17568 KB
scrambled_10.txt AC 782 ms 17048 KB
scrambled_11.txt AC 787 ms 17044 KB
scrambled_12.txt AC 640 ms 9244 KB
scrambled_13.txt AC 23 ms 924 KB
scrambled_14.txt AC 24 ms 928 KB
scrambled_15.txt AC 1716 ms 17576 KB
scrambled_16.txt AC 25 ms 844 KB
scrambled_17.txt AC 25 ms 928 KB
scrambled_18.txt AC 1715 ms 17564 KB
scrambled_19.txt AC 1705 ms 17568 KB
scrambled_20.txt AC 1852 ms 17564 KB
scrambled_21.txt AC 1801 ms 17572 KB
scrambled_22.txt AC 1863 ms 17692 KB
scrambled_23.txt AC 1780 ms 17556 KB
scrambled_24.txt AC 1870 ms 17568 KB
scrambled_25.txt AC 1543 ms 17568 KB
scrambled_26.txt AC 1787 ms 17568 KB
scrambled_27.txt AC 1810 ms 17632 KB
scrambled_28.txt AC 27 ms 924 KB
scrambled_29.txt AC 27 ms 804 KB
scrambled_30.txt AC 37 ms 788 KB
scrambled_31.txt AC 314 ms 1308 KB
scrambled_32.txt AC 26 ms 912 KB
scrambled_33.txt AC 26 ms 804 KB
scrambled_34.txt AC 523 ms 5280 KB
scrambled_35.txt AC 615 ms 9384 KB
scrambled_36.txt AC 752 ms 17564 KB
scrambled_37.txt AC 812 ms 17568 KB
scrambled_38.txt AC 807 ms 17576 KB
scrambled_39.txt AC 1848 ms 17596 KB
scrambled_40.txt AC 1784 ms 17572 KB
scrambled_41.txt AC 1841 ms 17568 KB
scrambled_42.txt AC 1832 ms 17572 KB
subtask_00.txt AC 25 ms 736 KB
subtask_01.txt AC 161 ms 1192 KB
subtask_02.txt AC 25 ms 736 KB
subtask_03.txt AC 25 ms 792 KB
subtask_04.txt AC 24 ms 800 KB
subtask_05.txt AC 24 ms 920 KB
subtask_06.txt AC 24 ms 800 KB
subtask_07.txt AC 24 ms 844 KB
subtask_08.txt AC 24 ms 796 KB
subtask_09.txt AC 25 ms 800 KB
subtask_10.txt AC 24 ms 736 KB
subtask_11.txt AC 23 ms 800 KB
subtask_12.txt AC 24 ms 924 KB
subtask_13.txt AC 24 ms 928 KB
subtask_14.txt AC 24 ms 732 KB
subtask_15.txt AC 24 ms 792 KB
subtask_16.txt AC 24 ms 736 KB
subtask_17.txt AC 31 ms 1180 KB
subtask_18.txt AC 24 ms 796 KB
subtask_19.txt AC 24 ms 920 KB