在一行中输入两个正整数
(
),分别表示行下标和列下标。
输出一个整数,表示
的值。
1 1
1
根据定义,。
2 2
2
由递推可得:,
;
。
提示,取模运算对加法运算满足交换律和结合律,所以在计算过程中多次取模得到的计算结果,和全部计算都完成后得到的计算结果是相同的。
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int mod = (int) Math.pow(10, 9) + 7; int[][] nums = new int[n+1][m+1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { nums[i][j] = 0; } else if (i == 1 && j == 1) { nums[i][j] = 1; } else { nums[i][j] = (nums[i-1][j] % mod + nums[i][j-1] % mod) % mod; } } } System.out.println(nums[n][m]); in.close(); } }
#include<bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; long long a[1001][1001]; int main(){ int n, m; cin >> n >> m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(i == 1 && j == 1) a[i][j] = 1; else if(i == 1) a[i][j] = a[i][j - 1]; else if(j == 1) a[i][j] = a[i - 1][j]; else a[i][j] = (a[i - 1][j] + a[i][j - 1]) % MOD; } } cout << a[n][m] % MOD; }
#include <stdio.h> #define MOD 1000000007 int main() { int arr[1000][1000] = {0}; int n = 0, m = 0; int i = 0, j = 0; scanf("%d %d", &n, &m); // 初始化第一列 for (i = 0; i < n; i++) { arr[i][0] = 1; } // 初始化第一行 for (i = 0; i < m; i++) { arr[0][i] = 1; } // 填充剩余部分 for (i = 1; i < n; i++) { for (j = 1; j < m; j++) { arr[i][j] = (arr[i-1][j] + arr[i][j-1]) % MOD; } } printf("%d\n", arr[n-1][m-1]); return 0; }
from operator import mod # 防止后续整数太大,所以取模进行运算 MOD = 10**9 + 7 # 读取输入 n, m = map(int, input().split()) # 初始化二维数组 a = [[0] * m for _ in range(n)] # 设置边界条件 for i in range(n): a[i][0] = 1 for j in range(m): a[0][j] = 1 # 填充二维数组 for i in range(1, n): for j in range(1, m): a[i][j] = (a[i-1][j] + a[i][j-1]) % MOD # 打印结果 print(a[n-1][m-1])
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll N = 1e3 + 5, INT = 1e9 + 7; ll cnt[N][N]; ll Dfs(int x, int y) { if(cnt[x][y]) return cnt[x][y]; if(x == 1 && y == 1) return cnt[x][y] = 1; if(y == 1) return cnt[x][y] = Dfs(x - 1, y) % INT; if(x == 1) return cnt[x][y] = Dfs(x, y - 1) % INT; return cnt[x][y] = (Dfs(x - 1, y) + Dfs(x, y - 1)) % INT; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; cout << Dfs(n, m) << "\n"; return 0; }
MOD = 10**9 + 7 def solve(n, m): max_n = n + m - 2 # Precompute factorial and inverse factorial up to max_n fact = [1] * (max_n + 1) inv_fact = [1] * (max_n + 1) for i in range(1, max_n + 1): fact[i] = fact[i-1] * i % MOD inv_fact[max_n] = pow(fact[max_n], MOD - 2, MOD) for i in range(max_n - 1, -1, -1): inv_fact[i] = inv_fact[i + 1] * (i + 1) % MOD a = n + m - 2 b = n - 1 if b < 0&nbs***bsp;b > a: return 0 res = fact[a] * inv_fact[b] % MOD res = res * inv_fact[a - b] % MOD return res n, m = map(int, input().split()) print(solve(n, m))