LA3700 Interesting Yang Hui Triangle(Lucas定理)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   ar   for   div   cti   

Harry is a Junior middle student. He is very interested in the story told by his mathematics teacher about the Yang Hui triangle in the class yesterday. After class he wrote the following numbers to show the triangle our ancestor studied.


                         1                    1         1                1        2        1            1       3         3       1         1      4        6        4      1      1     5       10        10      5     1   1     6      15       20       15     6     11     7     21      35        35      21    7     1                       ......


He found many interesting things in the above triangle. It is symmetrical, and the first and the last numbers on each line is 1; there are exactly i numbers on the line i.

Then Harry studied the elements on every line deeply. Of course, his study is comprehensive.

Now he wanted to count the number of elements which are the multiple of 3 on each line. He found that the numbers of elements which are the multiple of 3 on line 2, 3, 4, 5, 6, 7, ... are 0, 0, 2, 1, 0, 4, ... So the numbers of elements which are not divided by 3 are 2, 3, 2, 4, 6, 3, ... , respectively. But he also found that it was not an easy job to do so with the number of lines increasing. Furthermore, he is not satisfied with the research on the numbers divided only by 3. So he asked you, an erudite expert, to offer him help. Your kind help would be highly appreciated by him.

Since the result may be very large and rather difficult to compute, you only need to tell Harry the last four digits of the result.

Input 

There are multiple test cases in the input file. Each test case contains two numbers P and N , (P < 1000, N109) , where P is a prime number and N is a positive decimal integer.

P = 0, N = 0 indicates the end of input file and should not be processed by your program.

Output 

For each test case, output the last four digits of the number of elements on the N + 1 line on Yang Hui Triangle which can not be divided by P in the format as indicated in the sample output.

Sample Input 

3 4 3 48 0 0

Sample Output 

Case 1:  0004Case 2:  0012

題意:楊輝三角第n行的n+1個數有多少個數可以被p整除思路:將n,m轉化成p進位,每一位分別為(a1,a2,a3,a4.....) ,(b1,b2,b3,b4.....)C(n,m)%p  =  C(a1,b1)*C(a2,b2)*C(a3,b3)......%p要使C(ai,bi) = 0 只有當bi>ai ,因此求不能整除的數的個數可以通過 (ai+1)累乘(計數原理)
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>using namespace std;int p,n;int ans;vector<int> digit;int main(){    int T = 1;    while(~scanf("%d%d",&p,&n) && p+n){        digit.clear();        ans = 1;        while(n){            digit.push_back(n%p);            n /= p;        }        for(int i = 0; i < digit.size(); i++){            ans = (ans*(digit[i]+1))%10000;        }        printf("Case %d: %04d\n",T++,ans);    }    return 0;}





LA3700 Interesting Yang Hui Triangle(Lucas定理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.