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

The Fourth Dimension Space

枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

POJ 1150 ——The Last Non-zero Digit(數(shù)論)

題意很簡單,要求你求出一個排列數(shù)P(n,m)中最后一個非0的數(shù)字.
由于n的數(shù)值巨大,想直接求出來恐怕是不可行的。
在網(wǎng)上有這樣一個英文的解題報告,由于缺少中文的資料,硬著頭皮把它看完了:-)

The first program

Consider the number N! factored into product of powers of prime numbers. It means N!=2i * 3j * 5k * 7l * ... Note, that for each N>1 the power i is greater than k. It means, that the last non-zero digit of N! is the same as the last digit of N! / (2k * 5k). Therefore we can compute the result using the equation:

(N! / (2k * 5k)) mod 10 = ((N! / (2i * 5k)) mod 10 * 2i-k mod 10) mod 10

Number i can be obtained easily - we will divide each a=1,2,...,N by 2 until the resulting number is not divisible by 2 and after each division we will add one to the variable i. Number k can be obtained in the same manner. Let f(i) denotes the number which we obtain by dividing i by the 2a * 5b where a and b are the highest numbers such that the i is divisible by this product. Number (N! / (2i * 5k)) mod 10 is equal to f(N!) mod 10 and can be computed as f(1) * f(2) * ... * f(N) mod 10. We will perform operation mod 10 after each multiplication in order to keep the resulting number as small as possible.

The advantege of this approach is that we do not need to implement arithmetics of large numbers. Some ideas used here are used in the second, more efficient program, as well.

The second program

The second program also computes the result as (2i-k mod 10 * f(N!) ) mod 10. Numbers i and k are computed much more efficiently. More precisely

i=N div 2 + (N div 2) div 2 + ((N div 2) div 2) div 2 + ...

(We get zero after finite number of divisions.) Number k can be computed in the same way. After that we can compute i-k and we need to find 2i-k mod 10. Observe, that

21 mod 10 = 2, 22 mod 10 = 4, 23 mod 10 = 8, 24 mod 10 = 6, 25 mod 10 = 2, 26 mod 10 = 4, ...

i.e. the period is 4 and we need only to compute (i-k) mod 4 and then to find corresponding last digit. This observation can help us to simplify computation of i and k - we do not need their exact values (that can be long) but we need only (i-k) mod 4.

We have shown how to compute 2i-k mod 10. Now let us consider f(N!) mod 10 = ((f(1) mod 10) * (f(2) mod 10) * ... * (f(N) mod 10)) mod 10. Note, that f(i) mod 10 is always 1,3,7 or 9. If we knew, how many 3,7,9 are among (f(1) mod 10), (f(2) mod 10), ..., (f(N) mod 10), we could compute 3a mod 10, 7b mod 10, 9c mod 10 in the similar way as we did for 2i-k (last digits of powers of 3,7,9 are also periodical).

To compute the number of 3,7,9 among (f(1) mod 10), (f(2) mod 10), ..., (f(N) mod 10) is not so easy. We will divide numbers 1,2,...,N into groups so, that in each group are numbers with same quotient i/f(i) and we will compute number of 3,7,9 among (f(i) mod 10) for each such group separatelly (there are O(N2) such groups). First, let us consider a group in which i/f(i)=1. This is the group of all numbers not divisible by 2 and 5. The number of 3,7,9 in this group is the same as number of 3,7,9 among 1 mod 10, 2 mod 10, ..., N mod 10. This number can be counted easily - it is N div 10 + a where a is 1 if the last digit of N is at least 3 (resp. at least 7 or at least 9). Now let us consider a group in which i/f(i)=L (where L=2a * 5b). We obtain this group by taking each L-th number from the sequence 1,2,3,... and dividing it by L. It means that number of 3,7,9 for this group will be the same as the number of 3,7,9 among 1 mod 10, 2 mod 10, ..., (N div L) mod 10.

Now we know everything we needed for construction of a program. Since numbers in the input file are long, we need to implement arithmetics for long numbers. However, by careful implementation we can achieve that only division of a long number by small integer is necessary.



