标签:
2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 50 121 123 213 132 321
86814837 797922656Hint11 111 is different with 111 11
求出邻接矩阵
然后矩阵快速幂
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (1000000007) #define MAXN (60) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} int a[MAXN]; bool conn(ll a,ll b) { while (a<b) b/=10; ll p=1; while (p*10<=a) p*=10; while (b) { if (a<b) b/=10; else if (a>b) { a-=a/p*p; p/=10; } else return abs(a)>9; } return 0; } struct M { int n,m; ll a[MAXN][MAXN]; M(int _n=0){n=m=_n;MEM(a);} M(int _n,int _m){n=_n,m=_m;MEM(a);} void mem (int _n=0){n=m=_n;MEM(a);} void mem (int _n,int _m){n=_n,m=_m;MEM(a);} friend M operator*(M a,M b) { M c(a.n); For(k,a.m) For(i,a.n) For(j,b.m) c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%F; return c; } void pri() { For(i,n) { For(j,m) cout<<a[i][j]<<' ';cout<<endl; } } void make_I(int _n) { n=m=_n; MEM(a) For(i,n) a[i][i]=1; } }A,I; M pow2(M a,ll b) { M c=I; static bool a2[1000000]; int n=0;while (b) a2[++n]=b&1,b>>=1; For(i,n) { if (a2[i]) c=c*a; a=a*a; } return c; } int n,m; int main() { // freopen("C.in","r",stdin); int T;cin>>T; while(T--) { scanf("%d%d",&n,&m); For(i,n) scanf("%d",&a[i]); sort(a+1,a+1+n); n=unique(a+1,a+1+n)-(a+1); if (m==0) { cout<<"1"<<endl; continue; } I.make_I(n); A.mem(n); For(i,n) For(j,n) A.a[i][j]=conn(a[i],a[j]); // A.pri(); A=pow2(A,m-1); ll ans=0; For(i,n) For(j,n) upd(ans,A.a[i][j]); printf("%lld\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5318(The Goddess Of The Moon-矩阵乘法)[Template:矩阵]
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/47781543