HDU Interesting Yang Yui Triangle (Lucas定理)

题意:求杨辉三角中第 n+1行不能整除 p的数目。

析:运用Lucas定理,只要统计C(ni, mi)中全都不是0的数目即可,因为是第 n+1行,所以ni每次都不变,也就是mi <= ni,那么C(ni, mi),就不是0.

所以就有ni+1种答案,最后乘起来即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int hr[]= {-2, -2, -1, -1, 1, 1, 2, 2};
const int hc[]= {-1, 1, -2, 2, -2, 2, -1, 1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}

int main(){
    int kase = 0;
    while(scanf("%d %d", &m, &n) == 2 && m+n){
        int ans = 1;
        while(n){
            ans *= n%m + 1;
            n /= m;
        }
        printf("Case %d: %04d\n", ++kase, ans%10000);
    }
    return 0;
}
时间: 2024-10-17 04:41:41

HDU Interesting Yang Yui Triangle (Lucas定理)的相关文章

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

LA3700 Interesting Yang Hui Triangle(Lucas定理)

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

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

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

POJ 3146 &amp; 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 trian

HDU 3037 Saving Beans (Lucas定理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p, 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(long long n, long lo

HDU 3037 Saving Beans(lucas定理)

题目大意:豆子数i (1~m)分到n颗树上.  树可以为空,那么对于每个i,分配方式是 C(n+i-1,n-1)......于是我用for(i=0-->m)做,不幸超时,m太大. 不过竟然公式可以化简: for(int i=0;i<=m;i++) C(n+i-1,n-1)=C(n+i-1,i) 组合原理: 公式 C(n,k) = C(n-1,k)+C(n-1,k-1) C(n-1,0)+C(n,1)+...+C(n+m-1,m) = C(n,0)+C(n,1)+C(n+1,2)+...+C(n

HDU 5446 Unknown Treasure(Lucas定理+CRT)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] 首先我们可以用Lucas定理求出对答案对每个质因子的模,然后我们发现只要求解这个同余方程组就可以得到答案,所以我们用中国剩余定理解决剩下的问题. [代码] #include <cstdio> #include <cstring> #include <algorithm> u

HDU 3037 Saving Beans(Lucas定理模板题)

Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They supp