這個題怎么來做呢?先別急,我們先來討論一下下面幾個子問題:
1.如何求出n階乘中質(zhì)因數(shù)x(比如說5)出現(xiàn)的次數(shù)?
比如說15的階乘 :1*2*3*4*5*6*7*8*9*10*11*12*13*14*15
由于5這個質(zhì)因數(shù)只在5的倍數(shù)里面才出現(xiàn),所以我從5,10,15中各抽出一個5,這相當于有15/5個質(zhì)因數(shù)5,之后5,10,15就變成了1,2,3;
由于非5的倍數(shù)顯然不在考慮范圍之內(nèi),所以我們只需繼續(xù)討論它的子問題3!即可。
這樣,我們可以用遞歸來解決這個問題。有了這個方法,我們是不是能夠輕易地解決n!末尾有多少個0呢?想想看...n!后0的個數(shù)是不是就和某個質(zhì)因數(shù)的個數(shù)有關(guān)呢?^_^
比如說,我們要求5出現(xiàn)的次數(shù),可以這樣寫:

int get5(int n)//計算n!中質(zhì)因子5的出現(xiàn)次數(shù)
{
    
if(n==0)
        
return 0;
    
return n/5+get5(n/5);
}


 2.如何求出n!階乘最后非0位?
比如說我們要找10!最后非0位,由于質(zhì)因數(shù)2和5組合之后會使得末尾產(chǎn)生0.那么我們不妨把10!中2,5質(zhì)因數(shù)全部去掉,(但是請注意2的數(shù)目其實比5的要多,所以我們要在最后考慮多余的2對末位的影響)
如 1*2*3*4*5*6*7*8*9*10 去掉2 ,5 因子后 就是1*1*3*1*1*3*7*1*9*1,由于2,5因子已經(jīng)被去除,那么剩下的數(shù)字末尾一定是3,7,9,1中四者之一。然后我們再求出這么一串數(shù)相乘以后末尾的數(shù)是幾.最后再補上2對末位的影響即可!

總結(jié)一下,求10!最后一個非0位的步驟如下:
step1:首先將10!中所有2,5因子去掉;
step2:然后求出剩下的一串數(shù)字相乘后末尾的那個數(shù)。
step3:由于去掉的2比5多,最后還要考慮多余的那部分2對結(jié)果的影響。
step4:output your answer!

正如上面文章里所說的“To compute the number of 3,7,9 among (f(1) mod 10), (f(2) mod 10), ..., (f(N) mod 10) is not so easy”,這里面步驟2是個難點。如何求出剩下的這串數(shù)字相乘后最后一位是幾呢?這可以轉(zhuǎn)化成求出這些數(shù)里面末尾是3,7,9的數(shù)字出現(xiàn)的次數(shù)(為啥?因為這些數(shù)的n次方是有規(guī)律的,周期為4,不信你可以推一下)
好,現(xiàn)在問題就是如何求出這串數(shù)字中末尾3,7,9各自出現(xiàn)的次數(shù)了;

一個數(shù)列實際上可以分成偶數(shù)列和奇數(shù)列,以1*2*3*4*5*6*7*8*9*10為例

分成1 3 5 7 9,   2 4 6 8 10

這樣我們嘗試分別進行統(tǒng)計,可以發(fā)現(xiàn),實際上2,4,6,8,10中的個數(shù)也就是1 2 3 4 5中的個數(shù),也就是說我們又把這個問題劃分成了一個原來問題的子問題。

f(n) = f(n/2) + g(n),g(n)表示奇數(shù)列中的數(shù)目,所以我們需要解決g(n)

再次觀察g(n)

實際上又分成了兩部分1 3 7 9 11 13 17 19 21。。。以及5的奇倍數(shù)5,15,25。。。說明又出現(xiàn)了子問題,如果要統(tǒng)計這個數(shù)列中末尾為x(1,3,7,9)的個數(shù)可以這樣寫:g(n,x) = n/10+(n%10 >= x)+g(n/5,x) 

這樣利用了兩個遞歸方程,我們就可以在lgn的時間內(nèi)計算出末尾為1,3,7,9的數(shù)的個數(shù)了

