首页 > 代码库 > bzoj2004

bzoj2004

http://www.lydsy.com/JudgeOnline/problem.php?id=2004

矩阵快速幂。。。并没有想出来。。。

理解地不是很好 先挖个坑

技术分享
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 210, mod = 30031;
struct mat {
    int a[N][N];
} x, A;
int n, k, p, cnt;
int st[N];
mat operator * (mat A, mat B)
{
    mat ret; memset(ret.a, 0, sizeof(ret.a));
    for(int i = 1; i <= cnt; ++i)
        for(int j = 1; j <= cnt; ++j)
            for(int k = 1; k <= cnt; ++k) ret.a[i][j] = (ret.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
    return ret;        
}
mat power(mat x, int t)
{
    mat ret; memset(ret.a, 0, sizeof(ret.a));
    for(int i = 1; i <= cnt; ++i) ret.a[i][i] = 1;
    for(; t; t >>= 1, x = x * x) if(t & 1) ret = ret * x;
    return ret; 
}
void dfs(int d, int pos, int num)
{
    if(d == k) { st[++cnt] = num; return; }
    for(int i = pos; i; --i) dfs(d + 1, i - 1, num | (1 << (i - 1)));
}
int main()
{
    scanf("%d%d%d", &n, &k, &p);
    dfs(1, p - 1, 1 << (p - 1));
    for(int i = 1; i <= cnt; ++i)
        for(int j = 1; j <= cnt; ++j)
        {
            int s = st[i] ^ (st[j] << 1) ^ (1 << p);
            if(s == (s & -s)) x.a[i][j] = 1; 
        }
    x = power(x, n - k);
    A.a[1][1] = 1;
    A = x * A;
    printf("%d\n", A.a[1][1]);
    return 0;
}
View Code

 

bzoj2004