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

首頁 > 學院 > 開發設計 > 正文

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

2019-11-11 06:50:57
字體:
來源:轉載
供稿:網友

關于組合數取模和逆元的知識的參考 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)//可優化的地方

#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定理的理解還很淺顯 需要更深入去了解


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克旗| 嘉荫县| 开原市| 田东县| 台山市| 宜君县| 鄂尔多斯市| 芜湖县| 丰宁| 郁南县| 兴义市| 怀集县| 敖汉旗| 京山县| 鲁甸县| 兴安盟| 乐清市| 遵义市| 和顺县| 景德镇市| 喀喇| 屏山县| 连城县| 射阳县| 融水| 吉安市| 乌兰县| 辽源市| 十堰市| 高陵县| 游戏| 武陟县| 通江县| 贵定县| 塔城市| 县级市| 道真| 舒城县| 兴宁市| 小金县| 康保县|