好了,現(xiàn)在我們得到了這串數(shù)字中末尾是3,7,9的數(shù)字的個數(shù),我們利用循環(huán)節(jié)的性質(zhì)可以快速地算出這串數(shù)字相乘后mod 10的結(jié)果,在考慮下當時多除的2(其實也可以用循環(huán)節(jié)來處理),便可求出答案!




解決了上面兩個子問題,我想求P(n,m)最后一個非0位就變得十分容易了。
P(n,m)實際上等于 n! / (n-m)!
我們可以求出n! 和(n-m)!中質(zhì)因數(shù)2,5,3,7,9分別出現(xiàn)的次數(shù),然后再各自相減。
然后再用循環(huán)節(jié)處理,即可!
BTW,這里還要注意一個trick,就是2的出現(xiàn)次數(shù)如果小于5,(這對于排列數(shù)來說是可能的)我們可以直接輸出5,如果2的數(shù)目等于5,那么2的循環(huán)節(jié)不需要考慮。至于3,7,9的循環(huán)節(jié),由于這些數(shù)的4次方末位剛好是1,所以就不需要特殊考慮了。


附代碼:
#include<iostream>
#include
<cstring>
#include
<cmath>
using namespace std;

int get2(int n)//計算n!中質(zhì)因子2的出現(xiàn)次數(shù)
{
    
if(n==0)
        
return 0;
    
return n/2+get2(n/2);
}


int get5(int n)//計算n!中質(zhì)因子5的出現(xiàn)次數(shù)
{
    
if(n==0)
        
return 0;
    
return n/5+get5(n/5);
}


//////////////////////////////////////////////////////////////////////////
int g(int n,int x)//計算f(1) to f(n) 中,奇數(shù)數(shù)列中末尾為x的數(shù)出現(xiàn)的次數(shù)
{
    
if(n==0)
        
return 0;
    
return n/10+(n%10>=x)+g(n/5,x);
}


int getx(int n,int x)//計算f(1) to f(n)中,末尾為x的數(shù)的出現(xiàn)次數(shù)
{
    
if(n==0)
        
return 0;
    
return getx(n/2,x)+g(n,x);
}

//////////////////////////////////////////////////////////////////////////

int table[4][4=
{
        
6,2,4,8,//2^n%10的循環(huán)節(jié),注意如果2的個數(shù)為0時候,結(jié)果應(yīng)該是1,要特殊處理。 
        1,3,9,7,//3
        1,7,9,3,//7
        1,9,1,9,//9    
}
;//3,7,9的循環(huán)節(jié)中第一位,剛好是1,故不需要考慮這些數(shù)字出現(xiàn)次數(shù)為0的情況。


int main()
{

    
int n,m;
    
int num2;
    
int num3;
    
int num5;
    
int num7;
    
int num9;
    
while(scanf("%d%d",&n,&m)!=EOF)
    
{
        num2
=get2(n)-get2(n-m);
        num5
=get5(n)-get5(n-m);
        num3
=getx(n,3)-getx(n-m,3);
        num7
=getx(n,7)-getx(n-m,7);
        num9
=getx(n,9)-getx(n-m,9);
        
int res=1;
        
if(num5>num2)
        
{
            printf(
"5\n");
            
continue;
        }

        
else 
        
{
            
if(num2!=num5)
            
{
                res
*=table[0][(num2-num5)%4];
                res
%=10;
            }
//如果num2==num5,那么2^0次方mod 10應(yīng)該為1 ,而不是table中的6,所以要特殊處理。
            
            res
*=table[1][num3%4];
            res
%=10;
            res
*=table[2][num7%4];
            res
%=10;
            res
*=table[3][num9%4];
            res
%=10;
        }

        printf(
"%d\n",res);
    }

    
return 0;
}


                                                                                                                                                                                                         special thanks to 星星 

posted on 2009-10-31 19:07 abilitytao 閱讀(3619) 評論(8)  編輯 收藏 引用

評論

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2009-11-01 13:20 凡客誠品網(wǎng)

似懂非懂  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論)[未登錄] 2009-11-01 17:14 abilitytao

