• <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 若余 閱讀(963) 評論(0)  編輯 收藏 引用

            導航

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

            統計

            常用鏈接

            留言簿

            隨筆檔案(16)

            搜索

            最新隨筆

            最新評論

            評論排行榜

            久久久久亚洲AV片无码下载蜜桃 | 久久综合给合久久狠狠狠97色69| 久久久黄色大片| 日韩精品久久无码中文字幕| 狠狠狠色丁香婷婷综合久久俺| 色综合久久88色综合天天| 精品久久久久一区二区三区 | 久久国产福利免费| 精产国品久久一二三产区区别 | 熟妇人妻久久中文字幕| 国产免费久久久久久无码| 久久久久久亚洲精品影院| 久久精品人人做人人爽电影| 思思久久好好热精品国产| 99精品久久精品一区二区| 伊人情人综合成人久久网小说| 99国产欧美精品久久久蜜芽| 色综合久久天天综线观看| 国产成人精品久久一区二区三区| 婷婷久久综合九色综合绿巨人| 久久久久久a亚洲欧洲aⅴ| 精品国产乱码久久久久软件| 午夜精品久久久久久久无码| 久久噜噜电影你懂的| 97久久久久人妻精品专区| 久久久久久伊人高潮影院| 日批日出水久久亚洲精品tv| 99久久综合狠狠综合久久| 精品国产一区二区三区久久| 久久午夜伦鲁片免费无码| 久久99精品久久久大学生| 99久久国产亚洲综合精品| 久久一区二区三区99| 久久99精品国产麻豆蜜芽| 亚洲国产精品久久久久久| 大蕉久久伊人中文字幕| 欧美伊香蕉久久综合类网站| 国产99久久九九精品无码| 久久se精品一区二区影院 | 色偷偷91久久综合噜噜噜噜| 日本久久中文字幕|