转移矩阵+快速幂HDU5607

题意:http://acm.hdu.edu.cn/showproblem.php?pid=5607

有向图中问你从定点u走到x的概率

思路:

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=60;
 51 
 52 struct Mat
 53 {
 54     ll mat[N][N];
 55     Mat operator*(const Mat &a)const
 56     {
 57         Mat b=Mat();
 58         for(int i=1;i<=55;++i)
 59         {
 60             for(int j=1;j<=55;++j)
 61             {
 62                 for(int k=1;k<=55;++k)
 63                 {
 64                     b.mat[i][j]=(b.mat[i][j]+mat[i][k]*a.mat[k][j]%mod);
 65                     b.mat[i][j]%=mod;
 66                 }
 67             }
 68         }
 69         return b;
 70     }
 71 }start;
 72 Mat Mqpow(long long n,int x)
 73 {
 74     Mat ans=Mat();
 75     for(int i=1;i<=x;++i)
 76         ans.mat[i][i]=1;
 77     while(n)
 78     {
 79         if(n&1)
 80             ans=ans*start;
 81         start=start*start;
 82         n>>=1;
 83     }
 84     return ans;
 85 }
 86 ll qpow(ll a,ll b,ll mod)
 87 {
 88     ll ans;
 89 //    a%=mod;
 90     ans=1;
 91     while(b!=0)
 92     {
 93         if(b&1)
 94             ans=(a*ans)%mod;//注意左乘右乘;
 95         b/=2;
 96         a=(a*a)%mod;
 97     }
 98     return ans;
 99 }
100 ll cnt[N];
101 
102 int main()
103 {
104     int n,m;
105     while(~sc("%d%d",&n,&m))
106     {
107         mem(cnt,0);
108         start=Mat();
109         for(int i=1;i<=m;++i)
110         {
111             int u,v;
112             sc("%d%d",&u,&v);
113             start.mat[u][v]++;
114             cnt[u]++;
115         }
116         for(int i=1;i<=n;++i)
117             for(int j=1;j<=n;++j)
118                 if(start.mat[i][j])
119                     start.mat[i][j]=start.mat[i][j]*qpow(cnt[i],mod-2,mod)%mod;
120         int ask;
121         sc("%d",&ask);
122         Mat temp=start;
123         for(int i=1;i<=ask;++i)
124         {
125             int u,k;
126             sc("%d%d",&u,&k);
127             start=temp;
128             Mat ans=Mqpow(k,n);
129             for(int j=1;j<=n;++j)
130                 pr("%lld ",ans.mat[u][j]);
131             pr("\n");
132         }
133     }
134     return 0;
135 }
136 
137 /**************************************************************************************/

 

posted @ 2020-02-08 20:19  ZMWLxh  阅读(247)  评论(0编辑  收藏  举报