BZOJ 1172 Balkan2007 Dream

提示:
1. 一个乘积如果要判断整除 k ,我们需要记录哪些量
2. 每一行最多取两个可以单独抽出来做 dp

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <map>

using namespace std;
const int maxn = 210;

int n , m , k , l , cnt;
int d[maxn][maxn] , t[maxn] , now[maxn] , g[maxn][11000];
map<int , int> dic; int redic[maxn] , trans[maxn][maxn];

int main(int argc, char *argv[]) {

    cin>>n>>m>>k>>l;
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d" , g[i] + j);

    for(int i=1;i<=k;i++) if(k % i == 0) redic[++cnt] = i , dic[i] = cnt;
    for(int i=1;i<=cnt;i++) for(int j=1;j<=cnt;j++) trans[i][j] = dic[__gcd((int)((1LL*redic[i]*redic[j])%k) , k)];

    d[0][1] = 1;
    for(int i=1;i<=n;i++)
    {
        memset(t , 0 , sizeof t);
        for(int j=1;j<=m;j++) t[dic[__gcd(g[i][j] , k)]]++;

        memcpy(now , t , sizeof t);
        if(1 < i && i < n) 
        {
            for(int j=1;j<=cnt;j++) for(int k=1;k<=cnt;k++) (now[trans[j][k]] += t[j]*t[k]) %= l;
            for(int j=1;j<=cnt;j++) (now[trans[j][j]] -= t[j]) %= l;
        }

        for(int j=1;j<=cnt;j++) for(int k=1;k<=cnt;k++) (d[i][trans[j][k]] += d[i-1][j]*now[k]) %= l;
    }

    cout<<(d[n][cnt]+l)%l<<endl;
    return 0;
}

di 记录目前 gcd(,k)==i 的方案数 , i 这个值的数量级是102级别的
那么我们就可以把相同 gcd 值的数归类,那么整体复杂度就降到了 106 级别。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值