HDU 5690 All X
矩阵快速幂
a[i]=a[i-1]*10+x; 问a[m]%k是否等于c
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long x,m,ans,MOD; struct Matrix { long long A[5][5]; int R, C; Matrix operator*(Matrix b); }; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b) { Matrix c; memset(c.A, 0, sizeof(c.A)); int i, j, k; for (i = 1; i <= R; i++) for (j = 1; j <= b.C; j++) for (k = 1; k <= C; k++) c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % MOD) % MOD; c.R = R; c.C = b.C; return c; } void init() { memset(X.A, 0, sizeof X.A); memset(Y.A, 0, sizeof Y.A); memset(Z.A, 0, sizeof Z.A); Y.R = 3; Y.C = 3; for (int i = 1; i <= 3; i++) Y.A[i][i] = 1; X.R = 3; X.C = 3; X.A[1][1]=10; X.A[2][2]=10; X.A[3][1]=1; X.A[3][2]=1; X.A[3][3]=1; Z.R = 1; Z.C = 3; Z.A[1][1]=0; Z.A[1][2]=x; Z.A[1][3]=x; } void read() { scanf("%lld%lld%lld%lld",&x,&m,&MOD,&ans); } void work() { while (m) { if (m % 2 == 1) Y = Y*X; m = m >> 1; X = X*X; } Z = Z*Y; if(Z.A[1][1]==ans) printf("Yes\n"); else printf("No\n"); } int main() { int Case=1; int T; scanf("%d", &T); while (T--) { read(); init(); printf("Case #%d:\n",Case++); work(); } return 0; }