poj3146 题解

本文介绍了如何使用Lucas定理解决POJ3146算法题,通过将数字n转化为p进制,计算满足条件的数的个数,通过分解并进行乘法操作来求解。
摘要由CSDN通过智能技术生成

poj3146

题目大意:

给定一个 n , p n, p n,p ( n 0 … n ) \binom{n}{0 \dots n} (0nn) 有多少数不能被 p p p 整除。

我们考虑 L u c a s Lucas Lucas 定理。

( n m ) = ( n / p m / p ) × ( n m o d    p m m o d    p ) \binom{n}{m} = \binom{n / p}{m/ p} \times \binom{n \mod p}{m \mod p} (mn)=(m/pn/p)×(mmodpnmodp)

也就是将 n n n 分解成 p p p 进制。如果上面的大于下面的就是有值的。

所以直接进行分解之后乘法原理即可。

#include <iostream>
#include <cstdio>
using namespace std;

//#define Fread
//#define Getmod

#ifdef Fread
char buf[1 << 21], *iS, *iT;
#define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
#endif // Fread

template <typename T>
void r1(T &x) {
	x = 0;
	char c(getchar());
	int f(1);
	for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
	for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48);
	x *= f;
}

#ifdef Getmod
const int mod  = 1e9 + 7;
template <int mod>
struct typemod {
    int z;
    typemod(int a = 0) : z(a) {}
    inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);}
    inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);}
    inline int mul(int a,int b) const {return 1ll * a * b % mod;}
    typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));}
    typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));}
    typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));}
    typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;}
    typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;}
    typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;}
    int operator == (const typemod<mod> &x) const {return x.z == z;}
    int operator != (const typemod<mod> &x) const {return x.z != z;}
};
typedef typemod<mod> Tm;
#endif

#define int long long
const int maxn = 2e5 + 5;
const int maxm = maxn << 1;
const int mod = 1e4;

int p, n;
int Work() {
    int sum(1);
    while(n) {
        int tmp = n % p; n /= p;
        ++ tmp;
        sum = sum * tmp % mod;
    }
    return sum;
}

signed main() {
//    freopen("S.in", "r", stdin);
//    freopen("S.out", "w", stdout);
    int i, j, Case(0);
    while(scanf("%lld%lld", &p, &n) != EOF) {
        if(p == 0 && n == 0) return 0;
        ++ Case;
        printf("Case %lld: %04lld\n", Case, Work());
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值