青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

風雨

驀然回首 卻在燈火闌珊處
posts - 3, comments - 2, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

[轉載] Computing n choose k mod p

Posted on 2010-05-04 10:07 zgm 閱讀(590) 評論(0)  編輯 收藏 引用

Computing n choose k mod p

Postby joshi13 » Tue Apr 14, 2009 4:49 am

Hi all.

How can we apply the modular multiplicative inverse when calculating

(n choose k) mod p, where 'p' is a prime number.

If you could suggest some related problems, it would be very helpful.

Thanks in advance.


Re: Computing n choose k mod p

Postby mf » Tue Apr 14, 2009 10:56 am

You could use .


Re: Computing n choose k mod p

Postby maxdiver » Tue Apr 14, 2009 12:03 pm

There is another, more "mechanical", but more general, approach. It can be applied to any formula containing factorials over some modulo.

C_n^k = n! / (k! (n-k)!)
Let's learn how to compute n! mod p, but factorial without factors p and so on:
n!* mod p = 1 * 2 * ... * (p-1) * _1_ * (p+1) * (p+2) * ... * (2p-1) * _2_ * (2p+1) * (2p+2) * ... * n.
We took the usual factorial, but excluded all factors of p (1 instead of p, 2 instead of 2p, and so on).
I name this 'strange factorial'.

If n is not very large, we can calculate this simply, then GOTO END_SCARY_MATHS :)
If p is not large, then GOTO BEGIN_SCARY_MATHS:
Else - skip the rest of the post :)

BEGIN_SCARY_MATHS:
After taking each factor mod p, we get:
n!* mod p = 1 * 2 * ... * (p-1) * 1 * 2 * ... * (p-1) * 2 * 1 * 2 * ... * n.
So 'strange factorial' is really several blocks of construction:
1 * 2 * 3 * ... * (p-1) * i
where i is a 1-indexed index of block taken again without factors p ('strange index' :) ).
The last block could be not full. More precisely, there will be floor(n/p) full blocks and some tail (its result we can compute easily, in O(P)).
The result in each block is multiplication 1 * 2 * ... * (p-1), which is common to all blocks, and multiplication of all 'strange indices' i from 1 to floor(n/p).
But multiplication of all 'strange indices' is really a 'strange factorial' again, so we can compute it recursively. Note, that in recursive calls n reduces exponentially, so this is rather fast algorithm.

So... After so much brainfucking maths I must give a code :)
Code: Select all
int factmod (int n, int p) {
   long long res = 1;
   while (n > 1) {
      long long cur = 1;
      for (int i=2; i<p; ++i)
         cur = (cur * i) % p;
      res = (res * powmod (cur, n/p, p)) % p;
      for (int i=2; i<=n%p; ++i)
         res = (res * i) % p;
      n /= p;
   }
   return int (res % p);
}

Asymptotic... There are log_p n iterations of while, inside it there O(p) multiplications, and calculation of power, that can be done in O(log n). So asymptotic is O ((log_p n) (p + log n)).
Unfortunately I didn't check the code on any online judge, but the idea (which was explained by Andrew Stankevich) is surely right.
END_SCARY_MATHS:

