首页 > 其他分享 >「解题报告」AGC013E Placing Squares

「解题报告」AGC013E Placing Squares

时间:2023-04-27 20:22:28浏览次数:27  
标签:AGC013E return Matrix int Placing ans operator const Squares

想了一会然后看题解,翻到日文题解然后又关了,然后突然会了,怎么回事

第一眼生成函数!做不了。

考虑经典拆贡献方法,把平方的贡献变成从区间中选两个数的方案数。这样我们可以用一个 DP 来计数。

设 \(f_{i, j}\) 表示到了第 \(i\) 格,已经选了 \(j\) 个数的方案数。如果没有限制,那么就直接枚举下一个格子选 \(0/1/2\) 个的方案数,或者进行一次划分。转移容易写成矩阵形式。然后有限制的位置只需要把划分的转移去掉就行了,中间部分矩阵快速幂一下。

好弱智。

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005, P = 1000000007;
int n, m, a[MAXN];
struct Matrix {
    int a[3][3];
    Matrix() { memset(a, 0, sizeof a); }
    int* operator[](int b) { return a[b]; }
    const int* operator[](int b) const { return a[b]; }
    Matrix operator*(Matrix b) {
        Matrix c;
        for (int k = 0; k < 3; k++) {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % P;
                }
            }
        }
        return c;
    }
    Matrix pow(int b) {
        Matrix a = *this, ans;
        ans[0][0] = ans[1][1] = ans[2][2] = 1;
        while (b) {
            if (b & 1) ans = ans * a;
            a = a * a;
            b >>= 1;
        }
        return ans;
    }
} x, y;
int main() {
    x[0][0] = 1, x[0][1] = 2, x[0][2] = 1;
    x[1][0] = 0, x[1][1] = 1, x[1][2] = 1;
    x[2][0] = 1, x[2][1] = 2, x[2][2] = 2;
    y[0][0] = 1, y[0][1] = 2, y[0][2] = 1;
    y[1][0] = 0, y[1][1] = 1, y[1][2] = 1;
    y[2][0] = 0, y[2][1] = 0, y[2][2] = 1;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
        scanf("%d", &a[i]);
    }
    a[m + 1] = n;
    Matrix p = x.pow(a[1]);
    for (int i = 1; i <= m; i++) {
        p = p * y;
        p = p * x.pow(a[i + 1] - a[i] - 1);
    }
    printf("%d\n", p[0][2]);
    return 0;
}

标签:AGC013E,return,Matrix,int,Placing,ans,operator,const,Squares
From: https://www.cnblogs.com/apjifengc/p/17360117.html

相关文章

  • Replacing Windows Notepad with Notepad2 4.1.24 (or newer)
    ReplacingWindowsNotepadwithNotepad24.1.24(ornewer)Asofversion4.1.24,theofficialreleaseofNotepad2supportsthismethodforreplacingWindowsNotepad,sothestepsoutlinedabovewillworkfine.However,there'snosupporttoperformthe......
  • CodeForces - 610B Vika and Squares (模拟)
    CodeForces-610BVikaandSquaresTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionVikahas n jarswithpaintsofdistinctcolors.Allthejarsarenumberedfrom 1 to n andthe......
  • 深度学习导论知识——最小二乘法(Ordinary Least Squares, OLS)
    1.知识点简介最小二乘法(OrdinaryLeastSquares,OLS)是常见的估计模型参数的方法。早在19世纪,勒让德就认为按照“误差的平方和最小”这个规则估计出来的模型是最接近......
  • 1736.latest-time-by-replacing-hidden-digits 替换隐藏数字得到的最晚时间
    问题描述1736.替换隐藏数字得到的最晚时间解题思路模拟+贪心代码classSolution{public:stringmaximumTime(stringtime){stringres;/......
  • Sum of Squares Theorems
    这个在Cryptography里有用,因为对于大的数找起来很难Legendre'sthreesquaretheorem: apositiveinteger canbeexpressedasasumof4squaresifandonlyifit......
  • D. Many Perfect Squares
    D.ManyPerfectSquaresYouaregivenaset$a_1,a_2,\ldots,a_n$ofdistinctpositiveintegers.Wedefinethesquarenessofaninteger$x$asthenumberof......
  • 「解题报告」ARC142D Deterministic Placing
    好?首先我们可以发现,第一步和第三步的局面必须相等,因为第二步可以反着走回第一步,如果不相等那么下一步走的方案就不唯一了。然后我们考虑走的形式,由于是一棵树,没有环,我们......
  • Squarespace 和 WordPress 的区别
    博主前些天发现了一个巨牛巨好用的刷题网站,忍不住分享一下给大家,......
  • Clickhouse表引擎探究-ReplacingMergeTree
    作者:耿宏宇1表引擎简述1.1官方描述MergeTree系列的引擎被设计用于插入极大量的数据到一张表当中。数据可以以数据片段的形式一个接着一个的快速写入,数据片段在后台按......
  • UVA 10859 Placing Lampposts--树形dp
    原题链接:​​http://vjudge.net/problem/UVA-10859​​题意:给一个N个点M条边的无向无环图,就是树的意思,每个节点都可以放灯。每盏灯将照亮以它为一个端点的所有边,在所有边都......