HDU2256-Problem of Precision (matrix construction + Rapid power), power zero matrix
Question Link
Question: sqrt (2) + sqrt (3) ^ 2n MOD 1024
Ideas:
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MOD = 1024;int n;struct mat { int s[2][2]; mat(int a = 0, int b = 0, int c = 0, int d = 0) { s[0][0] = a; s[0][1] = b; s[1][0] = c; s[1][1] = d; } mat operator * (const mat& c) { mat ans; sizeof(ans.s, 0, sizeof(ans.s)); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD; return ans; }}tmp(5, 12, 2, 5);mat pow_mod(int k) { if (k == 1) return tmp; mat a = pow_mod(k / 2); mat ans = a * a; if (k % 2) ans = ans * tmp; return ans;}int main() { int cas; scanf("%d", &cas); while (cas--) { scanf("%d", &n); mat ans = pow_mod(n); printf("%d\n", (ans.s[0][0] * 2 - 1) % MOD); } return 0;}
Pascal's rapid power matrix multiplication, detailed explanation and implementation
Write a framework for you. The quick power is binary recursion.
Function quick (var x: array [1 .. 2, 1 .. 2] of integer); based on your own matrix size
Var y: array [1 .. 2, 1 .. 2] of integer;
Begin
If n = 1 then exit (a); a is the original base matrix.
Y: = quick (n div 2 );
If n mod 2 = 0 then exit (jucheng (y, y) jucheng is the function of moment multiplication.
Else exit (jucheng (y, y), ));
End;
Construct a 3*3 Latin matrix, so that the numbers 1, 2, and 3 in each column in each row in this matrix only appear at 30 points.
There are more than three cases, and there should be 12.
3 2 1
2 1 3
1 3 2
3 2 1
1 3 2
2 1 3
2 1 3
3 2 1
1 3 2
2 1 3
1 3 2
3 2 1
1 3 2
3 2 1
2 1 3
1 3 2
2 1 3
3 2 1
1 2 3
2 3 1
3 1 2
1 2 3
3 1 2
2 3 1
2 3 1
1 2 3
3 1 2
2 3 1
3 1 2
1 2 3
3 1 2
1 2 3
2 3 1
3 1 2
2 3 1
1 2 3
The procedure is as follows:
# Include <stdio. h>
# Include <string. h>
Void init (int seed [3], int a [3] [3])
{
Int I, j;
For (I = 0; I <3; I ++ ){
For (j = 0; j <3; j ++ ){
A [I] [j] = seed [(I + j) % 3];
}
}
}
Void print (int x [3] [3])
{
Int I, j;
For (I = 0; I <3; I ++ ){
For (j = 0; j <3; j ++ ){
Printf ("% d", x [I] [j]);
}
Printf ("\ n ");
}
}
Void show (int seed [3])
{
Int x [3] [3];
Int a [3] [3];
Int I, j, k;
Init (seed, );
For (I = 0; I <3; I ++ ){
For (j = 0; j <3; j ++ ){
If (j! = I ){
K = 3-i-j;
Memcpy (x [0], a [I], sizeof (int [3]);
Memcpy (x [1], a [j], sizeof (int [3]);
Memcpy (x [2], a [k], sizeof (int [3]);
Print (x );
Printf ("\ n ");
}
}
}
}
Void main ()
{
Int I;
Int asc [3] = {1, 2}, dsc [3] = {3, 2, 1 };
Int matrix [3] [3];
Show (dsc );
Show (asc );
}... Remaining full text>