方法1:
#include <iostream> // 本题可以用递推的方法去考虑,结合了动态规划的思想
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1005
#define mod 1000000007
long long dp[maxn][maxn];
int main()
{
int i,j,n,m;
for(i=0;i<maxn;i++) //从前i个数中不取数只有一种方法
dp[i][0]=1;
for(i=1;i<maxn;i++)
{
for(j=1;j<=i;j++)
{
dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%mod; //要么在i-1个数中取j个数,则不取第i个数,要么在i-1个数中取j-1个数,取第i个数
}
}
while(~scanf("%d%d",&n,&m))
{
printf("%I64d\n",dp[n][m]);
}
return 0;
}
方法2:
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
LL n,m,p;
LL quick_mod(LL a, LL b)
{
LL ans = 1;
a %= p;
while(b)
{
if(b & 1)
{
ans = ans * a % p;
b--;
}
b >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL n, LL m)
{
if(m > n) return 0;
LL ans = 1;
for(int i=1; i<=m; i++)
{
LL a = (n + i - m) % p;
LL b = i % p;
ans = ans * (a * quick_mod(b, p-2) % p) % p;
}
return ans;
}
LL Lucas(LL n, LL m)
{
if(m == 0) return 1;
return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
int main()
{
p=1000000007;
while(~scanf("%I64d%I64d", &n, &m))
printf("%I64d\n", Lucas(n,m));
return 0;
}