阿里笔试AK(9月9日)

T2 四元环形构造

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] a = new int[n + 1];

        if (n % 4 == 0 || n % 4 == 1) {
            for (int i = 1; i <= n / 2; i += 2) {
                a[i] = i + 1;
                a[i + 1] = n - i + 1;
                a[n - i + 1] = n - i;
                a[n - i] = i;
            }
            if (n % 2 == 1) {
                a[n / 2 + 1] = n / 2 + 1;
            }

            for (int i = 1; i <= n; ++i) {
                System.out.print(a[i] + " ");
            }
        } else {
            System.out.print(-1);
        }

        scanner.close();
    }
}

T3 组合数学

import java.util.Scanner;

public class Main {
    static int Mod = 1000000007;
    static int cmbN = 1000000;
    static long[] cmbfact = new long[cmbN + 1];
    static long[] cmbfactinv = new long[cmbN + 1];
    static long[] cmbinv = new long[cmbN + 1];
    public static long quickPow(long a, long b) {
        long ans = 1;
        while (b > 0) {
            if ((b & 1) == 1) {
                ans = ans * a % Mod;
            }
            b >>= 1;
            a = a * a % Mod;
        }
        return ans;
    }
    public static long cmb(int n, int r) {
        if (n < r) {
            return 0;
        } else {
            return cmbfact[n] * cmbfactinv[r] % Mod * cmbfactinv[n - r] % Mod;
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        int n = s.length();


        cmbfact[0] = 1;
        cmbfactinv[0] = 1;
        cmbinv[0] = 0;
        cmbfact[1] = 1;
        cmbfactinv[1] = 1;
        cmbinv[1] = 1;
        for (int i = 2; i <= cmbN; i++) {
            cmbfact[i] = (cmbfact[i - 1] * i) % Mod;
            cmbinv[i] = (Mod - cmbinv[Mod % i] * (Mod / i) % Mod) % Mod;
            cmbfactinv[i] = (cmbfactinv[i - 1] * cmbinv[i]) % Mod;
        }

        long[] c = new long[26];
        for (int i = 0; i < n; i++) {
            c[s.charAt(i) - 'a']++;
        }

        long[] two = new long[26];
        long[] another = new long[26];

        for (int i = 0; i < 26; i++) {
            two[i] = cmb((int) c[i], 2);
            another[i] = (quickPow(2, c[i]) - two[i] + Mod) % Mod;
        }
        long ans = 0;
        for (int i = 0; i < 26; i++) {
            long count = 1;
            for (int j = 0; j < 26; j++) {
                if (i != j) {
                    count = count * another[j] % Mod;
                }
            }
            ans += count * two[i] % Mod;
            ans %= Mod;
        }

        System.out.println(ans);
    }

}

#笔试##笔试题解##阿里##阿里笔试##秋招#
全部评论

相关推荐

11-03 20:56
已编辑
门头沟学院 供应链管理
创维 需求计划方向 月薪:14k*14,80%基本工资20%绩效工资
点赞 评论 收藏
分享
10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
2 2 评论
分享
牛客网
牛客企业服务