1. 程式人生 > >hdu1575(矩陣快速冪入門題)

hdu1575(矩陣快速冪入門題)

struct mat{
    int m[maxn][maxn];
}unit;//矩陣的資料結構
**過載矩陣*強調內容*乘法**
mat operator * (mat a,mat b)
{
    mat ret;
    ll x;
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    {
        x=0;
        for(int k=0;k<n;k++)
            x+=mod((ll)a.m[i][k]*b.m[k][j]);
        ret.m[i][j]=mod(x);
    }
    return
ret; }
#include<iostream>
#include<algorithm>
using namespace std;
#define mod(x) ((x)%9973)
typedef long long ll;
const int maxn =11;
int n;
struct mat{
    int m[maxn][maxn];
}unit;
//矩陣乘法
mat operator * (mat a,mat b)
{
    mat ret;
    ll x;
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    {
        x=0
; for(int k=0;k<n;k++) x+=mod((ll)a.m[i][k]*b.m[k][j]); ret.m[i][j]=mod(x); } return ret; } void init_unit() { for(int i=0;i<maxn;i++) unit.m[i][i]=1; return ; } mat pow_mat(mat a,ll n) { mat ret=unit; while(n) { if(n&1) { ret= ret*a; } n>>=1
; a=a*a; } return ret; } int main() { ios::sync_with_stdio(false); int x; int T; cin>>T; init_unit(); while(T--&&cin>>n>>x) { mat a; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a.m[i][j]; a=pow_mat(a,x); ll ans=0; for(int i=0;i<n;i++) ans+=a.m[i][i]; ans%=9973; cout<<ans<<endl; } return 0; }

相關推薦

hdu1575矩陣快速入門

struct mat{ int m[maxn][maxn]; }unit;//矩陣的資料結構 **過載矩陣*強調內容*乘法** mat operator * (mat a,mat b) { mat ret; ll x;

HDU1575矩陣快速模板

簡單的矩陣快速冪,輸入矩陣直接套模板做就行了。 code #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm>

HDU 5411 CRB and Puzzle 矩陣快速

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

【HDU2604】Queuing矩陣快速+遞推

題目連結 Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(

POJ——3070 Fibonacci 矩陣快速求fibonacci

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n&nbs

1757 A Simple Math Problem(矩陣快速入門)

題意:求下圖公式的結果     這一題 還是個 遞推關係式 的矩陣就非常的好推了  和之前的幾乎一樣  這裡就不解釋了 #include<cstdio> #include<cstring> using namespace std; const

Codeforces-954F:Runner's Problem矩陣快速+離散化

F. Runner's Problemtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are running t

hdu 5950 Recursive sequence矩陣快速,構造

N較大,直接遞推會超時,可以用矩陣快速冪 也是的函式,且 所以在構造的矩陣中維護到 #include <iostream> #include <cstdio> #include <algorithm> #include &

poj3734——矩陣快速入門

Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7111 Accepted: 3443 Description Panda has received an assignmen

POJ3070 Fibonacci矩陣快速加速遞推【模板

題目連結:傳送門 題目大意:   求斐波那契數列第n項F(n)。   (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路:   用矩陣乘法加速遞推。 演算法競賽進階指南的模板: #include <iostream> #include &l

2018ACM_ICPC焦作網路賽L Poor God Water矩陣快速

目錄 題意: 思路: AC程式碼: God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some s

洛谷 P1939 【模板】矩陣加速數列——————矩陣快速(水)

P1939 【模板】矩陣加速(數列) 題目描述 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 求a數列的第n項對1000000007(10^9+7)取餘的值。 輸入輸出格式 輸入格式: 第一行一個整數T,表示詢問個數。

Bzoj2510 弱矩陣快速

cst isp 這樣的 swap clu sca span ems inline 題面(權限題) 題解 一道概率\(dp\),可以設\(f[i][j]\)表示第\(i\)次操作後,標號為\(j\)的小球的期望個數,那麽有: \[ \begin{aligned} &f

HDU——1005Number Sequence模版 二維矩陣快速+操作符過載

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 148003    Accepted

矩陣快速HDU1575 Tr A

amp ott acc desc pre col stream ons 矩陣快速冪 Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total

UVa 11149 矩陣矩陣倍增法模板

ble 化簡 .cn target ans txt put std net https://vjudge.net/problem/UVA-11149 題意: 輸入一個n×n矩陣A,計算A+A^2+A^3+...A^k的值。 思路: 矩陣倍增法。

快速算法矩陣快速還不是很會。。日後會更新

代碼 -s get 運算 logs == data 。。 outb PS:轉載,自己寫的不如人家,怕誤導。轉載地址:http://www.cnblogs.com/CXCXCXC/p/4641812.html 首先,快速冪的目的就是做到快速求冪,假設我們要求a^b,按照樸素算

poj 3070 Fibonacci矩陣快速求Fibonacci數列

代碼 include cnblogs inf stream exp class set names 題目鏈接: http://poj.org/problem?id=3070 題意: 我們知道斐波那契數列0 1 1 2 3 5 8 13…… 數列中的第i位為第i-1位

poj 3735 Training little cats 矩陣快速

log ack make .cn code little logs 矩陣快速冪 style 題目鏈接: http://poj.org/problem?id=3735 題意: 有n只貓咪,開始時每只貓咪有花生0顆,現有一組操作,由下面三個中的k個操作組成:

[luoguP1962] 斐波那契數列矩陣快速

truct ons 技術 pan opera http 快速冪 printf ble 傳送門 解析詳見julao博客連接 http://worldframe.top/2017/05/10/清單-數學方法-——-矩陣/ —&