Java implements Blue Bridge Cup algorithm training Number Challenge (violent)

Test algorithm training Number Challenge

Resource limit
Time limit: 3.0s Memory limit: 512.0MB
Problem description
  Define d (n) as the number of n. Now, you have three numbers a, b, c. Your task is to calculate the value of the following formula modulo 1073741824 (2 ^ 30). Insert picture description here
Input format
  Three positive integers a, b, c.
The output format is
  a number, which is the value of the above formula modulo 1073741824 (2 ^ 30).
Sample input
2 2 2
Sample output
20
Data scale and convention
  a, b, c (1 ≤ a, b, c ≤ 2000)

 

import java.util.Scanner;

public class Main {

	// 转自:	https://blog.csdn.net/a1439775520   
	static int MAX = 2005;
	static int MOD = 1 << 30;
	static int[][] gd = new int[MAX][MAX];
	static int[] p = new int[MAX];
	static int[] mob = new int[MAX];
	static boolean[] noprime = new boolean[MAX];

	static void Mobius() {
		int pnum = 0;
		mob[1] = 1;
		for (int i = 2; i < MAX; i++) {
			if (!noprime[i]) {
				p[pnum++] = i;
				mob[i] = -1;
			}
			for (int j = 0; j < pnum && i * p[j] < MAX; j++) {
				noprime[i * p[j]] = true;
				if (i % p[j] == 0) {
					mob[i * p[j]] = 0;
					break;
				}
				mob[i * p[j]] = -mob[i];
			}
		}
	}

	static int Gcd(int a, int b) {
		if (b == 0)
			return a;
		if (gd[a][b] == 1)
			return gd[a][b];
		return gd[a][b] = Gcd(b, a % b);
	}

	static long cal(int d, int x) {
		long ans = 0;
		for (int i = 1; i <= d; i++)
			if (Gcd(i, x) == 1)
				ans += (long) (d / i);
		return ans;
	}

	public static void main(String[] args) {
		Mobius();
		int a, b, c;
		long ans = 0l;
		Scanner sc = new Scanner(System.in);
		a = sc.nextInt();
		b = sc.nextInt();
		c = sc.nextInt();
		sc.close();
		for (int i = 1; i <= a; i++)
			for (int j = 1; j <= Math.min(b, c); j++)
				if (Gcd(i, j) == 1)
					ans = (ans % MOD + (long) (a / i) * mob[j] * cal(b / j, i) * cal(c / j, i) % MOD) % MOD;
		System.out.println(  ans);
	}

}

1794 original articles published · 30,000 likes + · 4.49 million views

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105488641