CodeForces - 300C Beautiful Numbers 组合数取模 卢卡斯定理 模板

GDUT 2020寒假训练 数论 D

原题链接

题目

原题截图
outputstandard output
Vitaly is a very weird man. He’s got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.

For example, let’s say that Vitaly’s favourite digits are 1 and 3, then number 12 isn’t good and numbers 13 or 311 are. Also, number 111 is excellent and number 11 isn’t.

Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109 + 7).

A number’s length is the number of digits in its decimal representation without leading zeroes.

Input
The first line contains three integers: a, b, n (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

样例

input

1 3 3

output
1
input

2 3 10

output
165

题目大意

给出a和b,如果一个长度为n的数字中每一位都是a或者b且所有位的数字的和也符合上述规定,则说明这个数excellent,求长度为n的excellent的数的个数

思路

首先枚举有m个a,(n-m)个b,那么这些数相加的和 s u m = a m + b ( n m ) sum=am+b(n-m) ,那么判断这个数sum也是仅由a和b组成的数字,那么就有 C n m C^{m}_{n} 种方案,将方案数累计到ans中再模p即可
组合数取模
然后就是处理 C n m   m o d   p C^{m}_{n}\ mod\ p 的问题了。计算组合数取模可以用卢卡斯定理,

Lucas定理,对于质数p,有 C n m   m o d   p = C n / p m / p × C n   m o d   p m   m o d   p   m o d   p C^{m}_{n}\ mod\ p=C^{m/p}_{n/p}{\times}C^{m\ mod\ p}_{n\ mod\ p}\ mod\ p

那么对于n,m远大于p的情况下,使用lucas定理,将n,m模p,使其小于p,而对于n/p和m/p的部分可以继续使用卢卡斯定理进行范围的缩小。当m==0时返回1.
(这道题p远大于n和m所以不必使用lucas,当我注意到这点的时候已经学完lucas了

对于组合数公式有 C n m   =   n ! m ! ( n m ) ! C^{m}_{n}\ =\ {\frac{n!}{m!(n-m)!}}
所以 m ! ( n m ) ! {{m!(n-m)!}} 这部分要采用逆元的形式进行计算。
另外,因为组合数在计算的过程中要频繁的使用阶乘,所以可以将1~n的阶乘取模提前预处理出来节约时间

啊,还有一点要注意的是枚举的时候,m可以从0开始取,习惯性int i=1 导致一直WA疯狂怀疑是不是组合数那里写错了

代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e6+12;
long long fac[maxn];//fac[i]= (i!)%p
bool check(long long num,long long a,long long b)
{
	while(num)
	{
		if(num%10!=a&&num%10!=b)
			return false;
		num/=10;
	}
	return true;
}
long long power(long long a,long long b,long long mod)
{
	long long ans=1%mod;
	for(;b;b>>=1)
	{
		if(b&1)
		{
			ans=ans*a%mod;
		}
		a=a*a%mod;
	}
	
	return ans;
}
long long Comb(long long n,long long m,long long p)
{
	if(n<m)return 0;
	if(n==m||m==0)return 1;
	if(m==1)return n;
	return ( fac[n]*(power(fac[m]*fac[n-m]%p,p-2,p)) )%p;
}
long long Lucas(long long n,long long m,long long p)
{
	if(m==0)return 1;
	return (Comb(n%p,m%p,p)*Lucas(n/p,m/p,p))%p;
}
void pre(int n,long long p)
{
	fac[0]=1;
	fac[1]=1;
	for(int i=2;i<=n;i++)
	{
		fac[i]=fac[i-1]*i;
		fac[i]%=p;
	}
	return ;
}
int main()
{
	long long a,b,n;
	long long p=1e9+7;
	long long ans=0;
	cin>>a>>b>>n;
	pre(n,p);
	for(int i=0;i<=n;i++)//注意从0枚举
	{
		if(check((a*i+b*(n-i)),a,b))
		{
			ans+=Lucas(n,i,p);
			ans%=p;
			//cout<<i<<endl;
		}
	}
	cout<<ans<<endl;
	return 0;
}
发布了33 篇原创文章 · 获赞 1 · 访问量 674

猜你喜欢

转载自blog.csdn.net/xcy2001/article/details/104787736