首页 > 代码库 > [51NOD1119]机器人走方格 V2(dp,Lucas定理)

[51NOD1119]机器人走方格 V2(dp,Lucas定理)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119

题意:中文题面。

很容易知道式子dp(i,j)=dp(i-1,j)+dp(i,j-1),又知道从左上到右下一定是n+m-2步,打个表出来看m=1或n=1的时候结果是n或者m,m=2的时候结果是3、6、10。。。

猜想结果是C(n+m-2,k),带入值把k求出来,k是和n有关的,k=n-1。

所以结果是C(n+m-2,n-1)。大组合数要对素数取模,结果可以用lucas得到。

  1 /*  2 ━━━━━┒ギリギリ♂ eye!  3 ┓┏┓┏┓┃キリキリ♂ mind!  4 ┛┗┛┗┛┃\○/  5 ┓┏┓┏┓┃ /  6 ┛┗┛┗┛┃ノ)  7 ┓┏┓┏┓┃  8 ┛┗┛┗┛┃  9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onecnt(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70  71 LL  n, m; 72 LL p = 1e9+7; 73  74 LL exgcd(LL a,LL b,LL &x,LL &y) { 75     if(b == 0) { 76         x=1; 77         y=0; 78         return a; 79     } 80     LL ret = exgcd(b, a % b, y, x); 81     y -= a / b * x; 82     return ret; 83 } 84  85 LL inv(LL a,int m) { 86     LL d, x, y, t = LL(m); 87     d = exgcd(a, t, x, y); 88     if(d == 1) return (x % t + t) % t; 89     return -1; 90 } 91  92 LL Cm(LL n, LL m, LL p) { 93     LL a = 1, b = 1; 94     if(m > n) return 0; 95     while(m) { 96         a=(a*n)%p; 97         b=(b*m)%p; 98         m--; 99         n--;100     }101     return LL(a) * inv(b, p) % p;102 }103 104 int Lucas(LL n, LL m, LL p) {105     if(m == 0) return 1;106     return LL(Cm(n%p, m%p, p)) * LL(Lucas(n/p, m/p, p)) % p;107 }108 109 int main() {110     // FRead();111     while(cin >> n >> m) {112         n--; m--;113         cout << Lucas(n+m, n, p);114     }115     RT 0;116 }

 

[51NOD1119]机器人走方格 V2(dp,Lucas定理)