UvaLive 6600 Spanning trees in a secure lock pattern 矩陣行列式,uvalivespanning

來源:互聯網
上載者:User

UvaLive 6600 Spanning trees in a secure lock pattern 矩陣行列式,uvalivespanning

連結:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4611

題意:給一個N*N個點的矩陣(N<=6),每個點只能和周圍八個點相連,問有多少種產生樹的方式。

思路:題裡給的很明白,就是列一個每個點的邊的矩陣,然後求子矩陣的行列式就可以了,因為N只有6,所以打表就可以了。

打表代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;#define MOD 1000#define maxn 40#define maxm 40struct Matrix{    int n,m;    double a[maxn][maxm];    void change(int c,int d)    {        n=c;        m=d;        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)                a[i][j]=0;    }    void Copy(const Matrix &x)    {        n=x.n;        m=x.m;        for(int i=0; i<n; i++)            for(int j=0; j<m; j++)                a[i][j]=x.a[i][j];    }    void build(int n)    {        change(n*n,n*n);        for(int i=0; i<n*n; i++)        {            if(i%n!=0)            {                a[i][i-1]=-1;                a[i-1][i]=-1;                a[i][i]++;                a[i-1][i-1]++;            }            if(i%n!=0&&i/n!=0)            {                a[i][i-n-1]=-1;                a[i-n-1][i]=-1;                a[i][i]++;                a[i-n-1][i-n-1]++;            }            if(i%n!=0&&i/n!=n-1)            {                a[i][i+n-1]=-1;                a[i+n-1][i]=-1;                a[i][i]++;                a[i+n-1][i+n-1]++;            }            if(i/n!=n-1)            {                a[i][i+n]=-1;                a[i+n][i]=-1;                a[i][i]++;                a[i+n][i+n]++;            }        }    }    double det()    {        for(int i=1; i<n; i++)        {            for(int j=0; j<i; j++)                if(a[i][j]!=0)                {                    for(int k=j+1; k<m; k++)                        a[i][k]-=(a[j][k]*a[i][j]/a[j][j]);                    a[i][j]=0;                }        }        double ans=1;        for(int i=0; i<n-1; i++)            ans*=a[i][i];        return ans;    }};int main(){    int t;    scanf("%d",&t);    Matrix A;    A.build(t);    printf("%.0f\n",A.det());    return 0;}
AC代碼:

int main(){    char ss[10][40]={"1","16","17745","1064918960","3271331573452806","504061943351319050000000"};    int T;    scanf("%d",&T);    while(T--)    {        int a;        scanf("%d",&a);        puts(ss[a-1]);    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.