POJ 3146 & HDU 3304 Interesting Yang Yui Triangle(杨辉三角)

题目链接:

HDU 3304 :http://acm.hdu.edu.cn/showproblem.php?pid=3304

POJ 3146  :http://poj.org/problem?id=3146

Problem Description

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 1

1 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, N<=10^9) , 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: 0004
Case 2: 0012

Source

2006 Asia Regional Shanghai

题意:

求出杨辉三角的N+1行的数中不能被P整除的数的个数!

思路:

首先杨辉三角是对称的,

它的N+1行的每一位数分别是:C(n,0),C(n,1)…………C(n,n)!

n! 所包含的 p 的幂: f(n,p)
= n/p+n/(p^2)+n/(p^3)+...

C(n,m) = n!/(m!(n-m)!),判断这个数能否被 p 整除也就是看它是否含有因子p,

n!中含有n/p+n/p^2+....n/p^k个p,

又C(n,m)是整数,所以不能被p整除的情况应该是:n!中含p的个数等于其分子m!和 (n-m)!所含有p因子的个数和。

即需要满足:n/(p^i) = k/(p^i) + (n-k)/(p^i),

实际上可以枚举第i位的表示,即0,1,2,3...a。

每一位都满足,最后答案就是(a1+1)*(a2+1)...(ak+1);

具体请戳:http://blog.163.com/[email protected]/blog/static/15522639520103124641416/

代码如下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
//const double pi = atan(1.0)*4;
const double pi = 3.1415926535897932384626;
#define INF 1e18
//typedef long long LL;
//typedef __int64 LL;
int main()
{
    int P, N;
    int cas = 0;
    while(~scanf("%d%d",&P,&N))
    {
        if(P==0 && N==0)
            break;
        int sum = 1;
        for(int i = N; i > 0; i/=P)
        {
            sum*=i%P+1;
        }

        printf("Case %d: %04d\n",++cas,sum%10000);
    }
}
时间: 2024-08-05 15:15:13

POJ 3146 & HDU 3304 Interesting Yang Yui Triangle(杨辉三角)的相关文章

hdu 3304 Interesting Yang Yui Triangle

题意: 给出P,N,问第N行的斐波那契数模P不等于0的有多少个? 限制: P < 1000,N <= 10^9 思路: lucas定理, 如果: n = a[k]*p^k + a[k-1]*p^(k-1) + ... + a[1]*p + a[0] m = b[k]*p^k + b[k-1]*p^(k-1) + ... + b[1]*p + b[0] 则: C(n,m) = pe(i=0~k,C(a[i],b[i]))%p 其中pe表示连乘符号. 由于n已经确定,所以a[i] (0 <=

HDU 3304 Interesting Yang Yui Triangle lucas定理

输入p n 求杨辉三角的第n+1行不能被p整除的数有多少个 Lucas定理: A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]. 则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod p同余 即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p),在存在i.b[i]>a[i]时,mod值为0,所以必然整除.当对于全部i,b

Interesting Yang Yui Triangle(hdu3304)

Interesting Yang Yui Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 332    Accepted Submission(s): 199 Problem Description Harry is a Junior middle student. He is very interested in th

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

[LeetCode]15. Pascal&#39;s Triangle杨辉三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 杨辉三角主要有下列五条性质: 杨辉三角以正整数构成,数字左右对称,每行由1开始逐渐变大,然后变小,回到1. 第行的数字个数为个. 第行的第个数字为组合数. 第行数字和为. 除每行最左侧与最右侧的数字以

HDU 4927 Series 1(高精度+杨辉三角)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4927 解题报告:对于n,结果如下: C(0,n-1) *A[n] - C(1,n-1) * A[n-1] + C(2,n-1) * A[n-2] - C(3,n-1) * A[n-3] ....... C(n-1,n-1) * A[1]; n <= 3000,到了后面二项式会很大,因为要用到高精度的乘法和除法,所以直接用java的大数类写了,简单多了. 1 import java.math.BigI

Pascal&#39;s Triangle 杨辉三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 杨辉三角是二项式系数的一种写法,如果熟悉杨辉三角的五个性质,那么很好生成,可参见我的上一篇博文: http://www.cnblogs.com/grandyang/p/4031536.html 具体生

(LeetCode)Pascal&#39;s Triangle --- 杨辉三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Subscribe to see which companies asked this question 解题分析: 题目的这个帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉

[LeetCode] 118. Pascal&#39;s Triangle 杨辉三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 杨辉三角形,又称贾宪三角形.帕斯卡三角形.海亚姆三角形.巴斯卡三角形,是二项式系数在的一种写法,形似三角形,在中国首现于南宋杨辉的<详解九章算术>得名,书中杨辉说明是引自贾宪的<释锁算术>