2012-2013 ACM-ICPC, Asia Tokyo Regional Contest

https://codeforces.com/gym/101412

C - One-Dimensional Cellular Automaton

签到题,就是直接矩阵快速幂,一开始用longlong然后到处取模导致T了一发,卡常之后才过。

测出来了,取模大概是11倍常数,鉴于大概元素的范围是2^16次方,所以认为取模是近似一个log是可以的,毕竟本质是移位和乘法和加法神奇的编译器除法优化。就算乘法也是并行的,那也是几倍常数,而且假如不开O2就直接是除法和减法,这就完蛋了。

所以说矩阵的模板要设置上限n,特别是多组数据的那种,不要每次都MAXN。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int mod;

struct Matrix {
    static const int MAXN = 50;
    int ma[MAXN][MAXN], n;
    Matrix(int _n) {
        n = _n;
        init();
    }
    void init() {
        memset(ma, 0, sizeof(ma));
    }
    void setE() {
        init();
        for(int i = 0; i < n; ++i)
            ma[i][i] = 1;
    }
    Matrix operator*(const Matrix &x)const {
        Matrix res(x.n);
        for(int k = 0; k < n; ++k) {
            for(int i = 0; i < n; ++i) {
                register int r = ma[i][k];
                for(int j = 0; j < n; ++j)
                    res.ma[i][j] += r * x.ma[k][j];
            }
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j)
                res.ma[i][j] %= mod;
        }
        return res;
    }
};

Matrix qpow(Matrix x, ll n) {
    Matrix res(x.n);
    res.setE();
    while(n) {
        if(n & 1)
            res = res * x;
        x = x * x;
        n >>= 1;
    }
    return res;
}

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    int n, m, a, b, c, t;
    while(~scanf("%d%d%d%d%d%d", &n, &m, &a, &b, &c, &t)) {
        if(n == 0 && m == 0 && a == 0 && b == 0 && c == 0 && t == 0)
            break;
        mod = m;

        Matrix F(n), A(n);
        for(int i = 0; i < n; ++i)
            scanf("%d", &F.ma[i][0]);

        for(int i = 0; i < n; ++i) {
            if(i - 1 >= 0)
                A.ma[i][i - 1] = a;
            A.ma[i][i] = b;
            if(i + 1 < n)
                A.ma[i][i + 1] = c;
        }
        A = qpow(A, t);
        F = A * F;
        for(int i = 0; i < n; ++i)
            printf("%d%c", F.ma[i][0], " \n"[i == n - 1]);
    }
}

D - Find the Outlier

原文地址:https://www.cnblogs.com/Inko/p/11616854.html

时间: 2024-11-03 21:05:54

2012-2013 ACM-ICPC, Asia Tokyo Regional Contest的相关文章

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

2013 ACM/ICPC Asia Regional Chengdu Online

题目链接: 2013 ACM/ICPC Asia Regional Chengdu Online 上年网选被虐得好惨,遂决定把题目拿回来搞一遍, 不会的题补之! A. 2013 ACM/ICPC Asia Regional Chengdu Online,布布扣,bubuko.com

hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2005    Accepted Submission(s): 563 Problem Description Little Joh

hduoj 4706 Children&amp;#39;s Day 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4706 Children's Day Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Today is Children's Day. Some children ask you to output a big letter 'N'. 'N' is c

hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1610    Accepted Submission(s): 630 Problem Description (From wikipedia) For bina

hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in th

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 (Gym - 102082)

2018-2019, ICPC, Asia Yokohama Regional Contest 2018 A - Digits Are Not Just Characters 签到. B - Arithmetic Progressions 题意:从给定的集合中选出最多的数构成等差数列. 题解:数字排序后,设\(dp[i][j]\)表示等差数列最后一个数字为\(a[i]\),倒数第二个数字为\(a[j]\)的最大个数.然后对于每一位枚举 \(i\),\(lower\_bound()\)找有无合法的

hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)

SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是赛后看的网上大神,所以转载过来了,dfs染色的时候很巧妙,巧妙的用到了就两个无向完全图 #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <qu