【2018徐州网络赛】Morgana Net (矩阵快速幂)

给你一个n*n的矩阵A,和一个m*m的矩阵B(m%2==1)

B是卷积核,让你用B对A做t次卷积运算,并且对于A中的每一个元素计算出来的值要模2,所以A最后会是一个01矩阵。

问你经过t此后,A中有多少个元素=1

1<=t<=1e9,1<=n<=8,1<=m<=n

SOLUTION:

二维矩阵展成1维

那么做法就是把A矩阵变成一个1*(n*n)的一维向量,然后构造一个(n*n)*(n*n)的辅助矩阵

我们观察到对于A中的每一个元素,每一次卷积运算,所要求乘积的值的位置是固定的,那么我们就提前预处理出

因为值只有0和1,所以可以用bitset加速

CODE:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <bitset>
using namespace std;

typedef long long ll;
const int MOD = 2;
const int MAXN = 40000;

typedef struct {

    bitset<80> m[80];
    int sizx,sizy;

}Matrix;
int n,m;
Matrix cb;
inline Matrix Mul(Matrix a, Matrix b)
{
    Matrix c;

    c.sizx=a.sizx;
    c.sizy=b.sizy;
    cb.sizy=b.sizx;
    cb.sizx=b.sizy;
    for(int i=0;i<b.sizy;i++)
    {
        cb.m[i].reset();
        for(int j=0;j<b.sizx;j++)
        {
            cb.m[i][j]=b.m[j][i];
        }
    }
    for (int i = 0; i < a.sizx; i++)
    {
        c.m[i].reset();
        for (int j = 0; j < b.sizy; j++)
        {
            bitset<80> tmp=(a.m[i]&cb.m[j]);
            c.m[i][j]=tmp.count()&1;
        }
    }
    return c;
}

inline Matrix fastm(Matrix a, ll num)
{
    Matrix res;
    for(int i=0;i<a.sizx;i++) res.m[i].reset();
    res.sizx=a.sizx;
    res.sizy=a.sizy;
    for(int i=0;i<a.sizx;i++)
        res.m[i][i]=1;
    while (num)
    {
        if (num & 1)
            res = Mul(res, a);
        num >>= 1;
        a = Mul(a, a);
    }
    return res;
}

Matrix a,b,c;
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {

        int t;
        scanf("%d%d%d",&n,&m,&t);
        a.sizx=1;a.sizy=n*n;
        a.m[0].reset();
        for(int i=0;i<n*n;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            a.m[0][i]=tmp&1;
        }

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<m;j++)
            {
                int tmp;
                scanf("%d",&tmp);
                b.m[i][j]=tmp&1;
            }
        }

        c.sizx=c.sizy=n*n;
        int num=0;
        int in=0;
        m=m>>1;
        for(int i=0;i<n*n;i++) c.m[i].reset();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int p=i-m;p<=i+m;p++)
                {
                    for(int q=j-m;q<=j+m;q++)
                    {
                        in=p*n+q;
                        if(p>=0&&p<n&&q>=0&&q<n)
                        {
                            c.m[in][num]=(b.m[p-i+m][q-j+m]);
                        }

                    }

                }
                num++;
            }

        }
        c=fastm(c,t);
        a=Mul(a,c);

        printf("%d\n",a.m[0].count());
    }

}

  

原文地址:https://www.cnblogs.com/zhangbuang/p/11202620.html

时间: 2024-11-05 23:18:54

【2018徐州网络赛】Morgana Net (矩阵快速幂)的相关文章

017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 概率+矩阵快速幂

题目链接: https://nanti.jisuanke.com/t/17115 题意: 询问硬币K次,正面朝上次数为偶数. 思路: dp[i][0] = 下* dp[i-1][0] + 上*dp[i-1][1] (满足条件的) dp[i][1]= 上*dp[i-1][0] + 下*dp[i-1][1] (不满足条件的) 矩阵优化这个DP #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL m

2018徐州网络赛H. Ryuji doesn&#39;t want to study

题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1],a[2],a[3]...a[n] #include "bits/stdc++.h" using namespace std; #define ll long long const int MAXN=1e5+10; ll sum[MAXN],ans[MAXN]; ll num[MAXN];

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn&#39;t want to study

C*M....死于update的一个long long写成int了 心累 不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/31460 #include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn =

2018 徐州网络赛 H

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

2018 徐州网络赛A

262144K After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle. There are N guests Re

2018 徐州网络赛 J

131072K After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the height of the rectangle is NN and the width of the

江苏徐州邀请赛 I题 类矩阵快速幂

题意:即一个m个点的图,求走n步,所能获得的最大权值n(2e5),m(100)时间限制:2s,样例:10组思路1:dp dp[i][j]表示i步到达j所能获得的最大权值 for i=1:n for j=1:m for k=1:m dp[i][j]=max(dp[i][j],dp[i][k]+v[k][j]);复杂度为 n*m^2, 复杂度会爆的思路2:考虑下最短路中松弛的说法,我们定义为扩张for i=1:m for j=1:m for k=1:m f[i][j]=max(f[i][j],f[i

CodeChef February Challenge 2018 Broken Clock (三角函数推导 + 矩阵快速幂)

题目链接  Broken Clock   中文题面链接 令$cos(xα) = f(x)$ 根据三角函数变换公式有 $f(x) = \frac{2d}{l} f(x-1) - f(x-2)$ 令$f(x) = \frac{g(x)}{l^{x}}$ $\frac{g(x)}{l^{x}} = \frac{2d}{l} * \frac{g(x-1)}{l^{x-1}} - \frac{g(x-2)}{l^{x-2}}$ $g(x) = 2dg(x-1) - l^{2}g(x-2)$ 原文地址:ht

2018 ICPC 徐州网络赛

2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution 将环变成链,设\(f[i][0\)~\(2]\),分别表示与第一个数相同,与第一个数不同,与第一个数相同,与第一个数的反相同.然后\(dp\)即可. 时间复杂度:\(O(n)\) B. BE, GE or NE solution 根据题目描述\(dp\)即可. 时间复杂度:\(O(nm)\) C.