寒假集训数论D

题目
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).
题目大意
给出a,b,n,求仅由a,b两种数组成的长度为n,且各位数字之和也由a,b两种数组成的数的个数。
解题思路
设一种由k个a,(n-k)个b组成,各位数字之和为b*n+(a-b)*k,这种数共有C(n,k)个.
枚举k,算出答案即可。
计算C(n,k)时要用到逆元的知识。
由于1000000007为质数,用费马小定理求逆元即可。

#include <cstdio>
#include <iostream>
using namespace std;
const int MOD=1e9+7;
long long a,b;
int n;
bool check(int x)
{
	while (x!=0)
	{
		if (x%10!=a && x%10!=b) return false;
		x/=10;
	}
	return true;
}
long long MI(long long a,long long b)
{
	long long base=a;
	long long ans=1;
	while (b)
	{
		if (b&1) ans=(ans*base)%MOD;
		base=(base*base)%MOD;
		b>>=1; 
	}
	return ans;
}
int main()
{
     cin>>a>>b>>n;
     long long ans=0;
     long long c=1;
     long long p=1; 
	 for (int i=0;i<=n;i++)
	   {
	   	 int x=b*n+(a-b)*i;
	   	 if (check(x)) 
			{
			  long long k=MI(p,MOD-2);
			  long long t=(k*c)%MOD;
			  ans=(ans+t)%MOD;
		    }
	   	 c=c*(n-i)%MOD;
	   	 p=(p*(i+1))%MOD;
	   }
	 cout<<ans<<endl;  	
} 
发布了41 篇原创文章 · 获赞 0 · 访问量 591

猜你喜欢

转载自blog.csdn.net/weixin_45723759/article/details/104044979
今日推荐