打表枚举
import java.util.*;
public class Main {
static int p, q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
p = sc.nextInt(); q = sc.nextInt();
int res = 0;
for (int i = 1; i <= 1000; i++) {
if (!dfs(i, p, q)) res = i;
}
System.out.println(res);
}
/**
* 返回m是否可以被p和q拼凑起来
*/
static boolean dfs(int m, int p, int q) {
if (m == 0) return true;
if (m >= p && dfs(m - p, p, q)) return true;
if (m >= q && dfs(m - q, p, q)) return true;
return false;
}
}
使用公式
a和b最大不能凑出来的整数为m = (a-1) * (b - 1) - 1 = ab - a - b
import java.util.*;
public class Main {
static int p, q;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
p = sc.nextInt(); q = sc.nextInt();
System.out.println((p - 1) * (q - 1) - 1);
}
}