@凡客誠品網(wǎng)
請不要使用帶有廣告信息的署名  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2010-03-04 15:52 foreverlin

寫的很好很易懂,受教了  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2010-07-22 16:59 ACMer

贊一個  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2010-07-29 11:20 inowfordream

寫得很好,受教了!感謝萬分!  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2010-08-11 21:55 slp

您好,能問你一個問題嗎?這個函數(shù)的子函數(shù)
int odd_getX(int n,int x){
if(n == 0) return 0;
return n/10+(n%10 >= x)+ odd_getX(n/5,x);
}

odd_getX(n/5,x)不是又生成了一個1 2 3...的序列,應(yīng)該要在進行偶序列和奇序列的劃分,為什么直接判斷他為奇序列處理?如果你知道原理的話麻煩發(fā)郵件給我1032327983@qq.com 再次感謝  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2010-08-11 22:09 slp

哦 我發(fā)現(xiàn)我錯了,比如奇序列1 3 5 7 9 11 13 15 17 19 ......
二分為: 1 3 7 9 11 13 17 19 .....
五倍數(shù): {5 15 25 35 ....}/5 = {1 3 5 7} 還是奇序列 我錯了!
Orz!  回復  更多評論   

# re: POJ 1150 ——The Last Non-zero Digit(數(shù)論) 2012-07-14 09:52 pictureyong

