POJ 3233 Matrix Power Series(java)
型別:矩陣快速冪+二分
題解:
S1=A1;
S2=A1+A2=A1x(1+A1)=A1xS1;
S3=A1+A2+A3=A1x(1+A1)+A3=A1xS1+A3;
S4=A1+A2+A3+A4=A2x(1+A1+A2)=A2xS2;
然後這道題一開始是從高到低二分,發現tle了,然後選擇了從低到高 就ac了;
從高往低使用二分的程式碼(tle)
package POJ3233;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class Main {
static Matrix matrix;
static Matrix m;
static int mod;
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public static String next() throws IOException {
in.nextToken();
return (String) in.sval;
}
static class Matrix {
int a[][];
int n;
public Matrix(int n) {
super();
a = new int[n][n];
this.n = n;
}
public Matrix() {
super();
}
public Matrix Add(Matrix m) {
Matrix ans = new Matrix(m.n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans.a[i][j]=(a[i][j] + m.a[i][j])%mod;
}
}
return ans;
}
public Matrix Multi(Matrix m) {
Matrix ans = new Matrix(m.n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
ans.a[i][j] += a[i][k] * m.a[k][j];
}
ans.a[i][j]%=mod;
}
}
return ans;
}
}
public static void main(String[] args) throws IOException {
int n = nextInt();
int k = nextInt();
mod = nextInt();
matrix = new Matrix(n);
m= new Matrix(n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix.a[i][j] = nextInt()%mod;
m.a=matrix.a;
Matrix ans=getsum(k);
for (int i = 0; i < n; i++) {
out.print(ans.a[i][0]);
for (int j = 1; j < n; j++)
out.print(" "+ans.a[i][j]);
out.println();
}
out.flush();
}
static Matrix getsum( int sum) {
if (sum == 1)
return m;
else if (sum % 2 == 0) {
return quick(matrix, sum >>1)
.Multi(getsum(sum >>1))
.Add(getsum( sum >>1));
} else {
return quick(matrix, sum >>1)
.Multi(getsum( sum >>1))
.Add(getsum( sum >>1 ))
.Add(quick(matrix, sum));
}
}
static Matrix quick(Matrix n, int m) {
m--;
Matrix t = new Matrix(n.n);
t.a = n.a;
while (m != 0) {
if ((m & 1) == 1)
t.Multi(n);
n.Multi(n);
m >>= 1;
}
return t;
}
}
AC程式碼
import java.io.BufferedInputStream;
import java.util.Scanner;
public class A {
static Scanner in = new Scanner(new BufferedInputStream(System.in));
static long mod;
static class Mat {
public long a[][];
public int x;
public Mat(int x) {
a = new long[x][x];
this.x = x;
}
public Mat mu(Mat t) {
Mat ans = new Mat(x);
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++) {
for (int k = 0; k < x; k++) {
ans.a[i][j] += a[i][k] * t.a[k][j];
}
ans.a[i][j] %= mod;
}
}
return ans;
}
public Mat add(Mat t) {
Mat ans = new Mat(x);
for (int i = 0; i < x; i++) {
for (int j = 0; j < x; j++) {
ans.a[i][j] = (a[i][j] + t.a[i][j]) % mod;
}
}
return ans;
}
public Mat() {
super();
}
}
static Mat E;
static void setE(int s) {
E = new Mat(s);
for (int i = 0; i < s; i++)
E.a[i][i] = 1;
}
static Mat Z;
static void setZ(int s) {
Z = new Mat(s);
}
static Mat A;
static void setA(int s) {
A = new Mat(s);
for (int i = 0; i < s; i++) {
for (int j = 0; j < s; j++) {
A.a[i][j] = in.nextLong() % mod;
}
}
}
static class M {
public Mat a[][];
public M() {
a = new Mat[2][2];
}
public void set(Mat t, Mat b, Mat c, Mat d) {
a[0][0] = t;
a[0][1] = b;
a[1][0] = c;
a[1][1] = d;
}
public M mu(M t) {
M ans = new M();
ans.a[0][0] = E;
ans.a[1][0] = Z;
ans.a[0][1] = t.a[0][1].add(a[0][1].mu(t.a[1][1]));
ans.a[1][1] = a[1][1].mu(t.a[1][1]);
return ans;
}
}
static M x;
static M e;
static M pow(int t) {
M ans = e;
while (t > 0) {
if ((t & 1) == 1) {
ans = ans.mu(x);
}
x = x.mu(x);
t >>= 1;
}
return ans;
}
static Mat ans;
public static void main(String[] args) {
x=new M();
e=new M();
while(in.hasNext()) {
int n=in.nextInt(),k=in.nextInt();
mod=in.nextLong();
setE(n);setZ(n);setA(n);
x.set(E, A, Z, A);
e.set(E, Z, Z, E);
ans=pow(k).a[0][1];
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
if(j!=0)System.out.print(" ");
System.out.print(ans.a[i][j]);
}
System.out.println();
}
}
}
}
相關推薦
POJ 3233 Matrix Power Series(java)
型別:矩陣快速冪+二分 題解: S1=A1; S2=A1+A2=A1x(1+A1)=A1xS1; S3=A1+A2+A3=A1x(1+A1)+A3=A1xS1+A3; S4=A1+A2+A3+A4=A2x(1+A1+A2)=A2xS2; 然後這道
poj 3233 Matrix Power Series (構造分塊矩陣)
題目連結:哆啦A夢傳送門 題意:自己看。 參考部落格:神犇 題解:分塊矩陣:分塊矩陣可以構造求和。 例如:我們可以這樣構造, 還需注意一點的是:算完S(k+1),取出右上角矩陣分塊後,還需減掉單位矩陣E。 程式碼不是我寫的,我就按自己習慣改了下變數
POJ 3233 Matrix Power Series (矩陣快速冪+等比數列二分求和)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23205 Accepted: 9669 Description Given a n × n ma
POJ 3233 Matrix Power Series(矩陣等比數列求和)
題意就是一個等比數列求和的意思,只不過每一項都是矩陣 這裡需要進行一下轉移矩陣的構造,形成一個遞推累加的效果: 設 B = (A,I;0,I) 則B^(k + 1) = (A^(k + 1),I + A + A^2 + A^3 + … + A^k;0,I)
POJ 3233 Matrix Power Series 解題報告(子矩陣構造+矩陣快速冪)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 14105 Accepted: 6078 Description Given a n × n m
POJ 3233 Matrix Power Series
sin 多個 ide input span 拆分 lang con 快速冪 Matrix Power Series Time Limit:3000MS Memory Limit:131072K Description Given a n × n matrix A and
POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)
spa nta plm lines case arch lang stream 矩陣 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20309
POJ 3233 Matrix Power Series 【經典矩陣快速冪+二分】
ace series printf acc align clu same pro max 任意門:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit
POJ 3233 Matrix Power Series (矩陣乘法+快速冪+等比二分求和)
再加上快速冪演算法和就好了 #include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm>
POJ 3233 Matrix Power Series 【矩陣快速冪+等比矩陣】
——————————————————- Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20125 Accept
POJ 3233 Matrix Power Series(求矩陣冪的和——分塊矩陣快速冪 or 二分遞迴+矩陣快速冪)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 21451 Accepted:
POJ 3222 Matrix Power Series 【等比矩陣前n項之和(性質 OR 二分?)】
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20879 Accepted:
【poj3233】Matrix Power Series(遞推+矩陣快速冪)
題目描述 傳送門 題解 用樸素的矩陣快速冪的話時間無法承受。其實這道題的思路挺奇特的,不是自己想出來的很不爽。 這裡有一個遞推的思路:考慮k能不能從之前的狀態轉移過來。答案是肯定的。 當k%2
POJ 3233 Matrix Power (矩陣快速冪+等比數列求和)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23165 Accepted: 9651 Description Gi
【Matrix Power Series】【POJ - 3233 】(等比矩陣+矩陣乘法)
題目: Given a n × n matrix A and a positive integer k, find the sum S = A + A2 +&nb
poj3233(Matrix Power Series)解題報告
add ret can 運算 -- pro eof tar a* 題目鏈接 : http://poj.org/problem?id=3233 感覺做起來包含了矩陣的所有運算了,矩陣加法,矩陣乘法,矩陣快速冪,啊。。。寫了好久 題解 : k最大10^9,太大了,但經過觀察可以
Matrix Power Series POJ - 3233 (矩陣快速冪)
傳送門 題意: 題解: 附上程式碼: #include<iostream> #include<cstdio> using namespace std; typedef long long ll; const int MAXN=70; struct n
POJ 1753 翻轉游戲 列舉 (JAVA)
import java.util.Scanner; public class FlipGame { /* * 翻轉游戲 列舉 */ public static boolean isFinish(int[][] fields) { // 判斷是否完成
POJ:2406-Power Strings(尋找字串迴圈節)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Description Given two strings a and b we define a*b to be their con
poj 2808題校門外的樹(java)
先寫上程式碼,用java寫的: import java.util.*; import java.io.*; public class Main { public static void main(String []args)throws Exception{ Scan