国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

51nod 1627 瞬間移動 組合數取模

2019-11-11 06:09:29
字體:
來源:轉載
供稿:網友

關于組合數取模和逆元的知識的參考 http://blog.csdn.net/acdreamers/article/details/8037918 http://blog.csdn.net/acdreamers/article/details/8220787#comments 題目: 有一個無限大的矩形,初始時你在左上角(即第一行第一列),每次你都可以選擇一個右下方格子,并瞬移過去(如從下圖中的紅色格子能直接瞬移到藍色格子),求到第n行第m列的格子有幾種方案,答案對1000000007取模。 這里寫圖片描述 Input 單組測試數據。 兩個整數n,m(2<=n,m<=100000) Output 一個整數表示答案。 Input示例 4 5 Output示例 10 可通過打表或者其他理解得出 答案為C(m+n-4,m-2)或C(m+n-4,n-2)//可優(yōu)化的地方

#include <iostream>#include <cstdio>#include <sstream>#include <set>#include <bitset> #include <queue> #include <stack> #include <list>#include <vector>#include <map>#include <string>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef set<int> Set;typedef vector<int> Vec;typedef set<int>::iterator It;typedef long long ll;#define mem(s,n) memset(s,n,sizeof(s))int p = 1000000007;ll quick_mod(ll a,ll b)//a^b%p 快速冪{ 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)//nCm %p{ if(n < m) return 0; ll ans = 1; for(ll i=1;i<=m;i++) { ll a = (n - m + i) % p; ll b = i % p; ans = ans *(a * quick_mod(b,p-2) % p) % p;//逆元的知識 } return ans;}ll Lucas(ll n,ll m)//Lucas定理{ if(m == 0) return 1; return C(n % p,m % p) * Lucas(n / p,m / p) % p;}int main(int argc, char *argv[]){ ll m,n,a,b; scanf("%lld%lld",&m,&n); b=m+n-4; a=min(m-2,n-2); 對于正整數 a 和 p,若 ax≡1 mod p, 則稱a關于模f的乘法逆元為x。 也可表示為ax≡1(mod p)。逆元一般用擴展歐幾里得算法來求得,如果為素數,那么還可以根據費馬小定理得到逆元為 ap?2≡1a(mod p) 實際應用主要用于處理除法取模 如組合數

這里寫圖片描述這里寫圖片描述且p為素數 Lucas定理: 則有 利用逆元計算即可

對于逆元和Lucas定理的理解還很淺顯 需要更深入去了解


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 哈密市| 崇仁县| 胶南市| 灵寿县| 彭泽县| 新安县| 苏州市| 永昌县| 丰县| 根河市| 柳林县| 宁武县| 泰州市| 洞口县| 新宁县| 合水县| 章丘市| 伊宁县| 定陶县| 宜阳县| 金阳县| 菏泽市| 诸暨市| 子洲县| 东辽县| 鹤庆县| 晋州市| 泽州县| 安阳市| 乌鲁木齐市| 永川市| 买车| 沙河市| 阿克| 华阴市| 德阳市| 阆中市| 香河县| 彰武县| 鸡西市| 光泽县|