Sicily 14180. Encoded Coordinates

14180. Encoded Coordinates

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

You have been monitoring a terrorist cell that is planning a big attack somewhere close. The terrorists have been sending encoded coordinates to each other, and you suspect that the attack will happen at one of the locations described by these coordinates.
You have intercepted the relevant communications for a series of locations you want to investigate.

Every location consists of a separate x and y coordinate, each being a non-negative integer smaller than some prime number P. Both coordinates are encoded separately using the same process. A source has given you information on the process of decoding a single
coordinate. The input of this process consists of five values: A, B, C, K and N.

Central to the decoding process is a collection of three functions, defined in terms of each other and the input value K:

The values A, B and C are used to initialize these functions as follows:

The decoded coordinate is the value of F(N) mod P.

As you might have noticed, there is some crucial information missing: you need the value of H(0) to calculate G(2). While you do not have this value, you do know that the x and y coordinate use the same value for H(0). Luckily, you have managed to already obtain
the x coordinates for the locations. Your colleagues have suggested that this does not give you enough information to calculate the y coordinates, but you want to prove them wrong.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with the prime number P (2 ≤ P ≤ 19 997). 
  • one line with five space-separated integers Ax, Bx, Cx, Kx and Nx (0 ≤&#20; Ax, Bx, Cx, Kx < P and 1 ≤&#20; Nx ≤&#20; 109): the parameters for the x coordinate. &#15;
  • one line with five space-separated integers Ay, By, Cy, Ky and Ny (0 ≤&#20; Ay, By, Cy, Ky < P and 1 ≤&#20; Ny ≤&#20; 109): the parameters for the y coordinate. &#15;
  • one line with one integer x (0 ≤&#20; x < P): the x coordinate.

Output

Per test case:&#15;

  • one line with one integer: the calculated y coordinate. If your colleagues are right, and there is not exactly one valid y coordinate, print “UNKNOWN” on a single line instead.

Sample Input

2
11
2 3 5 7 4
4 6 8 9 7
5
7
1 6 2 5 1
3 4 2 5 3
1

Sample Output

7
UNKNOWN

Problem Source

2015年每周一赛第七场

矩阵快速幂。

#include <stdio.h>

/*
[ 0 1 1 0 ][F(  n  )]     [F(n + 1)]
[ K 0 0 1 ][G(  n  )]  =  [G(n + 1)]
[ 1 K 0 0 ][H(  n  )]     [H(n + 1)]
[ 0 0 1 0 ][H(n - 1)]     [H(  n  )]
*/

const int OMm[4][4] = { { 0, 1, 1, 0 }, { 'k', 0, 0, 1 }, { 1, 'k', 0, 0 }, { 0, 0, 1, 0 } };
const int UMm[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } };

struct Matrix
{
	int m[4][4];
	Matrix() {}
	Matrix(const int m[4][4]) {
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				this->m[i][j] = m[i][j];
	}
};

int P, Ax, Bx, Cx, Kx, Nx, Ay, By, Cy, Ky, Ny, X;
Matrix Mx, My, OM(OMm), UM(UMm);

Matrix MatrixPlus(Matrix M1, Matrix M2) {
	Matrix A;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			A.m[i][j] = 0;
			for (int k = 0; k < 4; k++) A.m[i][j] = (A.m[i][j] + M1.m[i][k] * M2.m[k][j]) % P;
		}
	}
	return A;
}

Matrix MatrixKPlus(int k) {
	if (k == 0) return UM;
	if (k & 1) return MatrixPlus(OM, MatrixKPlus(k - 1));
	else {
		Matrix TempM = MatrixKPlus(k / 2);
		return MatrixPlus(TempM, TempM);
	}
}

