BZOJ3738 [Ontak2013]Kapitał 【扩展Lucas】

题目链接

BZOJ3738

题解

复习
同上
但是为了消去因子\(10\),处理\(2^k\)的时候,乘回\(2^{k_1}\)时,应同时计算\(5^{k_2}\)
如果\(k_1 \ge k_2\),乘上\(5^{k_2}\)的逆元
如果\(k_1 < k_2\),乘上\(5^{k_1}\)的逆元
处理\(5^k\)的时候同理

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 2000005,maxm = 100005,INF = 0x3f3f3f3f;
LL pr[2],pk[2],fac[2],P,k1,k2,now;
inline LL qpow(LL a,LL b,LL p){
	LL re = 1;
	for (; b; b >>= 1,a = 1ll * a * a % p)
		if (b & 1) re = 1ll * re * a % p;
	return re;
}
inline void exgcd(LL a,LL b,LL&d ,LL& x,LL& y){
	if (!b){d = a; x = 1; y = 0;}
	else exgcd(b,a % b,d,y,x),y -= (a / b) * x;
}
inline LL inv(LL n,LL p){
	LL d,x,y; exgcd(n,p,d,x,y);
	return (x % p + p) % p;
}
inline LL Fac(LL n,LL P,LL p){
	if (!n) return 1;
	LL ans = 1;
	if (n / P) ans = qpow(fac[now],n / P,P);
	LL E = n % P;
	for (LL i = 2; i <= E; i++)
		if (i % p) ans = 1ll * ans * i % P;
	return 1ll * ans * Fac(n / p,P,p) % P;
}
inline int C(LL n,LL m,int pk,int p){
	now = (p == 5);
	LL a = Fac(n,pk,p),b = Fac(m,pk,p),c = Fac(n - m,pk,p),ans;
	ans = 1ll * a * inv(b,pk) % pk * inv(c,pk) % pk;
	if (p == 2){
		if (k1 >= k2)
			ans = 1ll * ans * qpow(inv(5,pk),k2,pk) % pk * qpow(2,k1 - k2,pk) % pk;
		else ans = 1ll * ans * qpow(inv(5,pk),k1,pk) % pk;
	}
	else {
		if (k1 >= k2)
			ans = 1ll * ans * qpow(inv(2,pk),k2,pk) % pk;
		else ans = 1ll * ans * qpow(inv(2,pk),k1,pk) % pk * qpow(5,k2 - k1,pk) % pk;
	}
	return 1ll * ans * (P / pk) % P * inv(P / pk,pk) % P;
}
inline LL exlucas(LL n,LL m){
	for (LL i = n; i; i /= 2) k1 += i / 2;
	for (LL i = m; i; i /= 2) k1 -= i / 2;
	for (LL i = n - m; i; i /= 2) k1 -= i / 2;
	for (LL i = n; i; i /= 5) k2 += i / 5;
	for (LL i = m; i; i /= 5) k2 -= i / 5;
	for (LL i = n - m; i; i /= 5) k2 -= i / 5;
	LL re = 0;
	re = (re + C(n,m,pk[0],pr[0])) % P;
	re = (re + C(n,m,pk[1],pr[1])) % P;
	return re;
}
int main(){
	LL N,M,K;
	cin >> N >> M >> K;
	pr[0] = 2; pr[1] = 5; pk[0] = pk[1] = P = 1;
	REP(i,K) pk[0] *= 2,pk[1] *= 5,P *= 10;
	fac[0] = 1; for (LL i = 2; i < pk[0]; i++) if (i % 2) fac[0] = 1ll * fac[0] * i % pk[0];
	fac[1] = 1; for (LL i = 2; i < pk[1]; i++) if (i % 5) fac[1] = 1ll * fac[1] * i % pk[1];
	cout << setfill('0') << setw(K) << exlucas(N + M,N) << endl;
	return 0;
}

posted @ 2018-07-10 20:22  Mychael  阅读(155)  评论(0编辑  收藏  举报