Ultraviolet A 10743-blocks on blocks (matrix power)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10743-blocks on blocks

Question: How many types of N-bone cards are there? After images are rotated, they are not in the same group. The grids in a row must be continuous. If the answer is greater than 10000, the last four digits are output.

Solution: After thinking about the recursive formula for one afternoon, I really couldn't stand it. I searched the launched sequence online. Is there a formula for AI = 5? AI? 1? 7? AI? 2 + 4? AI? 3 (I ≥ 5)

PS: if anyone knows how to launch the product, please leave me a message. Thank you ~

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 3;const int mod = 10000;int sign[maxn][maxn] = { {0, 1, 0}, {0, 0, 1}, {4, -7, 5} };struct MAT {    int s[maxn][maxn];    MAT () {}    void set () {        for (int i = 0; i < maxn; i++)            for (int j = 0; j < maxn; j++)                s[i][j] = sign[i][j];    }    MAT operator * (const MAT& a) {        MAT ans;        for (int i = 0; i < maxn; i++) {            for (int j = 0; j < maxn; j++) {                ans.s[i][j] = 0;                for (int k = 0; k < maxn; k++)                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * a.s[k][j] % mod) % mod;            }        }        return ans;    }};MAT pow_mat (int n) {    MAT ans, x;    ans.set();    x.set();    while (n) {        if (n&1)            ans = ans * x;        x = x * x;        n >>= 1;    }    return ans;}int solve (int n) {    if (n < 3)        return n;    else if (n == 3)        return 6;    else if (n == 4)        return 19;    MAT ans = pow_mat(n-5);    return ((ans.s[2][0] * 2 + ans.s[2][1] * 6 + ans.s[2][2] * 19) % mod + mod) % mod;}int main () {    int cas, n;    scanf("%d", &cas);    for (int i = 1; i <= cas; i++) {        scanf("%d", &n);        printf("Case %d: ", i);        int ans = solve(n);        if (n <= 9)            printf("%d\n", ans);        else            printf("%04d\n", ans);    }    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.