HDU - 2157 How many ways??

Posted cold-cold

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 2157 How many ways??相关的知识,希望对你有一定的参考价值。

#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
inline int read()
{
    register int p(1),a(0);register char ch=getchar();
    while((ch<0||ch>9)&&ch!=-) ch=getchar();
    if(ch==-) p=-1,ch=getchar();
    while(ch>=0&&ch<=9) a=a*10+ch-48,ch=getchar();
    return a*p;
}
const int N=22,mo=1000; 
struct matrix
{
    int data[N][N];
    matrix(){memset(data,0,sizeof(data));}
};
int u,v,n,m,k;
matrix chen(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++) if(a.data[i][j])
            for(int k=0;k<n;k++)
                c.data[i][k]=(c.data[i][k]+a.data[i][j]*b.data[j][k])%mo;
    return c;
}
matrix qs(matrix a,int b)
{
    matrix ans;
    for(int i=0;i<n;i++) ans.data[i][i]=1;
    while(b)
    {
        if(b&1) ans=chen(ans,a); 
        a=chen(a,a);
        b>>=1;
    }
    return ans;
}
int main()
{
//    freopen("input","r",stdin);
//    freopen("output","w",stdout);
    while(true)
    {
        n=read(),m=read();
        if(n+m==0) return 0;
        matrix a;
        while(m--)
        {
            u=read(),v=read();
            a.data[u][v]=1;
        }
        m=read();
        while(m--)
        {
            u=read(),v=read(),k=read();
            printf("%d
",qs(a,k).data[u][v]);
        }
    } 
    return 0;
}
/*

*/

 

 

以上是关于HDU - 2157 How many ways??的主要内容,如果未能解决你的问题,请参考以下文章

HDU2157 How many ways??---(邻接矩阵,图论,矩阵快速幂)

HDU 2157 How many ways??

hdu2157:How many ways??

hdu 2157 How many ways?? (可达矩阵)

HDU2157 How many ways矩阵再识

HDU 2157 How many ways?矩阵快速幂