int main() {

	int CaseNum;
	scanf("%d", &CaseNum);
	while (CaseNum--) {
		scanf("%d%d%d%d%d%d%d%d%d%d%d%d", &P, &Ax, &Bx, &Cx, &Kx, &Nx, &Ay, &By, &Cy, &Ky, &Ny, &X);
		OM.m[1][0] = OM.m[2][1] = Kx;
		Mx = MatrixKPlus(Nx - 1);
		OM.m[1][0] = OM.m[2][1] = Ky;
		My = MatrixKPlus(Ny - 1);
		int ABCx = (Mx.m[0][0] * Ax + Mx.m[0][1] * Bx + Mx.m[0][2] * Cx) % P;
		int ABCy = (My.m[0][0] * Ay + My.m[0][1] * By + My.m[0][2] * Cy) % P;
		int Ans = -1, MaybeH0;
		for (MaybeH0 = 0; MaybeH0 < P; MaybeH0++) {
			if ((MaybeH0 * Mx.m[0][3] + ABCx) % P == X) {
				if (Ans == -1) {
					Ans = (MaybeH0 * My.m[0][3] + ABCy) % P;
				}
				else if (Ans != (MaybeH0 * My.m[0][3] + ABCy) % P) {
					break;
				}
			}
		}
		if (MaybeH0 == P && Ans != -1) printf("%d\n", Ans);
		else printf("UNKNOWN\n");
	}

	return 0;
}
时间: 2024-12-29 01:53:45

Sicily 14180. Encoded Coordinates的相关文章

Project Euler 91:Right triangles with integer coordinates 格点直角三角形

Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ. There are exactly fourteen triangles containing a right angle that can be formed whe

Sicily 1146:Lenny&#39;s Lucky Lotto(dp)

题意:给出N,M,问有多少个长度为N的整数序列,满足所有数都在[1,M]内,并且每一个数至少是前一个数的两倍.例如给出N=4, M=10, 则有4个长度为4的整数序列满足条件: [1, 2, 4, 8], [1, 2, 4, 9], [1, 2, 4, 10], [1, 2, 5, 10] 分析:可用动态规划解题,假设dp[i][j],代表满足以整数i为尾数,长度为j的序列的个数(其中每一个数至少是前一个数的两倍).那么对于整数i,dp[i][j] 等于所有dp[k][j-1]的和,其中k满足:

Example: Encoded SNMP Message - SNMP Tutorial

30.11 Example Encoded SNMP Message The encoded form of ASN.1 uses variable-length fields to represent items. In general, each field begins with a header that specifies the type of object and its length in bytes. For example, each SEQUENCE begins with

Sicily 1004. I Conduit!

Constraints Time Limit: 3 secs, Memory Limit: 32 MB Description Irv Kenneth Diggit works for a company that excavates trenches, digs holes and generally tears up people's yards. Irv's job is to make sure that no underground pipe or cable is underneat

UVALive 5029 Encoded Barcodes

Encoded Barcodes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5029 Description All the big malls need a powerful system for the products retrieval. Now you are employed design a sub-system: rea

Project Euler:Problem 91 Right triangles with integer coordinates

The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ. There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 i

sicily 1345 能量项链

先模拟一下确定理解题意,然后再找状态转移方程,注意方向~ 1 //sicily 1345 能量项链 2 #include <bits/stdc++.h> 3 4 using namespace std; 5 6 int a[205]; 7 int dp[205][205]; 8 9 int main() 10 { 11 int n; 12 while(cin >> n) 13 { 14 memset(dp, 0, sizeof(dp)); 15 for(int i=0; i<

sicily 1063. Who&#39;s the Boss

Time Limit: 1sec    Memory Limit:32MB Description Several surveys indicate that the taller you are, the higher you can climb the corporate ladder. At TALL Enterprises Inc. this "de facto standard" has been properly formalized: your boss is alway

Sicily 1735 Encryption (模拟)

链接:http://soj.me/show_problem.php?pid=1735&cid= Description Let me introduce an easy method of encryption to you. Suppose there're N bytes (1 byte = 8 bits) data that are to be encrypted and we want to encrypt them in groups of M bytes, while for the