Gym-101502B Linear Algebra Test

本文介绍了一种解决矩阵乘法对计数问题的方法。利用map数据结构存储矩阵的行数与列数,通过查找匹配的列数和行数来统计能够进行矩阵乘法操作的矩阵对数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gym-101502B Linear Algebra Test


题目链接

题意:
给出n个矩阵的行数与列数,问有多少对矩阵可以相乘。

做法:
矩阵可以相乘的条件是,前一个矩阵的列数=后一个的行数。
我们只要分别用一个数组,保存行数a[]与列数b[],遍历b[],对b[]中每一个数,遍历a[],找到可以配对的行数个数,累加。

然而这样做是会超时的,1≤n≤10^5。

因为1≤ai,bi≤10^9,即行数与列数的范围,我想到建立一个10^9的数组a[],a[i]中保存的是行数为i的矩阵个数,保存所有列数至b[]。遍历b[],直接a[b[i]],累加。

可是我发现不能够用10^9的数组(win10, codeblocks16.01),说数组太大。然后,我就用了两个500000005的数组,哈哈。

但是,超内存了。

最后,我们用map(映射)优雅的解决了这个问题,思路差不多。

(1)ac代码

#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;

int
main() {
    int t, n, i, j, a[2][100005];
    long long ans;

    map <int, int> m;

    cin >> t;
    while( t-- ) {
        m.clear();  //
        cin >> n;
        for( j = 0; j < n; j++ ) {
            for( i = 0; i < 2; i++ ) {
                scanf("%d", &a[i][j]);  // cin会超时
            }
        }
        for( j = 0; j < n; j++ ) {
            m[a[0][j]]++;
            //cout << a[0][j] << endl;
        }
        //cout << endl;
        ans = 0;
        for( j = 0; j < n; j++ ) {
            ans += m[a[1][j]];
            //cout << a[1][j] << endl;
        }
        cout << ans << endl;
    }

    return 0;
}

(2)超内存,初始版本

#include <stdio.h>
#include <string.h>

int a[100000005], b[100005];

int
main() {
    int t, n, e, i, ans;

    scanf("%d", &t);
    while( t-- ) {
        ans = 0;
        memset(a, 0, sizeof(a));
        scanf("%d", &n);
        for( i = 0; i < n; i++ ) {
            scanf("%d %d", &e, &b[i]);
            a[e]++;
        }
        for( i = 0; i < n; i++ ) {
            ans += a[b[i]];
        }
        printf("%d\n", ans);
    }

    return 0;
}

(3)超内存/编译错误,两个数组

#include <stdio.h>
#include <string.h>

int a1[500000005], a2[500000005], b[100005];

int
main() {
    int t, n, e, i, ans;

    scanf("%d", &t);
    while( t-- ) {
        ans = 0;
        memset(a1, 0, sizeof(a1));
        memset(a2, 0, sizeof(a2));
        scanf("%d", &n);
        for( i = 0; i < n; i++ ) {
            scanf("%d %d", &e, &b[i]);
            if( e > 500000000 ) {
                a2[e % 500000000]++;
            }
            else {
                a1[e]++;
            }
        }
        for( i = 0; i < n; i++ ) {
            if( b[i] > 500000000 ) {
                ans += a2[b[i] % 500000000];
            }
            else {
                ans += a1[b[i]];
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}

(4)超时

#include <stdio.h>
#include <string.h>

int a[100005], b[100005];

int
main() {
    int t, n, i, j, ans;

    scanf("%d", &t);
    while( t-- ) {
        ans = 0;
        scanf("%d", &n);
        for( i = 0; i < n; i++ ) {
            scanf("%d %d", &a[i], &b[i]);
        }
        for( i = 0; i < n; i++ ) {
            for( j = 0; j < n; j++ ) {
                if( a[i] == b[j] ) {
                    ans++;
                }
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}
About the Author David C. Lay holds a B.A. from Aurora University (Illinois), and an M.A. and Ph.D. from the University of California at Los Angeles. David Lay has been an educator and research mathematician since 1966, mostly at the University of Maryland, College Park. He has also served as a visiting professor at the University of Amsterdam, the Free University in Amsterdam, and the University of Kaiserslautern, Germany. He has published more than 30 research articles on functional analysis and linear algebra. As a founding member of the NSF-sponsored Linear Algebra Curriculum Study Group, David Lay has been a leader in the current movement to modernize the linear algebra curriculum. Lay is also a coauthor of several mathematics texts, including Introduction to Functional Analysis with Angus E. Taylor, Calculus and Its Applications, with L. J. Goldstein and D. I. Schneider, and Linear Algebra Gems–Assets for Undergraduate Mathematics, with D. Carlson, C. R. Johnson, and A. D. Porter. David Lay has received four university awards for teaching excellence, including, in 1996, the title of Distinguished Scholar—Teacher of the University of Maryland. In 1994, he was given one of the Mathematical Association of America’s Awards for Distinguished College or University Teaching of Mathematics. He has been elected by the university students to membership in Alpha Lambda Delta National Scholastic Honor Society and Golden Key National Honor Society. In 1989, Aurora University conferred on him the Outstanding Alumnus award. David Lay is a member of the American Mathematical Society, the Canadian Mathematical Society, the International Linear Algebra Society, the Mathematical Association of America, Sigma Xi, and the Society for Industrial and Applied Mathematics. Since 1992, he has served several terms on the national board of the Association of Christians in the Mathematical Sciences. Steven R. Lay began his teaching career at Aurora University (Illinois) in 1971, after earning an M.A. and a Ph.D. in mathematics from the University of California at Los Angeles. His career in mathematics was interrupted for eight years while serving as a missionary in Japan. Upon his return to the States in 1998, he joined the mathematics faculty at Lee University (Tennessee) and has been there ever since. Since then he has supported his brother David in refining and expanding the scope of this popular linear algebra text, including writing most of Chapters 8 and 9. Steven is also the author of three college-level mathematics texts: Convex Sets and Their Applications, Analysis with an Introduction to Proof, and Principles of Algebra. In 1985, Steven received the Excellence in Teaching Award at Aurora University. He and David, and their father, Dr. L. Clark Lay, are all distinguished mathematicians, and in 1989 they jointly received the Outstanding Alumnus award from their alma mater, Aurora University. In 2006, Steven was honored to receive the Excellence in Scholarship Award at Lee University. He is a member of the American Mathematical Society, the Mathematics Association of America, and the Association of Christians in the Mathematical Sciences. Judi J. McDonald joins the authorship team after working closely with David on the fourth edition. She holds a B.Sc. in Mathematics from the University of Alberta, and an M.A. and Ph.D. from the University of Wisconsin. She is currently a professor at Washington State University. She has been an educator and research mathematician since the early 90s. She has more than 35 publications in linear algebra research journals. Several undergraduate and graduate students have written projects or theses on linear algebra under Judi’s supervision. She has also worked with the mathematics outreach project Math Central http://mathcentral.uregina.ca/ and continues to be passionate about mathematics education and outreach. Judi has received three teaching awards: two Inspiring Teaching awards at the University of Regina, and the Thomas Lutz College of Arts and Sciences Teaching Award at Washington State University. She has been an active member of the International Linear Algebra Society and the Association for Women in Mathematics throughout her career and has also been a member of the Canadian Mathematical Society, the American Mathematical Society, the Mathematical Association of America, and the Society for Industrial and Applied Mathematics.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值