# UVA11651 Krypton Number System【矩阵快速幂】

79人阅读 评论(0)

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

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



个人资料
等级：
访问量： 108万+
积分： 2万+
排名： 353
文章分类
博客专栏
 ACM题解 文章：590篇 阅读：403999 趣味程序 文章：40篇 阅读：34221 大学计算机读本 文章：14篇 阅读：19815 基础程序 文章：15篇 阅读：14006 CCF认证题解 文章：45篇 阅读：142030 Project Euler问题程序解 文章：39篇 阅读：16001 NOIP赛题解 文章：19篇 阅读：9487
最新评论