Beautiful Numbers



Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format:

 Status

Description

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 by1000000007(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: abn(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

Output

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

Sample Input

Input
1 3 3
Output
1
Input
2 3 10
Output
165

Source

这是我第一次了解到乘法逆元的强大。


费马小定理:

 若a,p互质,并且p为质数,那么a^(p-1)%p=1;

乘法逆元:

若对于a,p存在x,使得a*x mod p=1,那么我们称x为a对p的乘法逆元。  

假如我们要求(a/b) mod p且无法直接求得a/b的值时,我们可以求出b对p的乘法逆元inv,那么(a/b) mod p=(a*inv) mod p。

证明:

假如inv是b对于p的乘法逆元,即b*inv=p*t+1(t为整数),移项得inv=(p*t+1)/b;

(a*inv) mod p
=(a*((p*t+1)/b)) mod p
=(a*(p*t/b+1/b)) mod p

=( (a*p*t/b) mod p+(a/b) mod p ) mod p;

由于(a*p*t/b) mod p =0,故(a/b) mod p=(a*inv) mod p。

所以a,a^(p-2)互为逆元

组合数C(m,n)=( m!/ ((m-n)!*n!) )%p={m!*[n!*(m-n)!]^(p-2)}%p;


由于题目中给出了n,所以我们可以枚举a的个数m,那么剩下的(n-m)位就是b。再判断a*m+b*(n-m)是不是good数,如果是,那么我们在答案中加上C(m,n)即可,枚举完毕即最终答案。另外1e9+7是质数

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=1000000007;
int a,b,n;
ll fact[1000005],c[1000005];
bool check(ll x)//检验是否是good number
{
    ll t;
    while(x!=0)
    {
        t=x%10;
        if(t!=a&&t!=b) return false;
        x=x/10;
    }
    return true;
}
ll power(ll a,ll b)//快速幂
{
    ll t=a%mod,ans=1;
    while(b!=0)
    {
        if((b&1)!=0)
            ans=(ans*t)%mod;
        t=(t*t)%mod;
        b=b/2;
    }
    return ans;
}
void init()//阶乘
{
    fact[0]=1;
    for(ll i=1; i<1e6+5; i++)
        fact[i]=fact[i-1]*i%mod;
}
int main()
{
    init();
    while(scanf("%d%d%d",&a,&b,&n)!=EOF)
    {
        ll ans=0;
        for(int i=0; i<=n; i++)
        {
            int temp=a*i+b*(n-i);
            if(check(temp)==true)
            {//公式
                ll t1=power(fact[i],mod-2)%mod;
                ll t2=power(fact[n-i],mod-2)%mod;
                ans+=(fact[n]%mod*t1%mod*t2%mod)%mod;
            }
        }
        cout<<ans%mod<<endl;
    }
    return 0;
}



Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format:

 Status

Description

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 by1000000007(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: abn(1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

Output

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

Sample Input

Input
1 3 3
Output
1
Input
2 3 10
Output
165

Source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值