# 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

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

#### uva 11651 - Krypton Number System(矩阵快速幂)

2014-08-20 23:08:00

#### UVA11651Krypton Number System(DP+矩阵快速幂）

2016-11-07 20:54:54

#### UVA 11651 - Krypton Number System(DP+矩阵快速幂)

2014-08-10 10:51:56

#### Krypton控件组简介与使用

2017-11-15 16:38:12

#### ICPC2017网络赛（沈阳）1005&HDU6198 number （矩阵+快速幂）

2017-09-10 18:29:15

#### ComponentFactory.Krypton.Toolkit基于winform的皮肤类库

2013年03月11日 623KB 下载

2010-12-08 10:26:00

#### krypton toolkit简介

2011-11-04 21:40:59

#### Krypton Suite 4.4.0 with Toolkit Cracked

2014年01月27日 1.84MB 下载

#### C#免费窗体控件Krypton Toolkit 4.2.0的使用

2010-08-14 17:07:00