step2:然后求出剩下的一串數(shù)字相乘后末尾的那個數(shù)
對這步的理解不對吧,  回復  更多評論   


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導航: 博客園   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>
            国产真实乱偷精品视频免| 在线欧美影院| 亚洲男同1069视频| 亚洲资源在线观看| 午夜日韩激情| 性欧美在线看片a免费观看| 一区二区电影免费观看| 亚洲婷婷在线| 亚洲自拍偷拍麻豆| 久久久久久久综合| 欧美片第1页综合| 国产精品久久久久久久app| 亚洲国产精品毛片| 欧美韩国在线| 亚洲视频狠狠| 久久久青草青青国产亚洲免观| 葵司免费一区二区三区四区五区| 老色批av在线精品| 欧美三日本三级三级在线播放| 国产精品久久久久久久久久妞妞| 狠狠狠色丁香婷婷综合久久五月 | 国产综合视频| 亚洲国产精品一区二区三区| 在线视频日韩精品| 久久精品国产免费看久久精品| 久久日韩精品| 中文欧美字幕免费| 狂野欧美一区| 国产亚洲精品美女| 亚洲一级电影| 亚洲欧美日韩直播| 欧美mv日韩mv国产网站| 免费欧美电影| 国产一区日韩二区欧美三区| 夜夜嗨一区二区三区| 久久精品综合| 亚洲一区二区三区四区五区午夜 | 欧美大胆a视频| 午夜一区在线| 久久一区精品| 国产区精品视频| 亚洲一区二区在线播放| 亚洲国产成人av| 久久久久久久久久看片| 国产麻豆日韩| 91久久久久久久久| 久久另类ts人妖一区二区| 中日韩高清电影网| 欧美日韩一本到| 日韩亚洲欧美一区| 亚洲激情国产精品| 欧美成年人在线观看| 精品999久久久| 欧美一区二区三区四区在线| 亚洲九九爱视频| 欧美精品一二三| 精品不卡在线| 欧美成人一区二区三区片免费| 久久精品国产亚洲5555| 久久九九全国免费精品观看| 一区二区三区日韩欧美精品| 欧美黄色小视频| 亚洲精品乱码久久久久久蜜桃麻豆| 久久手机精品视频| 久热re这里精品视频在线6| 亚洲欧美日韩系列| 国产日韩精品在线| 久久人人看视频| 久久久久久电影| 亚洲国产二区| 亚洲人成人一区二区在线观看| 欧美国产日韩a欧美在线观看| 亚洲精品综合久久中文字幕| 亚洲经典自拍| 国产精品入口夜色视频大尺度| 欧美亚洲网站| 久久网站热最新地址| 亚洲国产视频a| 99国产精品视频免费观看| 国产精品夫妻自拍| 久久久久久久97| 欧美黑人在线观看| 亚洲综合视频在线| 久久国产福利| 亚洲美女电影在线| 亚洲欧美激情一区| 亚洲高清av在线| 日韩亚洲精品电影| 国产一区二区欧美日韩| 亚洲国产经典视频| 国产精品你懂的在线欣赏| 免费不卡中文字幕视频| 欧美日韩国产一区| 久久福利精品| 欧美激情精品久久久久久大尺度| 亚洲欧美www| 久久亚洲精品视频| 亚洲欧美日韩精品久久| 欧美一区二区性| 亚洲国产三级| 国产精品午夜在线观看| 美女91精品| 国产精品推荐精品| 亚洲激情二区| 好吊色欧美一区二区三区四区| 亚洲欧洲精品一区二区三区| 国产日韩高清一区二区三区在线| 亚洲国产一二三| 影音先锋久久| 香蕉成人久久| 亚洲特黄一级片| 麻豆精品传媒视频| 久久国产婷婷国产香蕉| 欧美日韩免费在线视频| 欧美电影电视剧在线观看| 国产欧美精品在线播放| 一本色道久久88综合亚洲精品ⅰ| 亚洲国产毛片完整版| 久久久www成人免费无遮挡大片| 亚洲欧美日韩中文播放| 欧美色大人视频| 亚洲人成网站影音先锋播放| 亚洲成色www8888| 久久爱www久久做| 欧美一级午夜免费电影| 欧美视频日韩| 99re6热只有精品免费观看| 亚洲欧洲精品一区二区三区波多野1战4 | 麻豆av一区二区三区| 亚洲精品视频在线观看免费| 亚洲电影欧美电影有声小说| 亚洲欧美日本视频在线观看| a91a精品视频在线观看| 欧美顶级大胆免费视频| 欧美国产日韩在线观看| 亚洲第一天堂无码专区| 久久先锋资源| 免费观看亚洲视频大全| 国内久久视频| 久久免费视频网站| 欧美福利一区二区三区| 亚洲青涩在线| 欧美精品三区| 宅男精品视频| 欧美一区二区三区另类| 国产欧美日韩在线播放| 欧美一区日本一区韩国一区| 老牛嫩草一区二区三区日本| 亚洲国产激情| 欧美日韩精品二区| 亚洲午夜激情| 久久午夜羞羞影院免费观看| 永久免费毛片在线播放不卡| 免费看黄裸体一级大秀欧美| 亚洲精品色图| 久久国产精品一区二区三区四区| 国产日韩精品视频一区二区三区| 欧美综合激情网| 亚洲国产成人91精品| 亚洲午夜电影在线观看| 国产日韩欧美精品| 欧美成人免费网| 亚洲自拍啪啪| 亚洲第一福利视频| 午夜精品三级视频福利| 一区二区三区国产盗摄| 国产精品自拍小视频| 久久久久久穴| 一区二区欧美国产| 久久综合狠狠综合久久激情| 亚洲人成绝费网站色www| 国产精品扒开腿做爽爽爽视频| 香蕉久久久久久久av网站| 亚洲国产老妈| 久久久国产精品亚洲一区| 日韩视频在线观看国产| 国产日韩欧美一区在线| 欧美男人的天堂| 久久久在线视频| 亚洲女同在线| 亚洲乱码国产乱码精品精天堂 | 亚洲国产一区二区视频| 国产精品va在线播放我和闺蜜| 久久精品成人欧美大片古装| 亚洲精品免费一二三区| 久久久亚洲一区| 亚洲午夜视频| 亚洲精品国产精品国自产观看浪潮| 国产精品美女xx| 欧美日韩国产专区| 老司机免费视频一区二区| 午夜亚洲精品| 亚洲精品一品区二品区三品区| 国产女人精品视频| 欧美日韩一区二区免费在线观看| 久久久久久9| 亚洲主播在线播放| 国产精品午夜久久| 欧美日韩精品一区| 欧美黄色一级视频|