hdu 5318 The Goddess Of The Moon(矩陣快速冪),hdu5318

來源:互聯網
上載者:User

hdu 5318 The Goddess Of The Moon(矩陣快速冪),hdu5318

題目連結:hdu 5318 The Goddess Of The Moon


將50個串處理成50*50的矩陣,注意重複串。


#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int maxn = 55;const int mod = 1e9+7;int N, M, A[maxn];struct Mat {int s[maxn][maxn];Mat () {memset(s, 0, sizeof(s));}Mat operator * (const Mat& b) {Mat ret;for (int k = 0; k < N; k++) {for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++)ret.s[i][j] = (ret.s[i][j] + 1LL * s[i][k] * b.s[k][j] % mod) % mod;}}return ret;}};void init () {scanf("%d%d", &N, &M);for (int i = 0; i < N; i++)scanf("%d", &A[i]);sort(A, A + N);N = unique(A, A + N) - A;}bool judge (int a, int b) {char p[15], q[15];sprintf(p, "%d", a);sprintf(q, "%d", b);int pp = strlen(p), qq = strlen(q);for (int i = 0; i < pp; i++) {int k = 0;while (i + k < pp && k < qq && p[i+k] == q[k])k++;if (i + k == pp && k > 1)return true;}return false;}Mat solve () {Mat ret;for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++) {if (judge(A[i], A[j]))ret.s[i][j] = 1;}}/*for (int i = 0; i < N; i++) {for (int j = 0; j < N; j++)printf("%d ", ret.s[i][j]);printf("\n");}*/return ret;}Mat pow_mat(Mat x, int n) {Mat ret;for (int i = 0; i < N; i++)ret.s[i][i] = 1;while (n) {if (n&1)ret = ret * x;x = x * x;n >>= 1;}return ret;}int main () {int cas;scanf("%d", &cas);while (cas--) {init();if (N == 0 || M == 0) {printf("0\n");continue;}Mat x = solve();Mat ret = pow_mat(x, M-1);int ans = 0;for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)ans = (ans + ret.s[i][j]) % mod;printf("%d\n", ans);}return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.