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

風雨

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

[轉載] Computing n choose k mod p

Posted on 2010-05-04 10:07 zgm 閱讀(594) 評論(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

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   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>
            久久久欧美一区二区| 午夜国产不卡在线观看视频| 久久精品国产69国产精品亚洲| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 美女网站久久| 91久久久在线| 一区二区高清视频在线观看| 国产精品av免费在线观看| 亚洲欧美国产精品专区久久| 午夜精品亚洲| 亚洲电影免费在线观看| 亚洲国产片色| 欧美天天综合网| 香蕉久久精品日日躁夜夜躁| 久久本道综合色狠狠五月| 亚洲国产综合91精品麻豆| 亚洲精品免费在线播放| 国产精品免费观看在线| 麻豆精品91| 欧美日韩亚洲高清| 久久精品国产一区二区三区免费看| 久久激情视频久久| 日韩亚洲在线| 欧美一区二区三区视频免费播放| 最新国产成人在线观看| 亚洲视频在线播放| 亚洲高清视频在线观看| 在线性视频日韩欧美| 影音先锋另类| 在线视频一区二区| 亚洲国产高清视频| 亚洲欧美久久久| 亚洲欧洲在线看| 欧美一区免费视频| 日韩视频精品在线观看| 久久成人在线| 亚洲免费在线| 欧美精品在线视频观看| 久久久久久伊人| 国产精品久久久久91| 亚洲承认在线| 韩国一区二区三区在线观看 | 亚洲免费视频网站| 久久一区二区三区av| 午夜精品影院| 欧美日韩伦理在线| 欧美国产综合一区二区| 国产一区视频在线观看免费| 一区二区三区日韩欧美| 亚洲另类自拍| 免费日韩av| 噜噜噜在线观看免费视频日韩| 国产精品视频免费观看www| 亚洲大胆人体视频| 在线播放不卡| 久久久999成人| 久久精品在线播放| 国产欧美在线| 午夜视频在线观看一区二区| 亚洲欧美综合v| 国产精品乱人伦一区二区 | 亚洲国产免费看| 在线成人国产| 久久久久久网址| 久久综合福利| 亚洲成在人线av| 久久青草久久| 欧美国产欧美综合| 亚洲欧洲在线播放| 欧美激情2020午夜免费观看| 亚洲国产精品传媒在线观看| 亚洲国产精品国自产拍av秋霞| 久久人人超碰| 亚洲国产婷婷| 一区二区三区精品视频| 欧美午夜宅男影院| 午夜精品久久久久久99热软件| 欧美综合第一页| 国产一区二区三区高清 | 日韩性生活视频| 亚洲在线成人精品| 国产欧美大片| 久久久五月天| 日韩视频在线一区| 午夜精品视频在线观看一区二区| 国产精品成人aaaaa网站| 亚洲在线成人| 免费成人av在线| 一本色道久久综合亚洲二区三区| 欧美日韩激情小视频| 亚洲在线视频一区| 久久尤物视频| 一区二区三区欧美视频| 国产精品视频yy9099| 久久久久久亚洲精品中文字幕 | 久久福利影视| 91久久久久久国产精品| 国产精品国产福利国产秒拍| 欧美在线二区| 亚洲精品久久久蜜桃| 欧美一区二区视频在线| 亚洲国产精品va在线看黑人| 欧美日韩另类字幕中文| 久久精品欧洲| 一区二区三区高清在线 | 亚洲视频免费看| 国产亚洲一区在线播放| 欧美精品二区| 欧美中文在线视频| 99国产一区| 蜜月aⅴ免费一区二区三区| 在线中文字幕一区| 亚洲福利在线看| 国产欧美短视频| 欧美日韩一区二区免费视频| 久久婷婷色综合| 亚洲一区在线观看免费观看电影高清| 美女精品一区| 久久不射网站| 亚洲免费在线播放| 日韩视频免费观看高清完整版| 国内精品久久久久国产盗摄免费观看完整版 | av成人激情| 亚洲二区在线视频| 久久亚洲综合色一区二区三区| 亚洲欧美日韩另类| 一区二区免费在线播放| 亚洲精品久久久久久久久久久久久 | 国产精品热久久久久夜色精品三区 | 久久精品国产第一区二区三区| 亚洲精品中文在线| 欧美韩国日本一区| 欧美99久久| 久久久久一区| 久久激情网站| 先锋影音国产精品| 亚洲与欧洲av电影| 亚洲一区二区三区午夜| 日韩系列欧美系列| 亚洲乱码精品一二三四区日韩在线| 韩国三级电影久久久久久| 国产日韩精品视频一区| 国产精品一区在线观看| 国产精品萝li| 国产欧美日韩在线观看| 国产精品色在线| 国产精品每日更新在线播放网址| 欧美性感一类影片在线播放 | 欧美一区二区三区在线观看| 午夜在线观看欧美| 欧美亚洲视频一区二区| 久久成人一区| 久久夜色精品国产欧美乱极品| 久久阴道视频| 欧美女同在线视频| 欧美日韩亚洲一区二区三区| 国产精品v亚洲精品v日韩精品| 国产精品乱子久久久久| 国产亚洲精品久| 在线观看国产精品网站| 亚洲欧洲在线播放| 一区二区三区四区五区精品视频| 亚洲免费网址| 久热成人在线视频| 亚洲激情在线视频| 亚洲视频国产视频| 久久激情视频| 欧美人与性动交α欧美精品济南到| 欧美日韩蜜桃| 国产综合亚洲精品一区二| 亚洲欧洲日本国产| 亚洲午夜免费视频| 久久裸体视频| 亚洲人成网站精品片在线观看| 亚洲一二三级电影| 免费欧美日韩国产三级电影| 欧美性一区二区| 在线精品亚洲一区二区| 亚洲视频精品| 欧美 日韩 国产精品免费观看| 亚洲精品社区| 久久久久久久综合色一本| 欧美日韩精品免费观看| 国精品一区二区三区| 99视频精品在线| 久久综合婷婷| 亚洲香蕉网站| 欧美激情一二三区| 国产综合欧美| 午夜精品www| 亚洲欧洲一区二区在线播放| 性欧美精品高清| 国产精品成人一区| 亚洲精品欧美日韩专区| 久久精品99| 亚洲综合三区| 欧美日韩视频专区在线播放 | 欧美成人免费va影院高清| 亚洲一级片在线看| 欧美精品日韩www.p站|