Codeforces Round 931 (Div. 2) ---- E. Weird LCM Operations ---- 题解

E. Weird LCM Operations:

题目大意:

思路解析:

这是一道构造题,那么观察这个构造有啥性质,观察到最多操作次数为 n/6 + 5,然后每次操作需要选择三个数,如果每次操作的三个数都不和之前的重复的话,应该至少需要n/3次,那么容易想到,每次操作应该只需要操作后半部分,且每次操作之间选择的数不重复。

证明:为什么操作后半部分是正确的,假设 n==27,可以发现 13和26本来gcd就是13了,无论26选择哪些数,反正一定会保留13这个因子,则前半部分的gcd是天然满足的。

还可以发现当 选择 13 14 15,这样的两个奇数一个偶数时,一定会同时满足这三个的gcd。

一个奇数两个偶数不行。

三个奇数也可以,但是发现一个偶数,就需要两个奇数,肯定不会有这么多额外的奇数,所以这个性质似乎没什么用

三个偶数,在这三个偶数都不含4这个因子时,也可以满足,这一下就很好的补充了,在一直使用第一个性质后剩余的偶数过多带来的麻烦。

因为第四个性质要求每次需要使用不含4这个因子的偶数,所以跨度为12,则我们需要单独处理n小于14的情况,然后其余情况利用第一个性质和第四个性质。 (这两个性质的配合使用,刚好可以组成一个循环)

代码实现:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

import static java.util.Collections.*;

public class Main {


    public static void main(String[] args) throws IOException {
        int t = f.nextInt();
        while (t > 0) {
            solve();
            t--;
        }

        w.flush();
        w.close();
        br.close();
    }


    public static void solve() {
        int n = f.nextInt();
        int[][][] pans = new int[14][][];
        pans[3]= new int[][]{{1, 2, 3}};
        pans[4]= new int[][]{{1, 3, 4}};
        pans[5]= new int[][]{{3, 4, 5}};
        pans[6]= new int[][]{{1, 3, 5}, {2, 4, 6}};
        pans[7]= new int[][]{{2, 4, 6}, {3, 5, 7}};
        pans[8]= new int[][]{{2, 6, 8}, {3, 5, 7}};
        pans[9]= new int[][]{{1, 3, 5}, {2, 4, 6}, {7, 8, 9}};
        pans[10]= new int[][]{{3, 4, 5}, {2, 6, 8}, {7, 9, 10}};
        pans[11]= new int[][]{{2, 6, 8}, {3, 5, 7}, {9, 10, 11}};
        pans[12]= new int[][]{{1, 11, 12}, {6, 8, 10}, {5, 7, 9}};
        pans[13]= new int[][]{{1, 13, 12}, {7, 9, 11}, {6, 8, 10}};
        if(n < 14){
            w.println(pans[n].length);
            for (int i = 0; i < pans[n].length; i++) {
                w.println(pans[n][i][0] + " " + pans[n][i][1] + " " + pans[n][i][2]);
            }
        }else {
            int[][] ans = new int[n / 6 + 5][3];
            int res = 0;
            int cur = n;
            if ((n - 3) % 4 == 0){
                ans[0][0] = 1; ans[0][1] = 2; ans[0][2] = n;
                res++;
                cur --;
            }else if (n % 4 == 0){
                ans[0][0] = 1; ans[0][1] = n-1; ans[0][2] = n;
                res++;
                cur-=2;
            }
            while (cur * 2 > n){
                if (cur % 2 == 0){
                    ans[res][0] = cur; ans[res][1] = cur - 4; ans[res][2] = cur - 8;
                    res++;
                    ans[res][0] = cur - 3; ans[res][1] = cur - 2; ans[res][2] = cur - 1;
                    res++;
                    ans[res][0] = cur - 7; ans[res][1] = cur - 6; ans[res][2] = cur - 5;
                    res++;
                    ans[res][0] = cur - 11; ans[res][1] = cur - 10; ans[res][2] = cur - 9;
                    res++;
                }else {
                    ans[res][0] = cur - 2; ans[res][1] = cur - 1; ans[res][2] = cur;
                    res++;
                    ans[res][0] = cur - 6; ans[res][1] = cur - 5; ans[res][2] = cur - 4;
                    res++;
                    ans[res][0] = cur - 10; ans[res][1] = cur - 9; ans[res][2] = cur - 8;
                    res++;
                    ans[res][0] = cur - 11; ans[res][1] = cur - 7; ans[res][2] = cur - 3;
                    res++;
                }
                cur -= 12;
            }
            w.println(res);
            for (int i = 0; i < res; i++) {
                w.println(ans[i][0] + " " + ans[i][1] + " "+ ans[i][2]);
            }
        }


    }


    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        public Input(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) {
                // TODO 自动生成的 catch 块
                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());
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Studying~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值