UVA11651 Krypton Number System【矩阵快速幂】

本文探讨了《超人》中Krypton星球独特的数字系统,该系统拥有未知基数且排除了相邻数字相同的数。通过矩阵运算,计算了特定基数下能达到指定得分的合法数字数量。

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值