HDU 2294 pendant (DP + matrix Rapid power dimensionality reduction)

Source: Internet
Author: User
HDU 2294 pendant (DP + matrix Rapid power dimensionality reduction)

ACM

Address: HDU 2294 pendant

Question:
Tuhao made jewelry for his sister. He had K kinds of pearls, n of each. To show off the wealth, he needed to use each Pearl. Ask him how many pieces of jewelry with a length of [1, N] can be made.

Analysis:
1 ≤ n ≤ 1,000,000,000 is terrible.
First, consider DP. Obviously, you can think of the following:
dp[i][j] = (k-(j-1))*dp[i-1][j-1] + j*dp[i-1][j](DP [I] [J] indicates the number of pendants with I and j types of pearls)
If n is too big, you have to consider optimization. It is too difficult to use a matrix to quickly transfer power.
Reference:

Because N is too large, I is regarded as a "phase" to construct a matrix and quickly transfer it through a matrix.
Set the one-dimensional array (DP [I] [0 ~ J]) if the State is set to F and the transition matrix is INIT (k + 1 matrix), there are: init * f = f'; (f' is a new State)
Set answer = GN = DP [1] [k] + dp [2] [k] +... + dp [N] [k] So there is a matrix:

| 1 0...............0 1 |       |g0|        |g1‘| | 0 1 0...............0 |       |f1|        |f1‘| | 0 k-1 2.............0 |       |f2|        |f2‘| | ..................... |   *   |..|    =   |..‘| | 0...0 k-(j-1) j 0...0 |       |fj|        |fj‘| | ..................... |       |..|        |..‘| | 0...............0 1 k |       |fk|        |fk‘| 

Initialization of this Code:[g0, f1, f2, ..., fk] = {0, k, 0, ..., 0}

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        2294.cpp*  Create Date: 2014-08-03 16:03:27*  Descripton:   */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;#define repf(i,a,b) for(int i=(a);i<=(b);i++)typedef long long ll;const int N = 31;const int SIZE = 31;// max size of the matrixconst int MOD = 1234567891;int ans[N];int t, n, k;struct Mat{int n;ll v[SIZE][SIZE];// value of matrixMat(int _n = SIZE) {n = _n;memset(v, 0, sizeof(v));}void init(ll _v) {memset(v, 0, sizeof(v));repf (i, 0, n - 1)v[i][i] = _v;}void output() {repf (i, 0, n - 1) {repf (j, 0, n - 1)printf("%lld ", v[i][j]);puts("");}puts("");}} a, b, c;Mat operator * (Mat a, Mat b) {Mat c(a.n);repf (i, 0, a.n - 1) {repf (j, 0, a.n - 1) {c.v[i][j] = 0;repf (k, 0, a.n - 1) {c.v[i][j] += (a.v[i][k] * b.v[k][j]) % MOD;c.v[i][j] %= MOD;}}}return c;}Mat operator ^ (Mat a, ll k) {Mat c(a.n);c.init(1);while (k) {if (k&1) c = c * a;a = a * a;k >>= 1;}return c;}void init() {scanf("%d%d", &n, &k);a.n = b.n = c.n = k + 1;a.init(0);a.v[0][0] = a.v[0][k] = 1;repf (i, 1, k) {if (i > 1)a.v[i][i - 1] = k - i + 1;a.v[i][i] = i;}b.init(0);b.v[1][0] = k;}void solve() {c = a ^ n;c = c * b;printf("%lld\n", c.v[0][0]);}int main() {scanf("%d", &t);while (t--) {init();solve();}return 0;}


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.