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

風雨

驀然回首 卻在燈火闌珊處
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>
            亚洲性夜色噜噜噜7777| 欧美国产激情| 午夜精品视频在线观看| 国产精品久线观看视频| 亚洲欧美日本日韩| 亚洲综合欧美日韩| 韩国一区二区三区美女美女秀| 久久婷婷麻豆| 欧美aⅴ一区二区三区视频| 亚洲久久视频| 亚洲视频欧美在线| 一区二区三区在线免费播放| 欧美激情一区三区| 欧美视频一区二区在线观看| 欧美一区深夜视频| 久热国产精品| 亚洲一区二区三区777| 亚洲欧美日韩视频一区| 亚洲电影免费| 在线午夜精品自拍| 影音先锋欧美精品| 99精品国产一区二区青青牛奶| 国产精品一区一区| 欧美国产日韩一区二区| 国产精品久久久亚洲一区| 久久综合九色欧美综合狠狠| 欧美精品v国产精品v日韩精品 | 欧美一区二区成人| 亚洲国产精品成人精品| 亚洲午夜激情网页| 亚洲国产欧美一区二区三区同亚洲| 日韩一级不卡| 亚洲狠狠丁香婷婷综合久久久| 一本久道久久综合中文字幕| 国产专区精品视频| 一区二区日韩欧美| 亚洲国产精品成人综合色在线婷婷| 日韩视频在线一区二区| 在线欧美日韩| 午夜精品久久| 亚洲视频综合| 欧美激情国产高清| 老司机免费视频一区二区三区 | 亚洲免费在线视频| 日韩网站在线观看| 久久人体大胆视频| 久久成人综合视频| 国产精品播放| 日韩一级黄色av| 亚洲精品免费观看| 久久久久亚洲综合| 久久久久国内| 国产精品永久免费在线| 一区二区精品在线| 一区二区精品在线观看| 欧美成人a视频| 你懂的成人av| 亚洲国产精品久久久久| 久久久久青草大香线综合精品| 久久大香伊蕉在人线观看热2| 欧美午夜片在线免费观看| 亚洲精品在线观看视频| 亚洲精品色图| 欧美日韩精品在线视频| 亚洲人成毛片在线播放女女| 在线免费不卡视频| 巨乳诱惑日韩免费av| 欧美www视频在线观看| 极品少妇一区二区三区| 久久久亚洲人| 欧美国产精品一区| 亚洲精品免费一区二区三区| 欧美国产综合视频| 91久久亚洲| 国产精品99久久久久久久久| 欧美三区美女| 亚洲一区二区久久| 久久九九电影| 亚洲激情女人| 欧美日韩成人在线观看| 亚洲素人在线| 久久爱www久久做| 亚洲高清av| 欧美日韩国产一中文字不卡| 亚洲天堂久久| 久久午夜av| 日韩午夜在线播放| 国产精品爽爽ⅴa在线观看| 欧美影视一区| 亚洲国产成人精品女人久久久 | 在线一区欧美| 国产日韩欧美精品一区| 久久久www成人免费精品| 亚洲国产日韩欧美在线动漫| 亚洲夜晚福利在线观看| 国产一区二区三区高清播放| 久久综合给合久久狠狠色| 亚洲肉体裸体xxxx137| 午夜精品久久久久久久99热浪潮| 国产色产综合产在线视频| 久久这里有精品视频| 一区二区三区国产精华| 久久免费视频网站| 亚洲婷婷在线| 亚洲福利视频一区| 国产精品视频一| 男人的天堂成人在线| 亚洲视频综合在线| 欧美国产第二页| 久久精品30| 亚洲天堂免费在线观看视频| 尤物网精品视频| 国产精品久久久久久久久久久久久| 久久精品女人的天堂av| 在线亚洲免费| 亚洲欧洲精品一区| 久久影音先锋| 久久gogo国模裸体人体| 亚洲视频免费在线| 亚洲精品视频一区| 依依成人综合视频| 国产欧美精品日韩精品| 欧美视频中文字幕在线| 欧美国产综合| 噜噜噜在线观看免费视频日韩| 亚洲一区免费网站| 一本色道久久88精品综合| 欧美国产先锋| 久久综合伊人77777| 久久久xxx| 欧美一区二区在线免费播放| 中文欧美日韩| 日韩网站在线观看| 99国产麻豆精品| 亚洲乱码国产乱码精品精98午夜 | 国产精品视频一区二区三区| 欧美精品麻豆| 欧美韩日一区二区| 蜜臀久久99精品久久久久久9 | 日韩一区二区电影网| 亚洲高清视频在线观看| 欧美成人综合一区| 美女国产精品| 欧美激情久久久久| 亚洲国产高潮在线观看| 亚洲国产高清视频| 亚洲黄色影院| 亚洲每日更新| av不卡在线看| 亚洲一二三区精品| 亚洲欧美日韩在线综合| 午夜久久影院| 欧美有码视频| 美女啪啪无遮挡免费久久网站| 老司机精品视频网站| 欧美大片国产精品| 欧美老女人xx| 国产精品欧美风情| 国产综合网站| 亚洲日韩欧美视频一区| 亚洲视频在线观看一区| 亚洲欧美三级在线| 久久久久综合一区二区三区| 免费美女久久99| 亚洲欧洲日本专区| 亚洲一区二区三区在线| 欧美一区在线直播| 嫩模写真一区二区三区三州| 欧美三级电影一区| 国产日韩欧美在线播放| 亚洲大胆人体视频| 中文在线资源观看视频网站免费不卡| 午夜久久资源| 免费视频一区| 亚洲视频欧美在线| 免费成年人欧美视频| 欧美午夜a级限制福利片| 国产一区二区精品久久| 亚洲免费久久| 久久成人精品一区二区三区| 欧美国产一区在线| 亚洲视频免费观看| 模特精品在线| 国产欧美日韩中文字幕在线| 最新国产成人av网站网址麻豆 | 亚洲男人的天堂在线aⅴ视频| 久久久久久9| 日韩亚洲欧美成人一区| 久久精品国产一区二区三区| 欧美日韩国产在线播放| 国内精品久久久久久久97牛牛| 一区二区三区四区国产精品| 久久综合久久美利坚合众国| 日韩视频免费观看高清完整版| 久久99在线观看| 国产精品久久久久aaaa樱花| 亚洲黄色在线观看| 久久在精品线影院精品国产| 亚洲一区二区三区在线| 欧美日韩一区二区免费视频|