E - Beautiful Numbers

本文介绍了一种使用乘法逆元结合二分幂算法解决特定数学问题的方法。问题旨在找出由两个特定数字组成的n位数中,哪些数的位数之和同样由这两个数字构成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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).

Sample Input

Input
1 3 3
Output
1
Input
2 3 10
Output

165


题意:

somebody喜欢两个数字a和b,他把各个位上完全由这两个数字组成的数字叫做good number(也可以只有a或只有b组成),要是组成的数字的各位加起来的和也是good number,那么这个由a,b组成的good number就是excellent数,输入三个数a,b,n,分别代表喜欢的数a,b和要组成的数的位数,求最多有多少个符合题意的excellent数?


解法:乘法逆元+二分幂

<span style="font-size:18px;">#include<stdio.h>
long long int mod=1000000007;
long long int vis[1000010]={1};
long long int charge(long long int m,long long int n)//快速二分幂求值
{
    if(n==0)
        return 1;
    long long int t=1;
    while(n)
    {
        if(n%2==1)
            t=t*m%mod;
        m=m*m%mod;
        n/=2;
    }
    return t;
}
int main()
{
    long long int  a,b,n,i,j,sum,t1,t2,ans;
    while(scanf("%lld%lld%lld",&a,&b,&n)!=EOF)
    {
        ans=0;
        for(i=1;i<=n;i++)
            vis[i]=vis[i-1]*i%mod;//求1到n的阶乘
        for(i=0;i<=n;i++)
        {
            j=n-i;
            sum=a*i+b*j;
            while(sum)
            {
                long long int t=sum%10;
                if(t!=a&&t!=b)
                    break;
                sum=sum/10;
            }
            if(sum==0)//其实这里还包含了一个乘法逆元的求法
            {
                t1=charge(vis[i],mod-2);
                t2=charge(vis[n-i],mod-2);
                ans+=vis[n]*t1%mod*t2%mod;
            }
        }
        printf("%lld\n",ans%mod);
    }
    return 0;
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值