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

题目链接:uva 11651 - Krypton
Number System

题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和。

解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef unsigned long long ll;
const int maxn = 155;
const ll MOD = 1ll<<32;

struct Mat {
    int r, c;
    ll arr[maxn][maxn];

    Mat (int r = 0, int c = 0) { set(r, c); }

    void set(int r, int c) {
        this->r = r;
        this->c = c;
        memset(arr, 0, sizeof(arr));
    }

    Mat operator * (const Mat& u) {
        Mat ret(r, u.c);
        for (int k = 0; k < c; k++) {
            for (int i = 0; i < r; i++) {
                if (arr[i][k] == 0)
                    continue;

                for (int j = 0; j < u.c; j++)
                    ret.arr[i][j] = (ret.arr[i][j] + arr[i][k] * u.arr[k][j]) % MOD;
            }
        }
        return ret;
    }
};

int base, N;
ll dp[maxn][maxn], score;

void init () {
    scanf("%d%llu", &base, &score);

    N = (base-1) * (base-1);
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= N; i++)
        dp[0][i] = 1;

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < base; j++) {
            for (int k = 0; k < base; k++) {
                int f = (j - k) * (j - k);

                if (i + f > N || f == 0)
                    continue;

                dp[i+f][j] = (dp[i+f][j] + dp[i][k]) % MOD;
            }
        }
    }
}

Mat change () {
    Mat ret(N*base, 1);

    for (int i = 1; i <= N; i++)
        for (int j = 0; j < base; j++)
            ret.arr[(i-1)*base+j][0] = dp[i][j];
    return ret;
}

Mat build () {
    int n = N * base;
    Mat x(n, n);

    for (int i = base; i < n; i++)
        x.arr[i-base][i] = 1;

    for (int i = 0; i < base; i++) {
        for (int j = 0; j < base; j++) {
            if (i == j)
                continue;
            int k = N - (i-j) * (i-j);
            x.arr[(N-1)*base+i][k*base+j] = 1;
        }
    }
    return x;
}

Mat pow_mat (Mat ret, int n) {
    Mat x = build();
    while (n) {
        if (n&1)
            ret = x * ret;
        x = x * x;
        n >>= 1;
    }
    return ret;
}

ll solve () {

    ll ans = 0;
    if (score <= N) {
        for (int i = 1; i < base; i++)
            ans = (ans + dp[score][i]) % MOD;
        return ans;
    }

    Mat ret = change();
    ret = pow_mat(ret, score-N);

    for (int i = 1; i < base; i++)
        ans = (ans + ret.arr[(N-1)*base+i][0]) % MOD;
    return ans;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        init();
        printf("Case %d: %llu\n", kcas, solve());
    }
    return 0;
}

uva 11651 - Krypton Number System(矩阵快速幂),布布扣,bubuko.com

时间: 2024-12-29 05:40:00

uva 11651 - Krypton Number System(矩阵快速幂)的相关文章

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

UVA 11651 - Krypton Number System 题目链接 题意:给一个进制base,一个分数score求该进制下,有多少数满足一下条件: 1.没有连续数字 2.没有前导零 3.分数为score,分数的计算方式为相邻数字的平方差的和 思路:先从dp入手,dp[i][j]表示组成i,最后一个数字为j的种数,然后进行状态转移,推出前面一步能构成的状态,也就是到dp[(b - 1) * (b - 1)][x]. 然后可以发现后面的状态,都可以由前面这些状态统一转移出来,这样就可以利用

Uva10689 Yet another Number Sequence ( 矩阵快速幂 )

Uva 10689Yet another Number Sequence(  矩阵快速幂  ) 题意: 就是矩阵快速幂,没什么好说的. 分析: 其实还是斐波那契数列.只是最后对应的矩阵不是(1,1)是(a,b)了 MOD = 1; for( int i = 0; i < m; ++i ) MOD *= 10; 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace s

uva 10743 - Blocks on Blocks(矩阵快速幂)

题目链接:uva 10743 - Blocks on Blocks 题目大意:问说n联骨牌有多少种,旋转镜像后相同不算同一组,一行的格子必须连续,如果答案大于10000,输出后四位. 解题思路:想了一下午的递推式,实在受不了,把推出的序列在网上搜了一下,有公式ai=5?ai?1?7?ai?2+4?ai?3 (i≥5) PS:哪位神人知道怎么推出来的请留言我,大恩不言谢~ #include <cstdio> #include <cstring> #include <algori

Uva 11149 - Power of Matrix ( 矩阵快速幂 )

Uva 11149 -Power of Matrix ( 矩阵快速幂 ) #include <cstdio> #include <cstring> #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 10 #define MAX_SIZE 40 struct Mat { int r, c; int mat[MAX_SIZE][MAX_SIZE]; Mat( int _r = 0 , int _c = 0 ) { CLR

UVA - 10689 Yet another Number Sequence 矩阵快速幂

                  Yet another Number Sequence Let’s de?ne another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values

UVA - 10229 - Modular Fibonacci (矩阵快速幂 + fibonacci)

题目传送:UVA - 10229 思路:就是简单的矩阵快速幂求fibonacci数列,然后注意可能中间结果会爆int,因为2^19有50多万 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #includ

zoj3690--Choosing number(dp,矩阵快速幂)

Choosing number Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description There are n people standing in a row. And There are m numbers, 1.2...m. Every one should choose a number. But if two persons standing

HDU 1005 Number Sequence 矩阵快速幂

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 236241    Accepted Submission(s): 60070 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))(A + A^2 + A^3 + ... + A^(m/2)),然后依次计算下去,就可以分解,logn的复杂度分解,注意要分奇偶. 另一种是直接构造矩阵,,然后就可以用辞阵快速幂计算了,注意要用分块矩阵的乘法. 代码如下: 倍增法: #pragma comment(linker, "/STACK:10