HDU 2842 Chinese Rings (with constant matrix + rapid matrix power), hdu2842

Source: Internet
Author: User

HDU 2842 Chinese Rings (with constant matrix + rapid matrix power), hdu2842
HDU 2842 Chinese Rings (with constant matrix + rapid matrix power)

ACM

Address: HDU 2842 Chinese Rings

Question:
A Chinese ring, the k ring to be unlocked first needs to be unlocked before (K-2) a ring, and leave the (k-1) ring. It takes at least a few steps to unlock n rings.

Analysis:
If f (n) is set, the n ring is unlocked.
1. Due to the game rules, the n-Ring cannot be completely unlocked at once, or n-1 n-Ring cannot be removed.
2. First remove the n: first complete f (n-2), and then remove the n ring.
3. Then put back before (n-2), in fact this is also f (n-2), because it is a inverse process.
4. Finally, f (n-1) is completed.

What about f (n-1? It's its own business...
So f (n) = f (n-2) + 1 + f (n-2) + f (n-1 ).
The first time I encountered a formula sub containing constants, the constants could have been used.

 
 
  1. | f[n] | | 1 2 1 | |f[n-2]|
  2. |f[n-1]| = | 1 0 0 | * |f[n-1]|
  3. | 1 | | 0 0 1 | | 1 |

Code:

/**  Author:      illuz <iilluzen[at]gmail.com>*  Blog:        http://blog.csdn.net/hcbbt*  File:        2842.cpp*  Create Date: 2014-08-03 22:22:57*  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 = 3;// max size of the matrixconst int MOD = 200907;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;}ll solve(int n) {if (n <= 2) {return n;}// inita.v[0][1] = 2;a.v[0][0] = a.v[0][2] = a.v[1][0] = a.v[2][2] = 1;b.v[0][0] = 2;b.v[1][0] = b.v[2][0] = 1;c = a ^ (n - 2);c = c * b;return c.v[0][0];}int main() {int n;while (~scanf("%d", &n) && n) {cout << solve(n) << endl;}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.