Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks (Matrix optimized DP)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: The number of N as a block, there is a B block, from which a number of options, each block can only select one number, and finally constitute a number modulo x equals K method number.

Idea: It is easy to think of such a DP equation: using DP[I][J] to represent the present I-bit, the remainder is J. Then Dp[i + 1][(J * + K)% x] = dp[i][j] * Cnt[k],k refers to which of the enumerations to put 1~9.

Because B is particularly large, obviously to matrix optimization, know the DP equation, in fact, the structure of the matrix is particularly simple. According to the rules of matrix multiplication, in fact this matrix is a[j][(J*10+a[k])%x]++, where A[k] is the K from the 1~n of each number.

In fact, it is very regular, the key is to construct a good DP equation.

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long Long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int INF = 10000 00000;const int mod = 1000000000 + 7;const int maxn = 50000 + 10;int t,n,q,u,bb,k,m,x,y,a[maxn];typedef vector<int>    Vec;typedef vector<vec> Mat;mat Mul (Mat &a, Mat &b) {Mat C (a.size (), VEC (A.size ())); for (int i=0;i<x;i++) {for (int. k=0;k<x;k++) {for (int j=0;j<x;j++) {C[i][j] = (            (ll) C[i][j] + (LL) a[i][k] * b[k][j])% MoD; }}} return C;} Mat Pow (Mat A, ll k) {Mat B (a.size (), VEC (a.size()));    for (int i=0;i<x;i++) {B[i][i] = 1;        } while (k > 0) {if (K & 1) b = Mul (b, a);        A = Mul (A, a);    K >>= 1; } return B;        int main () {while (~scanf ("%d%d%d%d", &n,&bb,&k,&x)) {Mat A (x+3, VEC (x+3));        for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);            } for (int i=0;i<x;i++) {for (int j=1;j<=n;j++) {a[i][(I * + a[j])% x]++;        }} A = Pow (A, BB);    printf ("%d\n", A[0][k]); } return 0;}


Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks (Matrix optimized DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.