• <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>

            1430 Binary Stirling Numbers 斯特靈數

            Binary Stirling Numbers
            Time Limit: 1000MS Memory Limit: 10000K
            Total Submissions: 1040 Accepted: 346

            Description

            The Stirling number of the second kind S(n, m) stands for the number of ways to partition a set of n things into m nonempty subsets. For example, there are seven ways to split a four-element set into two parts:

            {1, 2, 3} U {4}, {1, 2, 4} U {3}, {1, 3, 4} U {2}, {2, 3, 4} U {1}
            
            {1, 2} U {3, 4}, {1, 3} U {2, 4}, {1, 4} U {2, 3}.


            There is a recurrence which allows to compute S(n, m) for all m and n.

            S(0, 0) = 1; S(n, 0) = 0 for n > 0; S(0, m) = 0 for m > 0;
            S(n, m) = m S(n - 1, m) + S(n - 1, m - 1), for n, m > 0.


            Your task is much "easier". Given integers n and m satisfying 1 <= m <= n, compute the parity of S(n, m), i.e. S(n, m) mod 2.


            Example

            S(4, 2) mod 2 = 1.


            Task

            Write a program which for each data set:
            reads two positive integers n and m,
            computes S(n, m) mod 2,
            writes the result.

            Input

            The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 200. The data sets follow.

            Line i + 1 contains the i-th data set - exactly two integers ni and mi separated by a single space, 1 <= mi <= ni <= 10^9.

            Output

            The output should consist of exactly d lines, one line for each data set. Line i, 1 <= i <= d, should contain 0 or 1, the value of S(ni, mi) mod 2.

            Sample Input

            1
            4 2

            Sample Output

            1

            Source


            判斷第二類斯特靈數模 2 的余數。

            在劉汝佳的黑書上有詳細解答,基本思路是枚舉數值較小的斯特靈數,從中尋找規律。

            下面這幅圖是從維基百科截出來的,有一個二進制斯特靈數與組合數的轉化公式。而組合數模二的余數就很容易了。

            我們知道,組合數C(N,M)=N ! / M ! /(N-M)!,因而只需求得階乘質因數分解式中二的重數即可解決問題。
            而N !質因數分解后2的重數可用下式來計算之。
            K=N/2+N/2^2+N/2^3+....
            上式的除法全是下取整。(可參見任何一本初等數論課本,如北大潘承洞編的那本《初等數論》)。

            這樣,這個問題就迎刃而解。

            另外,有一點說明的是上面那個圖形,就是分形幾何中一個很重要的例子——謝彬斯基墊片。楊輝三角也有類似的形狀。
            這是我用MATLAB作的一個楊輝三角的二進制圖形。

            #include <stdio.h>
            int main(int argc, char *argv[])
            {
                
                
            int n,m;
                
            int z,w1,w2;
                
            int t;
                
            int a,b,c;
                scanf(
            "%d",&t);
                
                
            while (t--)
                
            {
                    scanf(
            "%d%d",&n,&m);
                    z
            =n-(m+2)/2;
                    w1
            =(m-1)/2;
                    w2
            =z-w1;
                    a
            =0;
                    
            while (z)
                    
            {
                        z
            >>=1;
                        a
            +=z;
                    }

                    b
            =0;
                    
            while (w1)
                    
            {
                        w1
            >>=1;
                        b
            +=w1;
                    }

                    c
            =0;
                    
            while (w2)
                    
            {
                        w2
            >>=1;
                        c
            +=w2;
                    }

                    printf(
            "%d\n",(a-b-c)==0);

                }

                
                
            return 0;
            }



            posted on 2010-08-28 08:51 若余 閱讀(978) 評論(0)  編輯 收藏 引用

            導航

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            常用鏈接

            留言簿

            隨筆檔案(16)

            搜索

            最新隨筆

            最新評論

            評論排行榜

            97久久精品午夜一区二区| 亚洲欧美精品伊人久久| 久久国内免费视频| 一本色道久久88—综合亚洲精品| 中文国产成人精品久久不卡| 久久精品国产亚洲AV无码娇色 | www.久久热.com| 91久久精品电影| 99久久国产亚洲综合精品| 国产精品久久久久…| 武侠古典久久婷婷狼人伊人| 国产精品久久久久久福利漫画| 看全色黄大色大片免费久久久| 久久久久亚洲av无码专区喷水| 久久一本综合| 国产精品青草久久久久婷婷| 一本一本久久A久久综合精品| 精品久久久久久久中文字幕 | 国产高清美女一级a毛片久久w| 久久精品国产日本波多野结衣| 精品人妻伦九区久久AAA片69 | 免费观看久久精彩视频| 亚洲日韩中文无码久久| 伊人久久精品影院| 精品人妻伦一二三区久久| 狠狠色丁香久久综合婷婷| 一本色道久久88精品综合| 香蕉久久夜色精品国产尤物| 久久夜色撩人精品国产| 久久se这里只有精品| 国产精品成人无码久久久久久| 伊人久久大香线焦综合四虎| 99久久精品日本一区二区免费| 亚洲狠狠婷婷综合久久蜜芽| 久久久久亚洲AV片无码下载蜜桃| 亚洲欧美久久久久9999| 久久婷婷是五月综合色狠狠| 久久男人AV资源网站| 久久一区二区三区99| 亚洲日韩欧美一区久久久久我| 久久国内免费视频|