POJ 3735 Training Little cats__ matrix fast Power

Source: Internet
Author: User
Tags mul

The main effect of the topic:

Fesse's pet cat just gave birth to a litter of kittens, considering their health, Fesse decided to let them do more exercise. It has designed a series of actions to let them do, and let you supervise them, just in case they are too much hung up, a total of three actions, formalized description:

G I: Let the first cat get a peanut

E i: Let the first cat eat all the peanuts in its hand

S I j: let the first cat and the J Cat Exchange The Peanuts in hand

Fesse will give some of the above action (number fixed), and then repeat some of the column actions several times.

There are a number of examples first, in each case there will be N cats (1≤n≤100, numbered 1-n, measured with n as 0), repeated m (0≤m≤1,000,000,000), and the number of repetitions of each series of actions K (1≤k≤100), which is now calculated in each case, repeating all moving How many peanuts are there in the hands of each cat?

Topic link

Comment Code:

* * Problem id:poj 3735 Training Little Cats * Author:lirx.t.una                          
* LANGUAGE:C * Run time:110 MS * Run memory:416 KB
* * #include <string.h> #include <stdio.h>//maximum Number of cats//cats, due to the width of the transfer matrix 1 larger than the number of cats

The subscript is therefore marked with 1 #define MAXCATN 102 typedef long LLG;
The body of main matrix, the main matrices//The results of the original series operations Llg B[maxcatn][maxcatn];
Answer matrix, storing matrices of fast power results llg A[maxcatn][maxcatn];

Temporary matrix, storing intermediate result matrices llg T[maxcatn][maxcatn]; int Ms;//matrix size, width of the matrix (cat number plus 1) void Mul (LLG (*M1) [Maxcatn], LLG (*M2) [Maxcatn]) {//multiply//matrix multiplication, the result is stored in the first matrix, so You can avoid a lot of duplication when the return value//is passed and the time to allocate space when creating matrices//matrix1,matrix2 int i, J, k;//count variable memset (t, 0, sizeof (t));//use T to store multiplication results fo R (i = 1; I <= ms; i++) for (k = 1; k <= ms; k++) if (m1[i][k))//important pruning, otherwise tle for (j = 1; J <= ms ; J + +) T[i][j] + = m1[i][K] * M2[k][j]; MEMCPY (M1, T, sizeof (T))//overwrite result to first matrix} void Fstxp (LLG (*m) [Maxcatn], Llg N) {//fast exponentiation//Matrix fast power, algorithm and exponentiation modulo operation

	In the same way, the binary int i is adopted;

	memset (A, 0, sizeof (a));

		for (i = 1; I <= ms; i++) A[i][i] = 1ll;//First initializes answer to the unit matrix while (n) {if (n & 1) Mul (A, m);

		Mul (M, m);
	n >>= 1;
	int main () {int n, m, k;//cat number, repeat number, each cycle operand char cmd;
	int x, y;
	int i;

		LLG Swp;//swap, as Exchange meta while (scanf ("%d%d%d", &n, &m, &k), n) {ms = n + 1;

		memset (b, 0, sizeof (b)); for (i = 1; I <= ms; i++) B[i][i] = 1ll;//First initializes the primary matrix to the unit matrix//Last row as the cat Peanut record row while (k--) {scanf ("\n%c"),
			&cmd);
					Switch (cmd) {case ' G '://+ 1 only acts on the last line scanf ("%d", &x);
					b[ms][x]++;

				Break
					Case ' E '://Clear 0 acts on each line scanf ("%d", &x);
					for (i = 1; I <= ms; i++) b[i][x] = 0LL;

				Break
					Default://Exchange functions on each line scanf ("%d%d", &x, &y); for (i = 1; i; = ms;
						i++) {SWP = b[i][x];
						B[I][X] = B[i][y];
					B[i][y] = SWP;
			} break;

		} fstxp (b, M);
		for (i = 1; I <= n; i++) printf ("%lld", A[ms][i));
	Putchar (' \ n ');
return 0; }

No comment code:

#include <string.h> #include <stdio.h> #define MAXCATN 102 typedef long LLG;
LLG B[maxcatn][maxcatn];
LLG A[maxcatn][maxcatn];

LLG T[maxcatn][maxcatn];

int MS;

	void Mul (LLG (*M1) [Maxcatn], LLG (*M2) [Maxcatn]) {int I, j, K;

	memset (t, 0, sizeof (t));
					for (i = 1; I <= ms; i++) for (k = 1; k <= ms; k++) if (m1[i][k)) for (j = 1; J <= ms; j)

	T[I][J] + = m1[i][k] * M2[k][j];
MEMCPY (M1, T, sizeof (t));

	} void Fstxp (LLG (*m) [Maxcatn], Llg n) {int i;

	memset (A, 0, sizeof (a));

	for (i = 1; I <= ms; i++) A[i][i] = 1LL;

		while (n) {if (n & 1) Mul (A, m);

		Mul (M, m);
	n >>= 1;
	int main () {int n, m, K;
	char cmd;
	int x, y;
	int i;

	LLG SWP;

		while (scanf ("%d%d%d", &n, &m, &k), n) {ms = n + 1;

		memset (b, 0, sizeof (b));

		for (i = 1; I <= ms; i++) B[i][i] = 1LL;
			while (k--) {scanf ("\n%c", &cmd); Switch (cmd) {case 'G ': scanf ("%d", &x);
					b[ms][x]++;

				Break
					Case ' E ': scanf ("%d", &x);
					for (i = 1; I <= ms; i++) b[i][x] = 0LL;

				Break
					DEFAULT:SCANF ("%d%d", &x, &y);
						for (i = 1; I <= ms; i++) {SWP = b[i][x];
						B[I][X] = B[i][y];
					B[i][y] = SWP;
			} break;

		} fstxp (b, M);
		for (i = 1; I <= n; i++) printf ("%lld", A[ms][i));
	Putchar (' \ n ');
return 0; }

Word Explanation:

Facer: Name, Fesse

Pet:n, Pets

Give birth to:vt, having a baby.

A brood of: a Nest, a gang

SUPERVISE:VT, supervision, guidance

Peanut:n, Peanut.

PERFORM:VT, execution, completion, performance

Come up WITH:VT, raised, came up

Embarrassing:adj, embarrassing.

Quantity:n, amount, quantity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.