So, we can now compute this 'strange factorial' modulo p. Because p is prime, and we've excluded all multiples of p, then the result would be different from zero. This means we can compute inverse for them, and compute C_n^k = n!* / (k!* (n-k)!*) (mod p).
But, of course, before all this, we should check, if p was in the same power in the nominator and denominator of the fraction. If it was indeed in the same power, then we can divide by it, and we get exactly these 'strange factorials'. If the power of p in nominator was greater, then the result will obviously be 0. The last case, when power in denominator is greater than in nominator, is obviously incorrect (the fraction won't be integer).

P.S. How to compute power of prime p in n! ? Easy formula: n/p + n/(p^2) + n/(p^3) + ...


(轉載:http://acm.uva.es/board/viewtopic.php?f=22&t=42690&sid=25bd8f7f17abec626f2ee065fec3703b

只有注冊用戶登錄后才能發(fā)表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产乱码最新视频| 另类天堂av| 欧美电影免费| 毛片精品免费在线观看| 久久久精品一区二区三区| 久久国产精品久久久久久| 亚洲视频一区二区| 亚洲免费小视频| 性色一区二区三区| 久久久人人人| 欧美伦理一区二区| 国产精品电影观看| 国产主播一区| 亚洲精品国产精品乱码不99按摩| 99re66热这里只有精品3直播| 亚洲深夜激情| 久久久中精品2020中文| 亚洲国产成人精品女人久久久 | 国产欧美一区视频| 一区二区在线观看视频在线观看| 亚洲激情婷婷| 亚洲免费影视第一页| 久久亚洲精品伦理| 亚洲精品免费网站| 亚洲男人的天堂在线观看| 久久婷婷丁香| 国产精品露脸自拍| 亚洲国产精品一区制服丝袜| 欧美午夜精品理论片a级大开眼界| 亚洲高清123| 一本到高清视频免费精品| 小处雏高清一区二区三区| 猫咪成人在线观看| 亚洲视频导航| 欧美激情aⅴ一区二区三区| 国产农村妇女精品一区二区| 亚洲人成在线影院| 久久精品1区| 亚洲人成艺术| 久久久久久久一区二区三区| 国产精品久久久久久久久借妻| 一区二区在线视频| 亚洲欧美日韩成人| 亚洲精品久久久一区二区三区| 亚洲欧美一区二区三区久久| 欧美日韩高清在线一区| 亚洲第一在线综合在线| 欧美在线精品免播放器视频| 亚洲另类自拍| 欧美精品系列| 亚洲另类一区二区| 亚洲第一网站| 免费久久精品视频| 尤物九九久久国产精品的特点 | 久久在线视频| 欧美一级在线视频| 国产精品一区视频| 欧美一区二区精品久久911| 日韩午夜激情| 欧美精品一区二区久久婷婷| 亚洲人成在线免费观看| 亚洲国产人成综合网站| 蜜臀久久久99精品久久久久久| 亚洲电影专区| 亚洲国产精品日韩| 欧美日韩在线播放三区| 亚洲中字黄色| 亚洲在线视频网站| 国产九九精品视频| 欧美一级日韩一级| 亚洲免费视频在线观看| 国产欧美日韩伦理| 久久久久久9999| 久久久久久久久综合| 亚洲福利视频网| 欧美激情一区二区三区在线视频观看 | 亚洲欧洲综合另类在线| 欧美激情第10页| 日韩一二三在线视频播| 亚洲精品国产视频| 国产精品进线69影院| 久久精品夜色噜噜亚洲a∨ | 午夜在线精品| 国内外成人免费激情在线视频| 久久精品99国产精品酒店日本| 久久成人一区| 亚洲精品美女久久7777777| 亚洲韩日在线| 国产欧美日韩精品在线| 男人的天堂亚洲| 欧美日韩hd| 久久久久久久波多野高潮日日 | 久久久久在线观看| 一级成人国产| 欧美亚洲一区二区三区| 亚洲国产日韩欧美| 亚洲综合社区| 亚洲精品专区| 亚洲欧美日韩天堂一区二区| 亚洲高清在线| 亚洲免费婷婷| 99精品欧美一区| 久久精品国产久精国产爱| 日韩亚洲不卡在线| 欧美亚洲在线观看| 一区二区欧美国产| 久久久亚洲综合| 亚洲综合国产| 欧美大胆成人| 久久免费高清| 国产精品家教| 日韩亚洲成人av在线| 影视先锋久久| 午夜精品久久久久久久| 在线综合欧美| 欧美经典一区二区三区| 久久综合给合久久狠狠色| 国产精品高清在线| 亚洲激情欧美激情| 尤物yw午夜国产精品视频| 亚洲欧美日韩高清| 亚洲影视综合| 国产精品ⅴa在线观看h| 亚洲激情婷婷| 亚洲免费av片| 欧美激情在线观看| 亚洲第一天堂av| 亚洲欧洲精品成人久久奇米网| 久久精品亚洲一区| 欧美精品一区三区在线观看| 久久久欧美一区二区| 国产日韩一区二区三区在线| 在线亚洲自拍| 午夜精品免费| 国产精品一区二区你懂得 | 亚洲在线不卡| 久久xxxx精品视频| 国产日韩1区| 欧美一级大片在线免费观看| 欧美一区二区成人| 亚洲三级免费| 欧美日韩国产免费观看| 亚洲高清在线观看| 亚洲精品小视频| 欧美精品日韩综合在线| 亚洲看片免费| 亚洲欧美bt| 国产婷婷色一区二区三区在线| 午夜视频在线观看一区| 久久精品女人的天堂av| 精品99一区二区| 噜噜噜躁狠狠躁狠狠精品视频 | 日韩网站在线看片你懂的| 欧美激情中文字幕乱码免费| 亚洲精品黄网在线观看| 中文精品视频| 国产精品一区二区三区久久久 | 亚洲精品综合在线| 欧美日韩高清区| 亚洲综合色自拍一区| 蜜桃av综合| 99国产精品久久| 国产欧美日韩中文字幕在线| 久久嫩草精品久久久精品| 亚洲高清免费视频| 亚洲欧美日韩国产中文在线| 国产亚洲精品aa| 欧美本精品男人aⅴ天堂| 99在线视频精品| 久久久综合免费视频| 亚洲精品一区在线| 国产精品美女一区二区在线观看| 久久高清免费观看| 亚洲精品一区在线观看| 欧美伊人精品成人久久综合97| 在线免费不卡视频| 欧美亚州在线观看| 男男成人高潮片免费网站| 亚洲视频图片小说| 欧美二区在线| 欧美专区亚洲专区| 99re6这里只有精品| 国产一区二区按摩在线观看| 欧美日韩国产综合在线| 久久er精品视频| 亚洲一二三四区| 亚洲日本久久| 欧美成人免费一级人片100| 欧美一区二区播放| 亚洲少妇自拍| 日韩午夜免费视频| 在线观看成人小视频| 欧美黄色网络| 久久精品女人天堂| 欧美一级在线视频| 亚洲视频一区二区免费在线观看| 亚洲第一主播视频| 韩国福利一区| 亚洲欧美电影在线观看| 亚洲欧洲日韩综合二区|