#include #include #include using namespace std; long long cannot_from00_flat[1024][1024]; long long cannot_from00_diag[1024][1024]; long long cannot_fromall[1024][1024]; long long gcd(long long a, long long b) { if (b == 0LL) { return a; } return gcd(b, a%b); } void count_invalid_triangles() { for (int i = 0; i < 1024; i++) { for (int j = 0; j < 1024; j++) { if (i == 0 && j == 0) { cannot_from00_diag[i][j] = 0LL; } else if (i == 0) { cannot_from00_diag[i][j] = 0LL; } else if (j == 0) { cannot_from00_diag[i][j] = 0LL; } else { cannot_from00_diag[i][j] = gcd(i, j) - 1 + cannot_from00_diag[i-1][j] + cannot_from00_diag[i][j-1] - cannot_from00_diag[i-1][j-1]; } } } /* for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { cout << setw(4) << cannot_from00_diag[i][j]; } cout << endl; } cout << endl; */ for (int i = 0; i < 1024; i++) { for (int j = 0; j < 1024; j++) { if (i == 0 && j == 0) { cannot_from00_flat[i][j] = 0LL; } else if (i == 0) { cannot_from00_flat[i][j] = j-1 + cannot_from00_flat[i][j-1]; } else if (j == 0) { cannot_from00_flat[i][j] = i-1 + cannot_from00_flat[i-1][j]; } else { cannot_from00_flat[i][j] = cannot_from00_flat[i-1][j] + cannot_from00_flat[i][j-1] - cannot_from00_flat[i-1][j-1]; } } } /* for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { cout << setw(4) << cannot_from00_flat[i][j]; } cout << endl; } cout << endl; */ for (int i = 0; i < 1024; i++) { for (int j = 0; j < 1024; j++) { if (i == 0 && j == 0) { cannot_fromall[i][j] = 0LL; } else if (i == 0) { cannot_fromall[i][j] = cannot_from00_flat[i][j] + cannot_fromall[i][j-1]; } else if (j == 0) { cannot_fromall[i][j] = cannot_from00_flat[i][j] + cannot_fromall[i-1][j]; } else { cannot_fromall[i][j] = cannot_from00_diag[i][j] * 2 + cannot_from00_flat[i][j] + cannot_fromall[i-1][j] + cannot_fromall[i][j-1] - cannot_fromall[i-1][j-1]; } } } /* for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { cout << setw(4) << cannot_fromall[i][j]; } cout << endl; } cout << endl; */ } int main() { ifstream fin("triangle.in"); count_invalid_triangles(); for (int cases = 1; ; ++cases) { long long m, n; fin >> m >> n; if (m == 0 && n == 0) { break; } ++m; ++n; cout << "Case " << cases << ": "; cout << ((m * n) * (m * n - 1) / 2LL * (m * n - 2) / 3LL) - cannot_fromall[m-1][n-1] << endl; } return 0; }