(数论)D - Beautiful Numbers

D - Beautiful Numbers

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 ≤ 1e6).

Output

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

Examples

Input

1 3 3

Output

1 

Input

2 3 10

Output

165 

题目描述:

给出a,b两个数,如果一个十进制数只由a,b组成。他是一个good number。如果各个位数上的数字加起来是一个good number。那么就可以说它是一个excellent number。给出n表示数的位数。问一共由多少个excellent number。(结果取模1000000007 (1e9 + 7))

分析:

由于要把位置上各数加起来。而且只有2个数a,b组成,规定由n为数。所以假设选i个a和(n-m)个b组成这个数,只要判断它各位数和sum=a x m+b x (n-m)是否是good number即可。每选完一次,要对a,b排列,才能组成不同的数。因为有n个要排列,m个a重复,(n-m)个b重复。每次排列数为A(n,n)/(A(m,m) x A(n-m,n-m)),化简一下可以发现它等于C(m,n)。

计算较大的组合数取模,因为C(m,n)=n!/(m! x (n-m)!),所以要用费曼小定理计算。计算(n-m)!的时候,可以看成n!/n ->> n/(n-1) ->>。。。。依次除以(n-m),同样可以用费曼小定理。

注意大数幂运算要用到快速幂运算。

注意:

要预先处理阶乘,不然会T。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mod=1000000007;
int a,b;
bool good(int x)
{
    while(x)
    {
        int gewei=x%10;
        if(gewei==a||gewei==b)
        {
            x/=10;
        }
        else return false;
    }
    return true;
}
ll mod_pow(ll x,ll n)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}
ll cn;
ll cm;
ll cn_m;
ll C()
{
    //组合数
    ll ans=cn * mod_pow(cm,mod-2)%mod * mod_pow(cn_m,mod-2)%mod;
     return ans;
}
int main()
{
    int n;
    scanf("%d%d%d",&a,&b,&n);
    ll ans=0;
    cn=1;
    for(int i=2;i<=n;i++)
    {
        cn=i*cn%mod;
    }
    cn_m=cn;
    cm=1;
    for(int i=0;i<=n;i++)
    {
        ll num=i*a+(n-i)*b;

        if(i!=0)
        cm=cm*i%mod;

        if(n-i+1!=0&&i!=0)
        cn_m=cn_m*mod_pow(n-i+1,mod-2)%mod;

        if(good(num))
        {
            ans=(ans+C())%mod;
        }
    }
    cout<<ans;
    return 0;
} 
??

原文地址:https://www.cnblogs.com/studyshare777/p/12202678.html

时间: 2024-11-05 23:31:21

(数论)D - Beautiful Numbers的相关文章

CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+快速幂)

C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard 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

CodeForces - 55D Beautiful numbers (数位DP)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

Beautiful numbers

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just

C. Beautiful Numbers

C. Beautiful Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other) Total Submission(s) : 27   Accepted Submission(s) : 7 Problem Description Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly

CF 55D - Beautiful numbers(数位DP)

题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公倍数是 2520({5,7,8,9}),先预处理出所有可能的最小公倍数m[c] dp[i][d][c]表示长度i, 余数d,最小公倍数是m[c]的个数. #include<cstdio> #include<cstring> #define mod 2520 ll dp[35][2520

[暑假集训--数位dp]cf55D Beautiful numbers

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num