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

風雨

驀然回首 卻在燈火闌珊處
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>
            在线视频欧美精品| 欧美日韩喷水| 久久久久91| 欧美精品www在线观看| 国产精品久久久对白| 国产欧美日韩精品在线| 91久久线看在观草草青青| 亚洲欧美日韩在线高清直播| 久久久久久亚洲精品中文字幕| 亚洲国产精品t66y| 亚洲欧美怡红院| 美女主播一区| 亚洲一区二区欧美日韩| 欧美激情按摩| 亚洲国产精品成人一区二区| 午夜国产精品影院在线观看| 亚洲欧美另类国产| 欧美日韩亚洲精品内裤| 欧美一级视频免费在线观看| 亚洲另类视频| 久久影视精品| 国产情人节一区| 欧美激情91| 久久综合图片| 亚洲免费在线观看视频| 久久福利影视| 国语自产精品视频在线看抢先版结局 | 欧美大片免费观看| 韩日精品视频| 99re热精品| 欧美韩日一区| 欧美在线三级| 亚洲欧美日韩国产成人| 国产精品久久婷婷六月丁香| 亚洲色图自拍| 一区二区电影免费观看| 国产精品r级在线| 亚洲性感激情| 亚洲视频第一页| 亚洲第一在线综合在线| 欧美电影在线观看完整版| 久久精品中文字幕一区| 亚洲一区二区三区在线视频| 久久精品九九| 欧美在线亚洲在线| 欧美日韩高清不卡| 午夜伦欧美伦电影理论片| 欧美h视频在线| av成人免费在线观看| 在线视频你懂得一区二区三区| 在线观看欧美亚洲| 亚洲国产欧美一区| 在线精品在线| 久久久蜜桃精品| 亚洲精品国产视频| 这里只有视频精品| 99精品欧美| 久久国产精品黑丝| 性欧美长视频| 国产精品进线69影院| 亚洲毛片在线| 在线综合视频| 久久成人免费| 久久露脸国产精品| 国产欧美日韩亚洲| 午夜精品久久久久久久白皮肤| a4yy欧美一区二区三区| 欧美精品一区三区| 久久成人18免费网站| 国产欧美短视频| 欧美一区二区三区的| 99v久久综合狠狠综合久久| 欧美成人第一页| 久久er精品视频| 狠久久av成人天堂| 美女精品视频一区| 亚洲一区二区精品视频| 欧美日韩在线一区| 久久免费视频在线观看| 国产亚洲欧美日韩日本| 亚洲美女免费视频| 亚洲欧美日韩国产成人| 久久久久久久久岛国免费| 欧美插天视频在线播放| 亚洲国产99| 影音国产精品| 欧美a级片网站| 一区二区三区视频免费在线观看| 中文国产成人精品| 国产欧美一区二区三区另类精品| 欧美国产日韩a欧美在线观看| 欧美三级第一页| 中国成人亚色综合网站| 欧美诱惑福利视频| 免费在线欧美视频| 亚洲精品资源| 欧美一级淫片aaaaaaa视频| 狠狠色狠狠色综合日日小说| 欧美成ee人免费视频| 一区二区精品在线| 久久视频免费观看| 欧美日韩一区二区三区| 亚洲欧美成人网| 欧美sm重口味系列视频在线观看| 国产精品日韩欧美一区| 亚洲美女视频在线免费观看| 欧美一级网站| 亚洲精品一区二区三区不| 国产精品网站在线播放| 蜜桃av综合| 亚洲欧美区自拍先锋| 欧美高清视频免费观看| 香蕉成人久久| 99亚洲伊人久久精品影院红桃| 国产精品伦理| 欧美精品电影| 久久亚洲图片| 乱中年女人伦av一区二区| 怡红院精品视频| 国产精品久久久久91| 欧美国产日韩一区| 久久久久久穴| 性欧美暴力猛交另类hd| 久久精品99久久香蕉国产色戒| 国产精品久久久久久妇女6080 | 亚洲一区在线免费| 国产精品成人免费精品自在线观看| 久久国产精品色婷婷| 宅男在线国产精品| 亚洲激精日韩激精欧美精品| 最新成人在线| 极品少妇一区二区三区| 欧美成人精品一区二区| 久久精品国产一区二区三区| 中文在线资源观看网站视频免费不卡| 欧美激情一二三区| 女主播福利一区| 暖暖成人免费视频| 蜜臀久久99精品久久久久久9| 性色一区二区三区| 亚洲一区中文| 亚洲男女自偷自拍| 亚洲香蕉成视频在线观看| 99精品视频免费| 99精品99| 亚洲一区国产一区| 亚洲免费在线| 亚洲欧美日韩国产综合在线| 亚洲午夜激情| 性欧美1819性猛交| 欧美主播一区二区三区| 久久精品视频亚洲| 国产一区二区三区视频在线观看| 亚洲第一福利社区| 国产中文一区二区| 欧美中文字幕不卡| 最新亚洲电影| 亚洲黄色毛片| 亚洲精品在线电影| 中国av一区| 午夜一级久久| 乱码第一页成人| 欧美—级在线免费片| 欧美日韩在线不卡一区| 国产精品美女久久久久aⅴ国产馆| 国产精品视频一二三| 国产日韩欧美精品在线| 欧美三区美女| 国产精品一卡| 欧美午夜久久| 国内精品模特av私拍在线观看| 精品成人免费| 中日韩美女免费视频网址在线观看| 亚洲女人天堂成人av在线| 欧美一级淫片播放口| 欧美在线亚洲在线| 欧美成人午夜免费视在线看片| 欧美日韩亚洲一区二区| 国产亚洲精品福利| 亚洲欧美欧美一区二区三区| 欧美一进一出视频| 欧美精品一区在线发布| 国产伦精品一区二区三区在线观看 | 免费在线观看日韩欧美| 欧美午夜在线一二页| 伊人成年综合电影网| 一本色道精品久久一区二区三区 | 性欧美在线看片a免费观看| 美女在线一区二区| 牛牛影视久久网| 亚洲片区在线| 性欧美大战久久久久久久久| 免费日本视频一区| 一本一本a久久| 久久综合九色| 国产伦精品一区二区三区高清版| 亚洲国产精品电影在线观看| 午夜久久久久久| 亚洲国产一区二区a毛片| 欧美一二区视频|