2014年第五届蓝桥杯Java程序设计本科B组决赛 幂一矩阵(编程大题)

2014年第五届蓝桥杯Java程序设计本科B组决赛个人题解汇总:

https://blog.csdn.net/daixinliangwyx/article/details/89948727

第五题

标题:幂一矩阵

交题测试链接:https://www.dotcpp.com/oj/problem1823.html

    天才少年的邻居 atm 最近学习了线性代数相关的理论,他对“矩阵”这个概念特别感兴趣。矩阵中有个概念叫做幂零矩阵。对于一个方阵 M ,如果存在一个正整数 k 满足 M^k = 0 ,那么 M 就是一个幂零矩阵。(^ 表示乘方)

    atm 不满足幂零矩阵,他自己设想了一个幂一矩阵:对于一个方阵 M ,如果存在一个正整数 k 满足 M^k = I ,其中 I 是单位矩阵,那么 M 就是一个幂一矩阵。

    atm 特别钟情于这样一种方阵:每行每列有且仅有一个 1 。经过 atm 不断实验,他发现这种矩阵都是幂一矩阵。

    现在,他的问题是,给定一个满足以上条件的方阵,他想求最小的 k 是多少。

【输入格式】
第一行一个正整数 n ,表示矩阵大小是 n * n 。
接下来 n 行,每行两个正整数 i j 表示方阵的第 i 行第 j 列为 1。
1 <= i, j <= n 。
行号,列号都从1开始。

【输出格式】
一行。一个正整数,即题目中所说最小的 k 。

【样例输入】
5
3 1
1 2
4 4
2 3
5 5

【样例输出】
3

【数据范围】
对于 30% 的数据满足 n <= 10 
对于 60% 的数据答案不超过 10^18 
对于 100% 的数据满足 n <= 10000


资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗  < 1000ms


请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。

所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。


参考了这篇博客:https://blog.csdn.net/qq_26358509/article/details/71056436

得知题目给的起始矩阵即幂一矩阵除了1其余都是0,那么一个矩阵要通过自乘变成单位矩阵(第i行第i行的值为1),通过手动运算几次会发现,每进行一次自乘,原矩阵有1的位置为2 3和3 1,那么自乘后有1的位置就是2 1,也就是说我们需要遍历每一行,查找哪些行进行运算后有1的位置会变成i i,并统计次数,则说明这些参与运算的行经过统计次数后是会变成i i位置为1的,因为要使所有行同时i i位置变为1,因此要算所有统计次数的最小公倍数,这就是答案了。

坑点:上面参考博客的代码会答案错误25%,这是因为数据范围的原因,光开long是不够的,算最小公倍数时两数相乘是会溢出long的,因此对于涉及最小公倍数运算的变量应该都开BigInteger才够。

代码:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static InputReader in = new InputReader(new BufferedInputStream(System.in));
    public static PrintWriter out = new PrintWriter(System.out);
    public static int n, r, c, now;
    public static int[] column = new int[10010];
    public static int[] vis = new int[10010];

    public static void main(String[] args) {
        n = in.nextInt();
        for (int i = 0; i < n; i++) {
            r = in.nextInt();
            c = in.nextInt();
            column[r] = c;
        }
        BigInteger ans = BigInteger.ONE;
        for (int i = 1; i <= n; i++) {
            if (vis[i] == 1) continue;
            now = i;
            BigInteger tmp = BigInteger.ONE;
            while (column[now] != i) {
                tmp = tmp.add(BigInteger.ONE);
                vis[now] = 1;
                now = column[now];
            }
            ans = gld(ans, tmp);
        }
        out.println(ans);
        out.flush();
        out.close();
    }

    static BigInteger gcd(BigInteger m, BigInteger n) {
        return m.mod(n) == BigInteger.ZERO ? n : gcd(n, m.mod(n));
    }

    static BigInteger gld(BigInteger m, BigInteger n) {
        return (m.multiply(n)).divide(gcd(m, n));
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }

        public BigDecimal nextBigDecimal() {
            return new BigDecimal(next());
        }

    }
}

评测结果:

猜你喜欢

转载自blog.csdn.net/daixinliangwyx/article/details/90050195