海岛Blog

海是无垠的,岛在海中是独立的,有一片自己的天空

UVA11651 Krypton Number System【矩阵快速幂】

All of you must have heard the name of the planet Krypton. If you can’t remember the planet, don't worry. Planet Krypton is the origin of Superman, that means the mother planet where Superman was born. Superman was sent to earth by his parents when the planet was about to explode. Legends say that only few people of the planet survived from that explosion.

  The inhabitants of the planet is called 'Kryptonians'. Kryptonians, though otherwise completely human, were superior both intellectually and physically to natives of Earth. One of the most common differences is their number system. The number system is denoted below

      1) The base of the number system is unknown, but legends say that the base lies between 2 and 6.

      2) Kryptinians don’t use a number where two adjacent digits are same. They simply ignore these numbers. So, 112 is not a valid number in Krypton.

      3) Numbers should not contain leading zeroes. So, 012 is not a valid number.

      4) For each number, there is a score. The score can be found by summing up the squares of differences of adjacent numbers. For example 1241 has the score of

(1 − 2)^2 + (2 − 4)^2 + (4 − 1)^2 = 1 + 4 + 9 = 14.

      5) All the numbers they use are integers.

  Now you are planning to research on their number system. So, you assume a base and a score. You have to find, how many numbers can make the score in that base.

Input

The first line of the input will contain an integer T (≤ 200), denoting the number of cases. Then T cases will follow.

  Each case contains two integers denoting base (2 ≤ base ≤ 6) and score (1 ≤ score ≤ 10^9). Boththe integers will be given in decimal base.

Output

For each case print the case number and the result modulo 232. Check the samples for details. Both the case number and result should be reported in decimal base

Sample Input

2

6 1

5 5

Sample Output

Case 1: 9

Case 2: 80


问题链接UVA11651 Krypton Number System

问题简述:(略)

问题分析:占个位置,不解释

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA11651 Krypton Number System */

#include <bits/stdc++.h>

using namespace std;

const int BASE = 6;
const int N = BASE * (BASE - 1) * (BASE - 1);

struct Matrix
{
    unsigned int n, m, g[N][N];
    Matrix(int _n, int _m)
    {
        n = _n;
        m = _m;
        memset(g, 0, sizeof(g));
    }

    // 矩阵相乘
    Matrix operator * (const Matrix& y)
    {
        Matrix z(n, y.m);

        for(unsigned int i=0; i<n; i++)
            for(unsigned int j=0; j<y.m; j++)
                for(unsigned int k=0; k<m; k++)
                z.g[i][j] += g[i][k] * y.g[k][j];

        return z;
    }
};

// 矩阵模幂
Matrix Matrix_Powmul(Matrix x, int m)
{
    Matrix z(x.n, x.n);

    for(unsigned int i=0; i<x.n; i++)
        z.g[i][i] = 1;
    while(m) {
        if(m & 1)
            z = z * x;
        x = x * x;
        m >>= 1;
    }

    return z;
}

int main()
{
    int t, base, score;

    scanf("%d", &t);
    for(int caseno=1; caseno<=t; caseno++) {
        scanf("%d%d", &base, &score);

        int n = base * (base - 1) * (base - 1);

        Matrix a(1, n);
        for(int i=1; i<base; i++)
            a.g[0][n - i] = 1;

        Matrix f(n, n);
        for(int i=base; i<n; i++)
            f.g[i][i - base] = 1;
        for(int i=0; i<base; i++)
            for(int j=0; j<base; j++)
                if(i != j)
                    f.g[n - (i - j) * (i - j) * base + j][n - base + i] = 1;

        a = a * Matrix_Powmul(f, score);

        unsigned int ans = 0;
        for(int i=1; i<=base; i++)
            ans += a.g[0][n - i];

        printf("Case %d: %u\n", caseno, ans);
    }

    return 0;
}



阅读更多
所属专栏: ACM题解
想对作者说点什么? 我来说一句

Krypton Suite 4.4.0 with Toolkit Cracked

2014年01月27日 1.84MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