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

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

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

2019-11-11 06:47:58
字體:
來源:轉載
供稿:網友

關于組合數取模和逆元的知識的參考 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定理的理解還很淺顯 需要更深入去了解


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永城市| 阳东县| 临颍县| 乌拉特前旗| 临夏县| 双峰县| 南涧| 彭州市| 托里县| 阳山县| 定南县| 即墨市| 屏边| 东方市| 新巴尔虎右旗| 百色市| 洮南市| 大姚县| 平江县| 应城市| 望城县| 平湖市| 精河县| 建德市| 涞源县| 仙居县| 江安县| 耒阳市| 六枝特区| 乌苏市| 徐水县| 衡东县| 娄烦县| 阳信县| 盘山县| 萨嘎县| 宁海县| 清水县| 奎屯市| 长春市| 集贤县|